Author: Stephan Bergmann Date: 2021-06-14T08:04:03+02:00 New Revision: b5b9489b2415dc48c39d4d7d4bae6197dc499f38
URL: https://github.com/llvm/llvm-project/commit/b5b9489b2415dc48c39d4d7d4bae6197dc499f38 DIFF: https://github.com/llvm/llvm-project/commit/b5b9489b2415dc48c39d4d7d4bae6197dc499f38.diff LOG: Only consider built-in compound assignment operators for -Wunused-but-set-* At least LibreOffice has, for mainly historic reasons that would be hard to change now, a class Any with an overloaded operator >>= that semantically does not assign to the LHS but rather extracts into the (by-reference) RHS. Which thus caused false positive -Wunused-but-set-parameter and -Wunused-but-set-variable after those have been introduced recently. This change is more conservative about the assumed semantics of overloaded operators, excluding compound assignment operators but keeping plain operator = ones. At least for LibreOffice, that strikes a good balance of not producing false positives but still finding lots of true ones. (The change to the BinaryOperator case in MaybeDecrementCount is necessary because e.g. the template f4 test code in warn-unused-but-set-variables-cpp.cpp turns the += into a BinaryOperator.) Differential Revision: https://reviews.llvm.org/D103949 Added: Modified: clang/lib/Sema/SemaExprCXX.cpp clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp Removed: ################################################################################ diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 20925f95f18e..a57c5ad198e1 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -7808,11 +7808,15 @@ static void MaybeDecrementCount( Expr *E, llvm::DenseMap<const VarDecl *, int> &RefsMinusAssignments) { DeclRefExpr *LHS = nullptr; if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { - if (!BO->isAssignmentOp()) + if (BO->getLHS()->getType()->isDependentType() || + BO->getRHS()->getType()->isDependentType()) { + if (BO->getOpcode() != BO_Assign) + return; + } else if (!BO->isAssignmentOp()) return; LHS = dyn_cast<DeclRefExpr>(BO->getLHS()); } else if (CXXOperatorCallExpr *COCE = dyn_cast<CXXOperatorCallExpr>(E)) { - if (!COCE->isAssignmentOp()) + if (COCE->getOperator() != OO_Equal) return; LHS = dyn_cast<DeclRefExpr>(COCE->getArg(0)); } diff --git a/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp b/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp index aa598408dee5..400e9d7681b3 100644 --- a/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp +++ b/clang/test/SemaCXX/warn-unused-but-set-variables-cpp.cpp @@ -6,6 +6,7 @@ struct S { struct __attribute__((warn_unused)) SWarnUnused { int j; + void operator +=(int); }; int f0() { @@ -48,3 +49,16 @@ void f2() { char a[x]; char b[y]; } + +void f3(int n) { + // Don't warn for overloaded compound assignment operators. + SWarnUnused swu; + swu += n; +} + +template<typename T> void f4(T n) { + // Don't warn for (potentially) overloaded compound assignment operators in + // template code. + SWarnUnused swu; + swu += n; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits