Fix crash in constPointer() (#4932)

* Fix crash in constPointer()

* Format
This commit is contained in:
chrchr-github 2023-04-03 19:44:08 +02:00 committed by GitHub
parent b2b0962067
commit 86efca28a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -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()) {

View File

@ -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<S>& 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() {