]> granicus.if.org Git - clang/commitdiff
refine the "use of unary operator that may be intended as compound assignment (+=)"
authorChris Lattner <sabre@nondot.org>
Sun, 8 Mar 2009 06:51:10 +0000 (06:51 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 8 Mar 2009 06:51:10 +0000 (06:51 +0000)
warning to only trigger when there is whitespace or something else after the + as
suggested by Eli.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66370 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/Sema/exprs.c

index 79be16252097847c9a86bdab0de6ffe4686c6e5a..6c4176720af9955cd304cb6cc625984f594a61f0 100644 (file)
@@ -3479,10 +3479,14 @@ QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS,
            UO->getOpcode() == UnaryOperator::Minus) &&
           Loc.isFileID() && UO->getOperatorLoc().isFileID() &&
           // Only if the two operators are exactly adjacent.
-          Loc.getFileLocWithOffset(1) == UO->getOperatorLoc())
+          Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() &&
+          // And there is a space or other character before the subexpr of the
+          // unary +/-.  We don't want to warn on "x=-1".
+          Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart()) {
         Diag(Loc, diag::warn_not_compound_assign)
           << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-")
           << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc());
+      }
     }
   } else {
     // Compound assignment "x += y"
index ba411c528da44ee16a8ed9470a70511a90979bbf..45e146286a3451c30fd2e86de389ef3ccecc6c92 100644 (file)
@@ -20,8 +20,11 @@ void test4() {
       static int var;
       var =+ 5;  // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
       var =- 5;  // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
-      var = +5;
+      var = +5;  // no warning when space between the = and +.
       var = -5;
+
+      var =+5;  // no warning when the subexpr of the unary op has no space before it.
+      var =-5;
 }
 
 // rdar://6319320