Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Documentation/RuleDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ than binding and immediately discarding the value.
For example, `if let _ = someValue { ... }` is forbidden. Use `if someValue != nil { ... }`
instead.

Note: If the conditional binding carries an explicit type annotation (e.g. `if let _: S? = expr`),
we skip the transformation. Such annotations can be necessary to drive generic type inference
when a function mentions a type only in its return position.

Lint: `let _ = expr` inside a condition list will yield a lint error.

Format: `let _ = expr` inside a condition list will be replaced by `expr != nil`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import SwiftSyntaxBuilder
/// For example, `if let _ = someValue { ... }` is forbidden. Use `if someValue != nil { ... }`
/// instead.
///
/// Note: If the conditional binding carries an explicit type annotation (e.g. `if let _: S? = expr`),
/// we skip the transformation. Such annotations can be necessary to drive generic type inference
/// when a function mentions a type only in its return position.
///
/// Lint: `let _ = expr` inside a condition list will yield a lint error.
///
/// Format: `let _ = expr` inside a condition list will be replaced by `expr != nil`.
Expand All @@ -29,7 +33,8 @@ public final class UseExplicitNilCheckInConditions: SyntaxFormatRule {
case .optionalBinding(let optionalBindingCondition):
guard
let initializerClause = optionalBindingCondition.initializer,
isDiscardedAssignmentPattern(optionalBindingCondition.pattern)
isDiscardedAssignmentPattern(optionalBindingCondition.pattern),
optionalBindingCondition.typeAnnotation == nil
else {
return node
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,18 @@ final class UseExplicitNilCheckInConditionsTests: LintOrFormatRuleTestCase {
]
)
}

func testTypeAnnotations() {
assertFormatting(
UseExplicitNilCheckInConditions.self,
input: """
if let _: F = foo() {}
if let _: S? = foo() {}
""",
expected: """
if let _: F = foo() {}
if let _: S? = foo() {}
"""
)
}
}
Loading