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 valueIf 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") } }
See Also
@dynamicCallable implementation requirements (DynamicCallable)Add @preconcurrency import (AddPreconcurrencyImport)Always enabled availability domains (AlwaysAvailableDomain)Argument matching for trailing closures (TrailingClosureMatching)Calling a mutating async actor-isolated method (ActorIsolatedMutatingAsync)Calling an actor-isolated method from a synchronous nonisolated context (ActorIsolatedCall)Captures in a `@Sendable` closure (SendableClosureCaptures)Compilation caching (CompilationCaching)Conforming to `StringInterpolationProtocol` (StringInterpolationConformance)Conversion from `@isolated(any)` function type to synchronous function type (ConversionFromIsolatedAnyToSynchronous)Cross-isolation data race (RegionIsolationCrossIsolationDataRace)Deprecated declaration warnings (DeprecatedDeclaration)Deprecated implementation-only imports (ImplementationOnlyDeprecated)Dynamic exclusivity (DynamicExclusivity)Embedded Swift language restrictions (EmbeddedRestrictions)