Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,25 @@ void CheckOther::checkConstVariable()
}
}
if (tok->isUnaryOp("&") && Token::Match(tok, "& %varid%", var->declarationId())) {
usedInAssignment = isExpressionChangedAt(tok->next(), tok, 0, false, mSettings, true);
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;
break;
}
if (astIsRangeBasedForDecl(tok) && Token::Match(tok->astParent()->astOperand2(), "%varid%", var->declarationId())) {
Expand Down
26 changes: 19 additions & 7 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2794,31 +2794,34 @@ class TestOther : public TestFixture {
"void a(T& x) {\n"
" x.dostuff();\n"
" const U * y = dynamic_cast<const U *>(&x);\n"
" y->mutate();\n" // to avoid warnings that y can be const
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
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
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
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
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
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"
" 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
"}");
ASSERT_EQUALS("", errout.str());
check("struct T : public U { void dostuff() const {}};\n"
"void a(T& x) {\n"
" x.dostuff();\n"
" U const * const * * const y = dynamic_cast<U const * const * * const>(&x);\n"
" 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
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
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
check("struct T : public U { void dostuff() const {}};\n"
"void a(T& x) {\n"
" x.dostuff();\n"
" my::fancy<typename type const *> const * * const y = dynamic_cast<my::fancy<typename type const *> const * * const>(&x);\n"
" const U const * const * * const y = dynamic_cast<const U const * const * * const>(&x);\n"
" y->mutate();\n" // to avoid warnings that y can be const
"}");
ASSERT_EQUALS("", errout.str());
Expand Down Expand Up @@ -3371,6 +3374,15 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 's1' can be declared as reference to const\n"
"[test.cpp:1]: (style) Parameter 's2' can be declared as reference to const\n",
errout.str());

check("struct S {\n"
" void f(int& r) { p = &r; }\n"
Copy link
Copy Markdown
Contributor

@pfultz2 pfultz2 Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think isVariableChanged can be updated to handle this case. We are already doing something similiar for references.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not even expect isVariableChanged() to return true here.

" int* p;\n"
"};\n"
"void g(std::vector<int>& v1, std::vector<int*>& v2) {\n"
" std::transform(v1.begin(), v1.end(), v2.begin(), [](auto& x) { return &x; });\n"
Copy link
Copy Markdown
Contributor

@pfultz2 pfultz2 Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be handled by isVariableChanged. We need to check if it escapes. Ideally it would be nice to write a generic function(maybe escapes or referenceEscapes?) to check for this(with an indirect parameter for pointers and references), this should help to simplify the logic for this checker(and maybe others).

"}\n");
ASSERT_EQUALS("", errout.str());
}

void constParameterCallback() {
Expand Down Expand Up @@ -3760,7 +3772,7 @@ class TestOther : public TestFixture {
check("void f(int& i) {\n"
" new (&i) int();\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:1]: (style) Parameter 'i' can be declared as reference to const\n", errout.str()); // don't crash
ASSERT_EQUALS("", errout.str()); // don't crash

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