Useless conditional statement (UselessConditionalStatement)
Warnings that identify conditional statements (if, while, and guard) whose conditions can never be false.
Overview
When the condition of an if, while, or guard statement is statically known to always be true, the conditional structure is unnecessary. For if and while, the body always executes (and a while loop with no break would never terminate); for guard, the else branch is unreachable.
Examples
let x = 0
if case _ = x { // warning: 'if' condition is always true
// ...
}
while case _ = x { // warning: 'while' condition is always true
break
}
guard case _ = x else { // warning: 'guard' condition is always true, body is unreachable
fatalError()
}In the examples above, the pattern matching conditions (case _ = ...) can never fail.
See Also
Related Documentation
@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)