Daniel Tull: Today I Learned

Deprecating functions silences deprecation warnings

Friday, 18 July 2025

In Swift, when we want to deprecate a function, we apply the @available attribute with the deprecated argument.

@available(*, deprecated, message: "Not a useful function.")
public func hello(_ name: String) {
  print("Hello, \(name)")
}

However, especially in the case of swift packages, we’d often like to ensure this behaviour continues to work for users who haven’t changed their code. This has the downside that in tests, usages will gather a warning causing a distraction.

@Test func hello() {
  #expect(hello("Daniel") == "Hello, Daniel”) // << 'hello' is deprecated: Not a useful function.
}

Today, I learned that if you apply an @available attribute to the test function, it will silence the warning entirely.

@available(*, deprecated)
@Test func hello() {
  #expect(hello("Daniel") == "Hello, Daniel”) // No warnings here
}

This doesn’t just work for tests, but for all code that uses deprecated code.

@available(*, deprecated, message: "Also not a useful function.")
public func lowercaseHello(_ name: String) -> String {
  hello(name).lowercased() // No warnings here
}

This makes it possible to deprecate a whole set of code together without polluting the issue navigator.