Skip to content

Fix a couple of issues with the fixit for unused if let bindings. #1963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,10 @@ bool Expr::canAppendCallParentheses() const {
case ExprKind::PointerToPointer:
case ExprKind::LValueToPointer:
case ExprKind::ForeignObjectConversion:
return false;
// Implicit conversion nodes have no syntax of their own; defer to the
// subexpression.
return cast<ImplicitConversionExpr>(this)->getSubExpr()
->canAppendCallParentheses();

case ExprKind::ForcedCheckedCast:
case ExprKind::ConditionalCheckedCast:
Expand Down
9 changes: 4 additions & 5 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,9 +1460,7 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
if (auto LP = dyn_cast<VarPattern>(OSP->getSubPattern()))
if (isa<NamedPattern>(LP->getSubPattern())) {
auto initExpr = SC->getCond()[0].getInitializer();
auto beforeExprLoc =
initExpr->getStartLoc().getAdvancedLocOrInvalid(-1);
if (beforeExprLoc.isValid()) {
if (initExpr->getStartLoc().isValid()) {
unsigned noParens = initExpr->canAppendCallParentheses();

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

if (isIsTest) {
// If this was an "x as? T" check, rewrite it to "x is T".
Expand Down
15 changes: 13 additions & 2 deletions test/decl/var/usage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ func testFixitsInStatementsWithPatterns(a : Int?) {
}
}


// <rdar://22774938> QoI: "never used" in an "if let" should rewrite expression to use != nil
func test(a : Int?, b : Any) {
if true == true, let x = a { // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}} {{24-25=_}}
Expand All @@ -217,7 +216,19 @@ func test(a : Int?, b : Any) {
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}}
}


// SR-1112

let xxx: Int? = 0

if let yyy = xxx { } // expected-warning{{with boolean test}} {{6-16=}} {{19-19= != nil}}

var zzz: Int? = 0
zzz = 1

if let yyy = zzz { } // expected-warning{{with boolean test}} {{6-16=}} {{19-19= != nil}}

if let yyy = zzz ?? xxx { } // expected-warning{{with boolean test}} {{6-16=(}} {{26-26=) != nil}}

}