Skip to content

Commit 88d056a

Browse files
committed
Merge pull request #1961 from jckarter/unused-if-let-fixit
Fix a couple of issues with the fixit for unused `if let` bindings.
2 parents defe843 + 07152b5 commit 88d056a

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
@@ -1466,9 +1466,7 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
14661466
if (auto LP = dyn_cast<VarPattern>(OSP->getSubPattern()))
14671467
if (isa<NamedPattern>(LP->getSubPattern())) {
14681468
auto initExpr = SC->getCond()[0].getInitializer();
1469-
auto beforeExprLoc =
1470-
initExpr->getStartLoc().getAdvancedLocOrInvalid(-1);
1471-
if (beforeExprLoc.isValid()) {
1469+
if (initExpr->getStartLoc().isValid()) {
14721470
unsigned noParens = initExpr->canAppendCallParentheses();
14731471

14741472
// If the subexpr is an "as?" cast, we can rewrite it to
@@ -1483,8 +1481,9 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
14831481
diag::pbd_never_used_stmtcond,
14841482
var->getName());
14851483
auto introducerLoc = SC->getCond()[0].getIntroducerLoc();
1486-
diagIF.fixItReplace(SourceRange(introducerLoc, beforeExprLoc),
1487-
&"("[noParens]);
1484+
diagIF.fixItReplaceChars(introducerLoc,
1485+
initExpr->getStartLoc(),
1486+
&"("[noParens]);
14881487

14891488
if (isIsTest) {
14901489
// 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)