Skip to content

Commit 77d8eaa

Browse files
Fix #12206 FN constParameterPointer with unknown const member (#5737)
1 parent c0a9927 commit 77d8eaa

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

lib/astutils.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn)
22542254
parent = parent->astParent();
22552255

22562256
// passing variable to subfunction?
2257-
if (Token::Match(parent, "[*[(,{]") || Token::Match(parent, "%oror%|&&"))
2257+
if (Token::Match(parent, "[*[(,{.]") || Token::Match(parent, "%oror%|&&"))
22582258
;
22592259
else if (Token::simpleMatch(parent, ":")) {
22602260
while (Token::Match(parent, "[?:]"))
@@ -3310,8 +3310,13 @@ ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings,
33103310
const Token* op = parent->astParent();
33113311
while (Token::simpleMatch(op, "."))
33123312
op = op->astParent();
3313-
if (Token::Match(op, "%assign%|++|--") && op->str() != "=")
3314-
return ExprUsage::Used;
3313+
if (Token::Match(op, "%assign%|++|--")) {
3314+
if (op->str() == "=") {
3315+
if (precedes(tok, op))
3316+
return ExprUsage::NotUsed;
3317+
} else
3318+
return ExprUsage::Used;
3319+
}
33153320
}
33163321
if (Token::simpleMatch(parent, "=") && astIsRHS(tok)) {
33173322
const Token* const lhs = parent->astOperand1();

test/testcondition.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,6 +5089,11 @@ class TestCondition : public TestFixture {
50895089
" return fwrite(s.c_str(), 1, s.length(), fp) == s.length();\n"
50905090
"}\n");
50915091
ASSERT_EQUALS("", errout.str());
5092+
5093+
check("void f(const std::string& s) {\n" // #9148
5094+
" if (s.empty() || s.size() < 1) {}\n"
5095+
"}\n");
5096+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 's.size()<1' is always false\n", errout.str());
50925097
}
50935098

50945099
void alwaysTrueLoop()

test/testother.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,29 @@ class TestOther : public TestFixture {
33953395
" std::transform(v1.begin(), v1.end(), v2.begin(), [](auto& x) { return &x; });\n"
33963396
"}\n");
33973397
ASSERT_EQUALS("", errout.str());
3398+
3399+
check("class T;\n" // #11869
3400+
"class E {\n"
3401+
"public:\n"
3402+
" class F {\n"
3403+
" public:\n"
3404+
" explicit F(const T* t);\n"
3405+
" };\n"
3406+
"};\n"
3407+
"void f(T& t) {\n"
3408+
" std::list<E::F> c(1, E::F(&t));\n"
3409+
"}\n");
3410+
ASSERT_EQUALS("[test.cpp:9]: (style) Parameter 't' can be declared as reference to const\n", errout.str());
3411+
3412+
check("struct T;\n"
3413+
"struct U {\n"
3414+
" struct V { explicit V(const T* p); };\n"
3415+
"};\n"
3416+
"void g(U::V v);\n"
3417+
"void f(T& t) {\n"
3418+
" g(U::V(&t));\n"
3419+
"}\n");
3420+
ASSERT_EQUALS("[test.cpp:6]: (style) Parameter 't' can be declared as reference to const\n", errout.str());
33983421
}
33993422

34003423
void constParameterCallback() {
@@ -3910,6 +3933,13 @@ class TestOther : public TestFixture {
39103933
"}\n");
39113934
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n",
39123935
errout.str());
3936+
3937+
check("struct S { const T* t; };\n" // #12206
3938+
"void f(S* s) {\n"
3939+
" if (s->t.i) {}\n"
3940+
"}\n");
3941+
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as pointer to const\n",
3942+
errout.str());
39133943
}
39143944

39153945
void switchRedundantAssignmentTest() {

0 commit comments

Comments
 (0)