Skip to content

Commit 3b3914d

Browse files
committed
Merge pull request #1963 from jckarter/unused-if-let-fixit-2.2
Fix a couple of issues with the fixit for unused `if let` bindings.
2 parents eda3c8d + b1e3de5 commit 3b3914d

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

lib/AST/Expr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,10 @@ bool Expr::canAppendCallParentheses() const {
597597
case ExprKind::PointerToPointer:
598598
case ExprKind::LValueToPointer:
599599
case ExprKind::ForeignObjectConversion:
600-
return false;
600+
// Implicit conversion nodes have no syntax of their own; defer to the
601+
// subexpression.
602+
return cast<ImplicitConversionExpr>(this)->getSubExpr()
603+
->canAppendCallParentheses();
601604

602605
case ExprKind::ForcedCheckedCast:
603606
case ExprKind::ConditionalCheckedCast:

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,9 +1460,7 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
14601460
if (auto LP = dyn_cast<VarPattern>(OSP->getSubPattern()))
14611461
if (isa<NamedPattern>(LP->getSubPattern())) {
14621462
auto initExpr = SC->getCond()[0].getInitializer();
1463-
auto beforeExprLoc =
1464-
initExpr->getStartLoc().getAdvancedLocOrInvalid(-1);
1465-
if (beforeExprLoc.isValid()) {
1463+
if (initExpr->getStartLoc().isValid()) {
14661464
unsigned noParens = initExpr->canAppendCallParentheses();
14671465

14681466
// If the subexpr is an "as?" cast, we can rewrite it to
@@ -1477,8 +1475,9 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
14771475
diag::pbd_never_used_stmtcond,
14781476
var->getName());
14791477
auto introducerLoc = SC->getCond()[0].getIntroducerLoc();
1480-
diagIF.fixItReplace(SourceRange(introducerLoc, beforeExprLoc),
1481-
&"("[noParens]);
1478+
diagIF.fixItReplaceChars(introducerLoc,
1479+
initExpr->getStartLoc(),
1480+
&"("[noParens]);
14821481

14831482
if (isIsTest) {
14841483
// If this was an "x as? T" check, rewrite it to "x is T".

test/decl/var/usage.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ func testFixitsInStatementsWithPatterns(a : Int?) {
197197
}
198198
}
199199

200-
201200
// <rdar://22774938> QoI: "never used" in an "if let" should rewrite expression to use != nil
202201
func test(a : Int?, b : Any) {
203202
if true == true, let x = a { // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}} {{24-25=_}}
@@ -217,7 +216,19 @@ func test(a : Int?, b : Any) {
217216
if let x = b as? Int { // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{6-14=}} {{16-19=is}}
218217
}
219218

220-
219+
// SR-1112
220+
221+
let xxx: Int? = 0
222+
223+
if let yyy = xxx { } // expected-warning{{with boolean test}} {{6-16=}} {{19-19= != nil}}
224+
225+
var zzz: Int? = 0
226+
zzz = 1
227+
228+
if let yyy = zzz { } // expected-warning{{with boolean test}} {{6-16=}} {{19-19= != nil}}
229+
230+
if let yyy = zzz ?? xxx { } // expected-warning{{with boolean test}} {{6-16=(}} {{26-26=) != nil}}
231+
221232
}
222233

223234

0 commit comments

Comments
 (0)