Skip to content

Commit c2d2ed6

Browse files
committed
Look for keywords in inactive #if regions when checking for variable uses
Fixes issue #79555
1 parent 08af019 commit c2d2ed6

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,15 @@ private enum InactiveCodeChecker {
359359
// match.
360360
switch self {
361361
case .name(let name):
362-
guard let identifier = token.identifier, identifier.name == name else {
363-
continue
362+
if let identifier = token.identifier, identifier.name == name {
363+
break
364364
}
365365

366-
break
366+
if case .keyword = token.tokenKind, token.text == name {
367+
break
368+
}
369+
370+
continue
367371

368372
case .tryOrThrow:
369373
guard let keywordKind = token.keywordKind,

test/decl/var/usage.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,16 @@ func testUselessCastWithInvalidParam(foo: Any?) -> Int {
563563
if let bar = foo as? Foo { return 42 } // expected-warning {{value 'bar' was defined but never used; consider replacing with boolean test}} {{6-16=}} {{20-23=is}}
564564
else { return 54 }
565565
}
566+
567+
// https://github.com/swiftlang/swift/issues/79555
568+
final class A {
569+
var x: () -> Void {
570+
{ [weak self] in // Used to warn: variable 'self' was written to, but never read
571+
#if NOT_PROCESSED
572+
self?.f()
573+
#endif
574+
}
575+
}
576+
577+
func f() {}
578+
}

0 commit comments

Comments
 (0)