From 4da68b41d54ad734d0ec425c008b550cdb9f2ce2 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 12:07:54 +0100 Subject: [PATCH 01/11] Fix FN constParameterReference --- lib/checkother.cpp | 16 ---------------- test/testother.cpp | 8 ++++++-- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1e5b719271c..4e52aadebf8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1333,20 +1333,6 @@ void CheckOther::passedByValueError(const Variable* var, bool inconclusive, bool reportError(errorPath, Severity::performance, id.c_str(), msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); } -static bool isUnusedVariable(const Variable *var) -{ - if (!var) - return false; - if (!var->scope()) - return false; - const Token *start = var->declEndToken(); - if (!start) - return false; - if (Token::Match(start, "; %varid% =", var->declarationId())) - start = start->tokAt(2); - return !Token::findmatch(start->next(), "%varid%", var->scope()->bodyEnd, var->declarationId()); -} - static bool isVariableMutableInInitializer(const Token* start, const Token * end, nonneg int varid) { if (!start) @@ -1402,8 +1388,6 @@ void CheckOther::checkConstVariable() if (function && var->isArgument()) { if (function->isImplicitlyVirtual() || function->templateDef) continue; - if (isUnusedVariable(var)) - continue; if (function->isConstructor() && isVariableMutableInInitializer(function->constructorMemberInitialization(), scope->bodyStart, var->declarationId())) continue; } diff --git a/test/testother.cpp b/test/testother.cpp index e85cd590bd5..12581c5012d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2576,11 +2576,15 @@ class TestOther : public TestFixture { "}"); ASSERT_EQUALS("", errout.str()); - // Perhaps unused variable should be checked as well. check("void f(int& x, int& y) {\n" " y++;\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'x' can be declared as reference to const\n", errout.str()); + + check("int f(int& t) {\n" // #11713 + " return 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 't' can be declared as reference to const\n", errout.str()); check("struct A {\n" " explicit A(int& y) : x(&y) {}\n" From d1dff1bb29568d0fd51a943c999ed2de339d16dd Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 12:50:42 +0100 Subject: [PATCH 02/11] Fix FN constParameterPointer --- lib/checkother.cpp | 37 +++++++++++++--- test/testother.cpp | 108 +++++++++++++++++---------------------------- 2 files changed, 70 insertions(+), 75 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4e52aadebf8..4afcab05d66 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1489,6 +1489,26 @@ static const Token* getVariableChangedStart(const Variable* p) return start; } +namespace { + struct CompareVariables { + bool operator()(const Variable* a, const Variable* b) const { + const int fileA = a->nameToken()->fileIndex(); + const int fileB = b->nameToken()->fileIndex(); + if (fileA == fileB) { + const int lineA = a->nameToken()->linenr(); + const int lineB = b->nameToken()->linenr(); + if (lineA == lineB) { + const int columnA = a->nameToken()->column(); + const int columnB = b->nameToken()->column(); + return columnA < columnB; + } + return lineA < lineB; + } + return fileA < fileB; + } + }; +} + void CheckOther::checkConstPointer() { if (!mSettings->severity.isEnabled(Severity::style) && @@ -1498,7 +1518,7 @@ void CheckOther::checkConstPointer() logChecker("CheckOther::checkConstPointer"); // style - std::vector pointers, nonConstPointers; + std::set pointers, nonConstPointers; for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { const Variable* const var = tok->variable(); if (!var) @@ -1506,10 +1526,12 @@ void CheckOther::checkConstPointer() if (!var->isLocal() && !var->isArgument()) continue; const Token* const nameTok = var->nameToken(); - // declarations of (static) pointers are (not) split up, array declarations are never split up - if (tok == nameTok && (!var->isStatic() || Token::simpleMatch(nameTok->next(), "[")) && - !astIsRangeBasedForDecl(nameTok)) - continue; + if (tok == nameTok) { + // declarations of (static) pointers are (not) split up, array declarations are never split up + if (var->isLocal() && (!var->isStatic() || Token::simpleMatch(nameTok->next(), "[")) && + !astIsRangeBasedForDecl(nameTok)) + continue; + } const ValueType* const vt = tok->valueType(); if (!vt) continue; @@ -1519,7 +1541,7 @@ void CheckOther::checkConstPointer() continue; if (std::find(nonConstPointers.cbegin(), nonConstPointers.cend(), var) != nonConstPointers.cend()) continue; - pointers.emplace_back(var); + pointers.emplace(var); const Token* parent = tok->astParent(); enum Deref { NONE, DEREF, MEMBER } deref = NONE; bool hasIncDec = false; @@ -1604,7 +1626,8 @@ void CheckOther::checkConstPointer() continue; } } - nonConstPointers.emplace_back(var); + if (tok != nameTok) + nonConstPointers.emplace(var); } for (const Variable *p: pointers) { if (p->isArgument()) { diff --git a/test/testother.cpp b/test/testother.cpp index 12581c5012d..12a77789a12 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3139,11 +3139,8 @@ class TestOther : public TestFixture { "void an();\n" "void h();"); ASSERT_EQUALS("[test.cpp:131]: (style) Variable 'tm' can be declared as pointer to const\n" - "[test.cpp:131]: (style) Variable 'tm' can be declared as pointer to const\n" // duplicate "[test.cpp:136]: (style) Variable 'af' can be declared as pointer to const\n" - "[test.cpp:137]: (style) Variable 'ag' can be declared as pointer to const\n" - "[test.cpp:136]: (style) Variable 'af' can be declared as pointer to const\n" // duplicate - "[test.cpp:137]: (style) Variable 'ag' can be declared as pointer to const\n", // duplicate + "[test.cpp:137]: (style) Variable 'ag' can be declared as pointer to const\n", errout.str()); check("class C\n" @@ -3359,9 +3356,7 @@ class TestOther : public TestFixture { " d->f();\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:4]: (style) Variable 'd' can be declared as pointer to const\n" - "[test.cpp:4]: (style) Variable 'd' can be declared as pointer to const\n" // duplicate - "[test.cpp:4]: (style) Variable 'd' can be declared as pointer to const\n", // duplicate + "[test.cpp:4]: (style) Variable 'd' can be declared as pointer to const\n", errout.str()); check("void g(const int*);\n" @@ -3370,8 +3365,7 @@ class TestOther : public TestFixture { " g(i);\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:3]: (style) Variable 'i' can be declared as pointer to const\n" - "[test.cpp:3]: (style) Variable 'i' can be declared as pointer to const\n", // duplicate + "[test.cpp:3]: (style) Variable 'i' can be declared as pointer to const\n", errout.str()); check("struct A {\n" // #11225 @@ -3748,8 +3742,7 @@ class TestOther : public TestFixture { " if (h) {}\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n" - "[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", // duplicate + "[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", errout.str()); check("void f(const std::vector& v) {\n" @@ -3759,8 +3752,7 @@ class TestOther : public TestFixture { " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); check("void f(std::vector& v) {\n" @@ -3774,8 +3766,7 @@ class TestOther : public TestFixture { " if (p == nullptr) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n" - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); check("void f(std::vector& v) {\n" @@ -3866,9 +3857,7 @@ class TestOther : public TestFixture { " v.clear();\n" "}\n"); ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n" // duplicate - "[test.cpp:5]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:5]: (style) Variable 'p' can be declared as pointer to const\n", // duplicate + "[test.cpp:5]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); check("void f() {\n" @@ -3931,8 +3920,7 @@ class TestOther : public TestFixture { " g(1, p);\n" " h(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 'p' can be declared as pointer to const\n" - "[test.cpp:3]: (style) Parameter 'p' can be declared as pointer to const\n", // duplicate + ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 'p' can be declared as pointer to const\n", errout.str()); check("void f(int, const int*);\n" @@ -4079,8 +4067,7 @@ class TestOther : public TestFixture { " void* p = &i;\n" " std::cout << p << '\\n';\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", // duplicate + 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 @@ -4096,10 +4083,14 @@ class TestOther : public TestFixture { " delete[] b;\n" "}\n"); ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'a1' can be declared as pointer to const\n" - "[test.cpp:1]: (style) Parameter 'a2' can be declared as pointer to const\n" - "[test.cpp:1]: (style) Parameter 'a1' can be declared as pointer to const\n" "[test.cpp:1]: (style) Parameter 'a2' can be declared as pointer to const\n", errout.str()); + + check("int f(int* p) {\n" // #11713 + " return 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p' can be declared as pointer to const\n", + errout.str()); } void switchRedundantAssignmentTest() { @@ -7789,8 +7780,7 @@ class TestOther : public TestFixture { " int end = x->first;\n" "}"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style, inconclusive) Same expression used in consecutive assignments of 'start' and 'end'.\n" - "[test.cpp:2]: (style) Parameter 'x' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Parameter 'x' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Parameter 'x' can be declared as pointer to const\n", errout.str()); check("struct SW { int first; };\n" @@ -7799,8 +7789,7 @@ class TestOther : public TestFixture { " int end = x->first;\n" "}"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style, inconclusive) Same expression used in consecutive assignments of 'start' and 'end'.\n" - "[test.cpp:2]: (style) Parameter 'x' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Parameter 'x' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Parameter 'x' can be declared as pointer to const\n", errout.str()); check("struct Foo { int f() const; };\n" @@ -8004,9 +7993,7 @@ class TestOther : public TestFixture { " if ((*p > 0)) {}\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n" // duplicate - "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n", // duplicate + "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); check("void f() {\n" @@ -8017,9 +8004,7 @@ class TestOther : public TestFixture { "}\n"); TODO_ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison '*p < 0' is always false.\n" "[test.cpp:2] -> [test.cpp:4]: (style) The comparison '*p > 0' is always false.\n", - "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n" - "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n" // duplicate - "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n", // duplicate + "[test.cpp:3]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); check("void f() {\n" @@ -8641,13 +8626,17 @@ class TestOther : public TestFixture { " char *a; a = new char[1024];\n" " delete[] (a + 10);\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p' can be declared as pointer to const\n" + "[test.cpp:3]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset.\n", + errout.str()); check("void foo(char *p) {\n" " char *a; a = new char;\n" " delete a + 10;\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p' can be declared as pointer to const\n" + "[test.cpp:3]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset.\n", + errout.str()); check("void foo(char *p) {\n" " char *a; a = new char;\n" @@ -8679,7 +8668,9 @@ class TestOther : public TestFixture { " bar()\n" " delete a + 10;\n" "}"); - ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p' can be declared as pointer to const\n" + "[test.cpp:4]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset.\n", + errout.str()); check("void foo(size_t xx) {\n" " char *ptr; ptr = malloc(42);\n" @@ -8695,9 +8686,7 @@ class TestOther : public TestFixture { " free(otherPtr - xx - 1);\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (style) Variable 'ptr' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'ptr' can be declared as pointer to const\n" // duplicate - "[test.cpp:2]: (style) Variable 'ptr' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Variable 'ptr' can be declared as pointer to const\n", errout.str()); } @@ -9276,8 +9265,7 @@ class TestOther : public TestFixture { " x = dostuff();\n" "}"); ASSERT_EQUALS( - "test.cpp:2:style:Variable 'x' can be declared as pointer to const\n" - "test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", // duplicate + "test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str()); check("void f() {\n" @@ -9286,8 +9274,7 @@ class TestOther : public TestFixture { " x = dostuff();\n" "}"); ASSERT_EQUALS( - "test.cpp:2:style:Variable 'x' can be declared as pointer to const\n" - "test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", // duplicate + "test.cpp:2:style:Variable 'x' can be declared as pointer to const\n", errout.str()); check("int foo() {\n" // #4420 @@ -9422,9 +9409,7 @@ class TestOther : public TestFixture { " }\n" "}"); ASSERT_EQUALS("test.cpp:2:style:The scope of the variable 'p' can be reduced.\n" - "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n" - "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n" // duplicate - "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", // duplicate + "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", errout.str()); check("void foo() {\n" @@ -9517,8 +9502,7 @@ class TestOther : public TestFixture { " a = p;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str()); check("void f() {\n" @@ -9527,8 +9511,7 @@ class TestOther : public TestFixture { " a = p;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n" - "[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", // duplicate + "[test.cpp:2]: (style) Variable 'a' can be declared as pointer to const\n", errout.str()); } @@ -9787,8 +9770,7 @@ class TestOther : public TestFixture { " p = dostuff();\n" "}"); ASSERT_EQUALS( - "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n" - "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", // duplicate + "test.cpp:2:style:Variable 'p' can be declared as pointer to const\n", errout.str()); // "trivial" initialization => do not warn @@ -11618,9 +11600,7 @@ class TestOther : public TestFixture { ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:4] -> [test.cpp:3] -> [test.cpp:5] -> [test.cpp:6]: (error) Comparing pointers that point to different objects\n" "[test.cpp:4]: (style) Variable 'xp' can be declared as pointer to const\n" - "[test.cpp:5]: (style) Variable 'yp' can be declared as pointer to const\n" - "[test.cpp:4]: (style) Variable 'xp' can be declared as pointer to const\n" // duplicate - "[test.cpp:5]: (style) Variable 'yp' can be declared as pointer to const\n", // duplicate + "[test.cpp:5]: (style) Variable 'yp' can be declared as pointer to const\n", errout.str()); check("bool f() {\n" @@ -11643,9 +11623,7 @@ class TestOther : public TestFixture { ASSERT_EQUALS( "[test.cpp:3] -> [test.cpp:5] -> [test.cpp:4] -> [test.cpp:6] -> [test.cpp:7]: (error) Comparing pointers that point to different objects\n" "[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" - "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n" - "[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" // duplicate - "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n", // duplicate + "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n", errout.str()); check("struct A {int data;};\n" @@ -11659,9 +11637,7 @@ class TestOther : public TestFixture { ASSERT_EQUALS( "[test.cpp:2] -> [test.cpp:3] -> [test.cpp:5] -> [test.cpp:2] -> [test.cpp:4] -> [test.cpp:6] -> [test.cpp:7]: (error) Comparing pointers that point to different objects\n" "[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" - "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n" - "[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" // duplicate - "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n", // duplicate + "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n", errout.str()); check("bool f(int * xp, int* yp) {\n" @@ -11687,9 +11663,7 @@ class TestOther : public TestFixture { " return xp > yp;\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'xp' can be declared as pointer to const\n" - "[test.cpp:4]: (style) Variable 'yp' can be declared as pointer to const\n" - "[test.cpp:3]: (style) Variable 'xp' can be declared as pointer to const\n" // duplicate - "[test.cpp:4]: (style) Variable 'yp' can be declared as pointer to const\n", // duplicate + "[test.cpp:4]: (style) Variable 'yp' can be declared as pointer to const\n", errout.str()); check("bool f(const int * xp, const int* yp) {\n" @@ -11721,9 +11695,7 @@ class TestOther : public TestFixture { " return xp > yp;\n" "}"); ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" - "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n" - "[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" // duplicate - "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n", // duplicate + "[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n", errout.str()); check("struct S { int i; };\n" // #11576 From 926cfeb9657a0f6cbb63b59f8b1c296fe33b8083 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 12:57:52 +0100 Subject: [PATCH 03/11] Add test for #12202 --- test/testother.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 12a77789a12..7284335fdb2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2581,11 +2581,6 @@ class TestOther : public TestFixture { "}"); ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'x' can be declared as reference to const\n", errout.str()); - check("int f(int& t) {\n" // #11713 - " return 0;\n" - "}\n"); - ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 't' can be declared as reference to const\n", errout.str()); - check("struct A {\n" " explicit A(int& y) : x(&y) {}\n" " int * x = nullptr;\n" @@ -3550,6 +3545,18 @@ class TestOther : public TestFixture { " c[0](5) = 12;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("int f(int& t) {\n" // #11713 + " return 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 't' can be declared as reference to const\n", errout.str()); + + check("void f(std::list& v) {\n" // #12202 + " v.remove_if([](std::string& s) {\n" + " return true;\n" + " });\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as reference to const\n", errout.str()); } void constParameterCallback() { From 5eacc4582b4de13cbb6001ff412d5cabe77d4c19 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 12:59:46 +0100 Subject: [PATCH 04/11] Fix test --- test/cfg/libcurl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/test/cfg/libcurl.c b/test/cfg/libcurl.c index 354ee549149..8c494494a03 100644 --- a/test/cfg/libcurl.c +++ b/test/cfg/libcurl.c @@ -34,6 +34,7 @@ void validCode() } } +// cppcheck-suppress constParameterPointer void ignoredReturnValue(CURL * handle) { // cppcheck-suppress ignoredReturnValue From f641c9a795e2e10b73d1daaf739522b5c35bfc51 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 13:15:23 +0100 Subject: [PATCH 05/11] Fix --- test/cfg/posix.c | 1 + test/cfg/wxwidgets.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/test/cfg/posix.c b/test/cfg/posix.c index 18400eff3c2..016081aa6c9 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -1023,6 +1023,7 @@ void nullPointer(char *p, int fd, pthread_mutex_t mutex) pthread_mutex_unlock(NULL); } +// cppcheck-suppress constParameterCallback void* f_returns_NULL(void* arg) { return NULL; diff --git a/test/cfg/wxwidgets.cpp b/test/cfg/wxwidgets.cpp index 7472fc54350..ffdf2d1f408 100644 --- a/test/cfg/wxwidgets.cpp +++ b/test/cfg/wxwidgets.cpp @@ -352,6 +352,7 @@ void deprecatedFunctions_wxDataViewCustomRenderer(wxDataViewCustomRenderer &data dataViewCustomRenderer.LeftClick(cursor, cell, model, item, col); } +// cppcheck-suppress constParameterReference void deprecatedFunctions(wxApp &a, const wxString &s, wxArtProvider *artProvider, From 724353ba6e52e73a9008b7b8393cef30ddb50b8f Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 14:05:23 +0100 Subject: [PATCH 06/11] Fix --- test/cfg/wxwidgets.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/cfg/wxwidgets.cpp b/test/cfg/wxwidgets.cpp index ffdf2d1f408..5ae82a71c74 100644 --- a/test/cfg/wxwidgets.cpp +++ b/test/cfg/wxwidgets.cpp @@ -352,11 +352,10 @@ void deprecatedFunctions_wxDataViewCustomRenderer(wxDataViewCustomRenderer &data dataViewCustomRenderer.LeftClick(cursor, cell, model, item, col); } -// cppcheck-suppress constParameterReference void deprecatedFunctions(wxApp &a, const wxString &s, wxArtProvider *artProvider, - wxCalendarCtrl &calenderCtrl, + const wxCalendarCtrl &calenderCtrl, wxComboCtrl &comboCtrl, wxChar * path) { From 743b29a2a1a2b439262ec1a0c26ffd320d8326f5 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 14:19:15 +0100 Subject: [PATCH 07/11] Format --- test/testother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testother.cpp b/test/testother.cpp index 7284335fdb2..11ceab209b3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3545,7 +3545,7 @@ class TestOther : public TestFixture { " c[0](5) = 12;\n" "}\n"); ASSERT_EQUALS("", errout.str()); - + check("int f(int& t) {\n" // #11713 " return 0;\n" "}\n"); From f61352108125c6936f3b2cd6300f9b59e5c55c42 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 14:28:07 +0100 Subject: [PATCH 08/11] Don't warn for variables with [[maybe_unused]] --- lib/checkother.cpp | 4 ++++ test/cfg/wxwidgets.cpp | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4afcab05d66..6ff2ef6b50f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1401,6 +1401,8 @@ void CheckOther::checkConstVariable() continue; if (var->isVolatile()) continue; + if (var->isMaybeUnused()) + continue; if (var->nameToken()->isExpandedMacro()) continue; if (isStructuredBindingVariable(var)) // TODO: check all bound variables @@ -1633,6 +1635,8 @@ void CheckOther::checkConstPointer() if (p->isArgument()) { if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier()) continue; + if (p->isMaybeUnused()) + continue; } if (std::find(nonConstPointers.cbegin(), nonConstPointers.cend(), p) == nonConstPointers.cend()) { const Token *start = getVariableChangedStart(p); diff --git a/test/cfg/wxwidgets.cpp b/test/cfg/wxwidgets.cpp index 5ae82a71c74..09933b10617 100644 --- a/test/cfg/wxwidgets.cpp +++ b/test/cfg/wxwidgets.cpp @@ -352,10 +352,10 @@ void deprecatedFunctions_wxDataViewCustomRenderer(wxDataViewCustomRenderer &data dataViewCustomRenderer.LeftClick(cursor, cell, model, item, col); } -void deprecatedFunctions(wxApp &a, +void deprecatedFunctions(wxApp &a [[maybe_unused]], const wxString &s, - wxArtProvider *artProvider, - const wxCalendarCtrl &calenderCtrl, + wxArtProvider *artProvider [[maybe_unused]], + wxCalendarCtrl &calenderCtrl [[maybe_unused]], wxComboCtrl &comboCtrl, wxChar * path) { From 9c347e382bd320467c85b4f9056524083d99a46a Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 5 Mar 2024 14:33:19 +0100 Subject: [PATCH 09/11] Fix --- test/cfg/wxwidgets.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/cfg/wxwidgets.cpp b/test/cfg/wxwidgets.cpp index 09933b10617..bbe799815b0 100644 --- a/test/cfg/wxwidgets.cpp +++ b/test/cfg/wxwidgets.cpp @@ -352,10 +352,10 @@ void deprecatedFunctions_wxDataViewCustomRenderer(wxDataViewCustomRenderer &data dataViewCustomRenderer.LeftClick(cursor, cell, model, item, col); } -void deprecatedFunctions(wxApp &a [[maybe_unused]], +void deprecatedFunctions([[maybe_unused]] wxApp &a, const wxString &s, - wxArtProvider *artProvider [[maybe_unused]], - wxCalendarCtrl &calenderCtrl [[maybe_unused]], + [[maybe_unused]] wxArtProvider *artProvider, + [[maybe_unused]] wxCalendarCtrl &calenderCtrl, wxComboCtrl &comboCtrl, wxChar * path) { From e0007d14691ad6c97a0cfd2675300ba28dddcc84 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 8 Mar 2024 13:14:30 +0100 Subject: [PATCH 10/11] Flatten comparisons --- lib/checkother.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 6ff2ef6b50f..3a4e80d3388 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1496,17 +1496,15 @@ namespace { bool operator()(const Variable* a, const Variable* b) const { const int fileA = a->nameToken()->fileIndex(); const int fileB = b->nameToken()->fileIndex(); - if (fileA == fileB) { - const int lineA = a->nameToken()->linenr(); - const int lineB = b->nameToken()->linenr(); - if (lineA == lineB) { - const int columnA = a->nameToken()->column(); - const int columnB = b->nameToken()->column(); - return columnA < columnB; - } + if (fileA != fileB) + return fileA < fileB; + const int lineA = a->nameToken()->linenr(); + const int lineB = b->nameToken()->linenr(); + if (lineA != lineB) return lineA < lineB; - } - return fileA < fileB; + const int columnA = a->nameToken()->column(); + const int columnB = b->nameToken()->column(); + return columnA < columnB; } }; } From e29e5c824f8baf979c32807499e487ddcd234319 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 8 Mar 2024 14:08:51 +0100 Subject: [PATCH 11/11] Format --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3a4e80d3388..6eb877a6b70 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1496,7 +1496,7 @@ namespace { bool operator()(const Variable* a, const Variable* b) const { const int fileA = a->nameToken()->fileIndex(); const int fileB = b->nameToken()->fileIndex(); - if (fileA != fileB) + if (fileA != fileB) return fileA < fileB; const int lineA = a->nameToken()->linenr(); const int lineB = b->nameToken()->linenr();