Skip to content

Commit

Permalink
Fix #12206 FN constParameterPointer with unknown const member (#5737)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Dec 7, 2023
1 parent c0a9927 commit 77d8eaa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,7 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn)
parent = parent->astParent();

// passing variable to subfunction?
if (Token::Match(parent, "[*[(,{]") || Token::Match(parent, "%oror%|&&"))
if (Token::Match(parent, "[*[(,{.]") || Token::Match(parent, "%oror%|&&"))
;
else if (Token::simpleMatch(parent, ":")) {
while (Token::Match(parent, "[?:]"))
Expand Down Expand Up @@ -3310,8 +3310,13 @@ ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings,
const Token* op = parent->astParent();
while (Token::simpleMatch(op, "."))
op = op->astParent();
if (Token::Match(op, "%assign%|++|--") && op->str() != "=")
return ExprUsage::Used;
if (Token::Match(op, "%assign%|++|--")) {
if (op->str() == "=") {
if (precedes(tok, op))
return ExprUsage::NotUsed;
} else
return ExprUsage::Used;
}
}
if (Token::simpleMatch(parent, "=") && astIsRHS(tok)) {
const Token* const lhs = parent->astOperand1();
Expand Down
5 changes: 5 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5089,6 +5089,11 @@ class TestCondition : public TestFixture {
" return fwrite(s.c_str(), 1, s.length(), fp) == s.length();\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f(const std::string& s) {\n" // #9148
" if (s.empty() || s.size() < 1) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 's.size()<1' is always false\n", errout.str());
}

void alwaysTrueLoop()
Expand Down
30 changes: 30 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,29 @@ class TestOther : public TestFixture {
" std::transform(v1.begin(), v1.end(), v2.begin(), [](auto& x) { return &x; });\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("class T;\n" // #11869
"class E {\n"
"public:\n"
" class F {\n"
" public:\n"
" explicit F(const T* t);\n"
" };\n"
"};\n"
"void f(T& t) {\n"
" std::list<E::F> c(1, E::F(&t));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (style) Parameter 't' can be declared as reference to const\n", errout.str());

check("struct T;\n"
"struct U {\n"
" struct V { explicit V(const T* p); };\n"
"};\n"
"void g(U::V v);\n"
"void f(T& t) {\n"
" g(U::V(&t));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) Parameter 't' can be declared as reference to const\n", errout.str());
}

void constParameterCallback() {
Expand Down Expand Up @@ -3910,6 +3933,13 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n",
errout.str());

check("struct S { const T* t; };\n" // #12206
"void f(S* s) {\n"
" if (s->t.i) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as pointer to const\n",
errout.str());
}

void switchRedundantAssignmentTest() {
Expand Down

0 comments on commit 77d8eaa

Please sign in to comment.