Skip to content

Commit

Permalink
Use isExpressionChangedAt()
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Nov 20, 2023
1 parent 040952e commit d7fc3c9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
20 changes: 1 addition & 19 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,25 +1429,7 @@ void CheckOther::checkConstVariable()
}
}
if (tok->isUnaryOp("&") && Token::Match(tok, "& %varid%", var->declarationId())) {
const Token* opTok = tok->astParent();
int argn = -1;
if (opTok && (opTok->isUnaryOp("!") || opTok->isComparisonOp()))
continue;
if (opTok && (opTok->isAssignmentOp() || opTok->isCalculation())) {
if (opTok->isCalculation()) {
if (opTok->astOperand1() != tok)
opTok = opTok->astOperand1();
else
opTok = opTok->astOperand2();
}
if (opTok && opTok->valueType() && var->valueType() && opTok->valueType()->isConst(var->valueType()->pointer))
continue;
} else if (const Token* ftok = getTokenArgumentFunction(tok, argn)) {
bool inconclusive{};
if (var->valueType() && !isVariableChangedByFunctionCall(ftok, var->valueType()->pointer, var->declarationId(), mSettings, &inconclusive) && !inconclusive)
continue;
}
usedInAssignment = true;
usedInAssignment = isExpressionChangedAt(tok->next(), tok, 0, false, mSettings, true);
break;
}
if (astIsRangeBasedForDecl(tok) && Token::Match(tok->astParent()->astOperand2(), "%varid%", var->declarationId())) {
Expand Down
8 changes: 4 additions & 4 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2796,14 +2796,14 @@ class TestOther : public TestFixture {
" const U * y = dynamic_cast<const U *>(&x);\n"
" y->mutate();\n" // to avoid warnings that y can be const
"}");
TODO_ASSERT_EQUALS("can be const", errout.str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
check("struct T : public U { void dostuff() const {}};\n"
"void a(T& x) {\n"
" x.dostuff();\n"
" U const * y = dynamic_cast<U const *>(&x);\n"
" y->mutate();\n" // to avoid warnings that y can be const
"}");
TODO_ASSERT_EQUALS("can be const", errout.str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
check("struct T : public U { void dostuff() const {}};\n"
"void a(T& x) {\n"
" x.dostuff();\n"
Expand All @@ -2817,7 +2817,7 @@ class TestOther : public TestFixture {
" const U const * const * const * const y = dynamic_cast<const U const * const * const * const>(&x);\n"
" y->mutate();\n" // to avoid warnings that y can be const
"}");
TODO_ASSERT_EQUALS("can be const", errout.str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
check("struct T : public U { void dostuff() const {}};\n"
"void a(T& x) {\n"
" x.dostuff();\n"
Expand Down Expand Up @@ -3770,7 +3770,7 @@ class TestOther : public TestFixture {
check("void f(int& i) {\n"
" new (&i) int();\n"
"}\n");
ASSERT_EQUALS("", errout.str()); // don't crash
TODO_ASSERT_EQUALS("", "[test.cpp:1]: (style) Parameter 'i' can be declared as reference to const\n", errout.str()); // don't crash

check("void f(int& i) {\n"
" int& r = i;\n"
Expand Down

0 comments on commit d7fc3c9

Please sign in to comment.