Variable never mutated (VariableNeverMutated)
Warnings that identify local var bindings which are never mutated after initialization and could instead be declared with let.
Overview
When a variable is introduced with var but never reassigned or otherwise mutated, it can be declared with let instead to explicitly indicate that the variable is constant.
Example
func swap(_ a: inout Int, _ b: inout Int) {
var temp = a // warning: variable 'temp' was never mutated; consider changing to 'let' constant
a = b
b = temp
}The fix is to change var to let:
func swap(_ a: inout Int, _ b: inout Int) {
let temp = a
a = b
b = temp
}Sub-groups
The VariableNeverMutated group contains the following sub-group, which can also be enabled or controlled independently:
doc:weak-mutability: Warns when a
weak varbinding is never mutated and could be aweak letinstead.
See Also
@dynamicCallable implementation requirements (DynamicCallable)Actors can’t conform to global actor protocols (ActorConformanceToGlobalActorProtocol)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)