Skip to content

Commit 33981fe

Browse files
Fix #12214 FN constParameterReference / #12216 FP constParameterReference (#5691)
1 parent c1f6132 commit 33981fe

7 files changed

Lines changed: 29 additions & 31 deletions

File tree

lib/astutils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,8 @@ static T* getTokenArgumentFunctionImpl(T* tok, int& argn)
22902290
tok = tok->astOperand1();
22912291
while (tok && (tok->isUnaryOp("*") || tok->str() == "["))
22922292
tok = tok->astOperand1();
2293+
if (Token::Match(tok, ". * %name%")) // bailout for pointer to member
2294+
return tok->tokAt(2);
22932295
while (Token::simpleMatch(tok, "."))
22942296
tok = tok->astOperand2();
22952297
while (Token::simpleMatch(tok, "::")) {
@@ -2630,6 +2632,8 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
26302632
if (!ftok->function() || !ftok->function()->isConst())
26312633
return true;
26322634
}
2635+
if (Token::Match(tok2->astParent(), ". * %name%")) // bailout
2636+
return true;
26332637

26342638
if (Token::simpleMatch(tok2, "[") && astIsContainer(tok) && vt && vt->container && vt->container->stdAssociativeLike)
26352639
return true;

lib/checkother.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,8 @@ void CheckOther::checkConstVariable()
13901390
continue;
13911391
if (var->isVolatile())
13921392
continue;
1393+
if (var->nameToken()->isExpandedMacro())
1394+
continue;
13931395
if (isStructuredBindingVariable(var)) // TODO: check all bound variables
13941396
continue;
13951397
if (isVariableChanged(var, mSettings, mTokenizer->isCPP()))
@@ -1461,25 +1463,6 @@ void CheckOther::checkConstVariable()
14611463
if (usedInAssignment)
14621464
continue;
14631465
}
1464-
// Skip if we ever cast this variable to a pointer/reference to a non-const type
1465-
{
1466-
bool castToNonConst = false;
1467-
for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) {
1468-
if (tok->isCast()) {
1469-
if (!tok->valueType()) {
1470-
castToNonConst = true; // safe guess
1471-
break;
1472-
}
1473-
const bool isConst = tok->valueType()->isConst(tok->valueType()->pointer);
1474-
if (!isConst) {
1475-
castToNonConst = true;
1476-
break;
1477-
}
1478-
}
1479-
}
1480-
if (castToNonConst)
1481-
continue;
1482-
}
14831466

14841467
constVariableError(var, hasFunction ? function : nullptr);
14851468
}

