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

- Fix SR-1112, where the fixit would eat all characters in the RHS expression except for the last one due to improper SourceRange math.
- Noticed by inspection, an expression undergoing an implicit conversion (such as a load from an optional `var`) would be judged as requiring parens, when that's really unnecessary.
This commit is contained in:
Joe Groff
2016-03-30 15:09:01 -07:00
parent b332321457
commit 07152b51e5
3 changed files with 21 additions and 8 deletions

View File

@@ -1466,9 +1466,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
@@ -1483,8 +1481,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".