diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c7305e89e..d3b395e20 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1479,7 +1479,7 @@ void CheckOther::checkConstVariable() break; } } - if (Token::Match(tok, "& %varid%", var->declarationId())) { + if (tok->isUnaryOp("&") && Token::Match(tok, "& %varid%", var->declarationId())) { const Token* opTok = tok->astParent(); if (opTok->isComparisonOp() || opTok->isAssignmentOp() || opTok->isCalculation()) { if (opTok->isComparisonOp() || opTok->isCalculation()) { diff --git a/test/testother.cpp b/test/testother.cpp index ec94525e8..922d47696 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3453,6 +3453,17 @@ private: "[test.cpp:11]: (style) Parameter 's' can be declared as pointer to const\n" "[test.cpp:14]: (style) Parameter 's' can be declared as pointer to const\n", errout.str()); + + check("struct S { int a; };\n" + "void f(std::vector& v, int b) {\n" + " size_t n = v.size();\n" + " for (size_t i = 0; i < n; i++) {\n" + " S& s = v[i];\n" + " if (!(b & s.a))\n" + " continue;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (style) Variable 's' can be declared as reference to const\n", errout.str()); // don't crash } void switchRedundantAssignmentTest() {