lib/tokenize.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ namespace {
619619
bool mUsed = false;
620620

621621
public:
622-
TypedefSimplifier(Token* typedefToken, int &num) : mTypedefToken(typedefToken) {
622+
explicit TypedefSimplifier(Token* typedefToken) : mTypedefToken(typedefToken) {
623623
Token* start = typedefToken->next();
624624
if (Token::simpleMatch(start, "typename"))
625625
start = start->next();
@@ -641,7 +641,6 @@ namespace {
641641
mRangeTypeQualifiers = rangeQualifiers;
642642
Token* typeName = rangeBefore.second->previous();
643643
if (typeName->isKeyword()) {
644-
(void)num;
645644
// TODO typeName->insertToken("T:" + std::to_string(num++));
646645
typeName->insertToken(nameToken->str());
647646
}
@@ -1048,16 +1047,14 @@ void Tokenizer::simplifyTypedef()
10481047
std::map<std::string, int> numberOfTypedefs;
10491048
for (Token* tok = list.front(); tok; tok = tok->next()) {
10501049
if (tok->str() == "typedef") {
1051-
int dummy = 0;
1052-
TypedefSimplifier ts(tok, dummy);
1050+
TypedefSimplifier ts(tok);
10531051
if (!ts.fail())
10541052
numberOfTypedefs[ts.name()]++;
10551053
continue;
10561054
}
10571055
}
10581056

10591057
int indentlevel = 0;
1060-
int typeNum = 1;
10611058
std::map<std::string, TypedefSimplifier> typedefs;
10621059
for (Token* tok = list.front(); tok; tok = tok->next()) {
10631060
if (!tok->isName()) {
@@ -1069,7 +1066,7 @@ void Tokenizer::simplifyTypedef()
10691066
}
10701067

10711068
if (indentlevel == 0 && tok->str() == "typedef") {
1072-
TypedefSimplifier ts(tok, typeNum);
1069+
TypedefSimplifier ts(tok);
10731070
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1) {
10741071
if (mSettings->severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs))
10751072
reportError(tok->next(), Severity::portability, "invalidConstFunctionType",

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5238,7 +5238,7 @@ static const Scope* getLoopScope(const Token* tok)
52385238
}
52395239

52405240
//
5241-
static void valueFlowConditionExpressions(TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger *errorLogger, const Settings &settings)
5241+
static void valueFlowConditionExpressions(const TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger *errorLogger, const Settings &settings)
52425242
{
52435243
for (const Scope * scope : symboldatabase.functionScopes) {
52445244
if (const Token* incompleteTok = findIncompleteVar(scope->bodyStart, scope->bodyEnd)) {
@@ -7482,7 +7482,7 @@ static void valueFlowInjectParameter(const TokenList& tokenlist,
74827482
settings);
74837483
}
74847484

7485-
static void valueFlowSwitchVariable(TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
7485+
static void valueFlowSwitchVariable(const TokenList &tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
74867486
{
74877487
for (const Scope &scope : symboldatabase.scopeList) {
74887488
if (scope.type != Scope::ScopeType::eSwitch)

test/cfg/std.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4206,7 +4206,7 @@ void uninivar_istream_read(std::istream &f)
42064206
f.read(buffer, size);
42074207
}
42084208

4209-
void uninitvar_string_compare(std::string &teststr, std::wstring &testwstr)
4209+
void uninitvar_string_compare(const std::string &teststr, const std::wstring &testwstr)
42104210
{
42114211
const char *pStrUninit;
42124212
// cppcheck-suppress uninitvar

test/cfg/wxwidgets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <wx/textctrl.h>
3030
#include <wx/propgrid/property.h>
3131

32-
void uninitvar_wxRegEx_GetMatch(wxRegEx &obj, size_t *start, size_t *len, size_t index)
32+
void uninitvar_wxRegEx_GetMatch(const wxRegEx &obj, size_t *start, size_t *len, size_t index)
3333
{
3434
size_t s,l;
3535
size_t *sPtr,*lPtr;

test/testother.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,7 +2873,7 @@ class TestOther : public TestFixture {
28732873
" x.f();\n"
28742874
" foo( static_cast<U2>(0) );\n"
28752875
"}");
2876-
ASSERT_EQUALS("", errout.str());
2876+
ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
28772877

28782878
check("class a {\n"
28792879
" void foo(const int& i) const;\n"
@@ -3375,6 +3375,18 @@ class TestOther : public TestFixture {
33753375
"[test.cpp:1]: (style) Parameter 's2' can be declared as reference to const\n",
33763376
errout.str());
33773377

3378+
check("void f(int& r) {\n" // #12214
3379+
" (void)(true);\n"
3380+
" if (r) {}\n"
3381+
"}\n");
3382+
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'r' can be declared as reference to const\n", errout.str());
3383+
3384+
check("struct S { void f(int&); };\n" // #12216
3385+
"void g(S& s, int& r, void (S::* p2m)(int&)) {\n"
3386+
" (s.*p2m)(r);\n"
3387+
"}\n");
3388+
ASSERT_EQUALS("", errout.str());
3389+
33783390
check("struct S {\n"
33793391
" void f(int& r) { p = &r; }\n"
33803392
" int* p;\n"
@@ -10861,7 +10873,9 @@ class TestOther : public TestFixture {
1086110873
" for (auto &j : g(std::move(l))) { (void)j; }\n"
1086210874
" }\n"
1086310875
"}\n");
10864-
ASSERT_EQUALS("[test.cpp:4]: (warning) Access of moved variable 'l'.\n", errout.str());
10876+
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'j' can be declared as reference to const\n"
10877+
"[test.cpp:4]: (warning) Access of moved variable 'l'.\n",
10878+
errout.str());
1086510879
}
1086610880

1086710881
void moveCallback()

0 commit comments

Comments
 (0)