mirror of
https://github.com/apple/swift.git
synced 2026-06-20 15:42:51 +02:00
c0b5e6c7b8
Adds every diagnostic in the legacy `no-usage` diagnostic category to a new diagnostic group called NoUsage so that the diagnostics can participate in diagnostic control mechanisms like `-Werror`. Resolves rdar://160488389.
1.5 KiB
1.5 KiB
Unused values (NoUsage)
Overview
These diagnostics are emitted when a value is computed or defined but never used, which often indicates a programming mistake such as a missing assignment or an unintentionally discarded result.
The NoUsage group covers a range of unused-value situations like calling a function and ignoring its return value or declaring a variable or constant that is never read.
Example
func sortedCopy(_ array: [Int]) -> [Int] {
var copy = array
copy.sort() // ok: sort() mutates in place
array.sorted() // warning: result of call to 'sorted()' is unused
return copy
}
How to fix
-
Use the result, or assign it to
_to explicitly discard it:_ = incrementGlobalCounter() // increment the counter without using the new value -
If ignoring the return value of a function is intentional and common for callers, then annotate the function with
@discardableResult:private var counter = 0 @discardableResult // callers may not need to use the new value func incrementGlobalCounter() -> Int { counter += 1 return counter } -
Replace an unused binding with
_:enum NetworkError: Error { case requestFailed(statusCode: Int, message: String) case timeout } func handle(_ error: NetworkError) { switch error { case .requestFailed(let statusCode, _): // message not needed here print("Request failed with status \(statusCode)") case .timeout: print("Request timed out") } }