aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/SemaStmt.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2011-08-17 08:38:11 +0000
committerChandler Carruth <chandlerc@gmail.com>2011-08-17 08:38:11 +0000
commit50bf68fc9698742e36c311fc37e6e4b7de235c4b (patch)
treed287458caa1be426e98bbadcd85e0c52e7ada5af /lib/Sema/SemaStmt.cpp
parent9d8eb3b2a892697aed332f6c318a8554fc2623ce (diff)
Don't suggest assignment in implausible situation. We still warn, as the
code is very likely to be buggy, but its going to require more significant changes on the part of the user to correct it in this case. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137820 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaStmt.cpp')
-rw-r--r--lib/Sema/SemaStmt.cpp24
1 files changed, 15 insertions, 9 deletions
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 79ae67210f..79e317bb8c 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -147,21 +147,23 @@ static void DiagnoseTopLevelComparison(Sema &S, const Stmt *Statement) {
}
SourceLocation Loc;
- bool IsNotEqual = false;
+ bool IsNotEqual, CanAssign;
if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) {
if (Op->getOpcode() != BO_EQ && Op->getOpcode() != BO_NE)
return;
- IsNotEqual = Op->getOpcode() == BO_NE;
Loc = Op->getOperatorLoc();
+ IsNotEqual = Op->getOpcode() == BO_NE;
+ CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue();
} else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) {
if (Op->getOperator() != OO_EqualEqual &&
Op->getOperator() != OO_ExclaimEqual)
return;
- IsNotEqual = Op->getOperator() == OO_ExclaimEqual;
Loc = Op->getOperatorLoc();
+ IsNotEqual = Op->getOperator() == OO_ExclaimEqual;
+ CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue();
} else {
// Not a typo-prone comparison.
return;
@@ -181,12 +183,16 @@ static void DiagnoseTopLevelComparison(Sema &S, const Stmt *Statement) {
<< FixItHint::CreateInsertion(Open, "(void)(")
<< FixItHint::CreateInsertion(Close, ")");
- if (IsNotEqual)
- S.Diag(Loc, diag::note_inequality_comparison_to_or_assign)
- << FixItHint::CreateReplacement(Loc, "|=");
- else
- S.Diag(Loc, diag::note_equality_comparison_to_assign)
- << FixItHint::CreateReplacement(Loc, "=");
+ // If the LHS is a plausible entity to assign to, provide a fixit hint to
+ // correct common typos.
+ if (CanAssign) {
+ if (IsNotEqual)
+ S.Diag(Loc, diag::note_inequality_comparison_to_or_assign)
+ << FixItHint::CreateReplacement(Loc, "|=");
+ else
+ S.Diag(Loc, diag::note_equality_comparison_to_assign)
+ << FixItHint::CreateReplacement(Loc, "=");
+ }
}
void Sema::DiagnoseUnusedExprResult(const Stmt *S) {