From 78ef8d4742f4efc2cc32da1c84b2177b96b44c62 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 20 Mar 2023 17:12:35 -0500 Subject: [PATCH 1/4] Update constVariable IDs for references and pointers --- lib/checkother.cpp | 8 ++++++++ test/cfg/std.cpp | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 6c232cb94d6..bb265b16909 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1629,6 +1629,10 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio if (!var) { reportError(nullptr, Severity::style, "constParameter", "Parameter 'x' can be declared with const"); reportError(nullptr, Severity::style, "constVariable", "Variable 'x' can be declared with const"); + reportError(nullptr, Severity::style, "constParameterReference", "Parameter 'x' can be declared with const"); + reportError(nullptr, Severity::style, "constVariableReference", "Variable 'x' can be declared with const"); + reportError(nullptr, Severity::style, "constParameterPointer", "Parameter 'x' can be declared with const"); + reportError(nullptr, Severity::style, "constVariablePointer", "Variable 'x' can be declared with const"); reportError(nullptr, Severity::style, "constParameterCallback", "Parameter 'x' can be declared with const, however it seems that 'f' is a callback function."); return; } @@ -1645,6 +1649,10 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio errorPath.emplace_front(function->functionPointerUsage, "You might need to cast the function pointer here"); id += "Callback"; message += ". However it seems that '" + function->name() + "' is a callback function, if '$symbol' is declared with const you might also need to cast function pointer(s)."; + } else if (var->isReference()) { + id += "Reference"; + } else if (var->isPointer() && !var->isArray()) { + id += "Pointer"; } reportError(errorPath, Severity::style, id.c_str(), message, CWE398, Certainty::normal); diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index f29c4023458..66b86264b87 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4236,7 +4236,7 @@ void ignoredReturnValue_string_compare(std::string teststr, std::wstring testwst testwstr.compare(L"wtest"); } -// cppcheck-suppress constParameter +// cppcheck-suppress constParameterReference void ignoredReturnValue_container_access(std::string& s, std::string_view& sv, std::vector& v) { // cppcheck-suppress ignoredReturnValue From 3b1fe5a9536ef7ce7f470f718848e05689bcb11d Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 20 Mar 2023 17:15:53 -0500 Subject: [PATCH 2/4] Format --- lib/checkother.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bb265b16909..151ba6b0638 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1630,9 +1630,9 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio reportError(nullptr, Severity::style, "constParameter", "Parameter 'x' can be declared with const"); reportError(nullptr, Severity::style, "constVariable", "Variable 'x' can be declared with const"); reportError(nullptr, Severity::style, "constParameterReference", "Parameter 'x' can be declared with const"); - reportError(nullptr, Severity::style, "constVariableReference", "Variable 'x' can be declared with const"); + reportError(nullptr, Severity::style, "constVariableReference", "Variable 'x' can be declared with const"); reportError(nullptr, Severity::style, "constParameterPointer", "Parameter 'x' can be declared with const"); - reportError(nullptr, Severity::style, "constVariablePointer", "Variable 'x' can be declared with const"); + reportError(nullptr, Severity::style, "constVariablePointer", "Variable 'x' can be declared with const"); reportError(nullptr, Severity::style, "constParameterCallback", "Parameter 'x' can be declared with const, however it seems that 'f' is a callback function."); return; } From 1fe28e4fff39847716516bcf8d97e484ad325904 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 21 Mar 2023 08:59:11 -0500 Subject: [PATCH 3/4] Fix suppression --- test/fixture.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixture.h b/test/fixture.h index 818c0e6bd2f..2eb9edac497 100644 --- a/test/fixture.h +++ b/test/fixture.h @@ -114,7 +114,7 @@ class TestFixture : public ErrorLogger { static T& getCheck() { for (Check *check : Check::instances()) { - //cppcheck-suppress [constVariable, useStlAlgorithm] - TODO: fix constVariable FP + //cppcheck-suppress [constVariablePointer, useStlAlgorithm] - TODO: fix constVariable FP if (T* c = dynamic_cast(check)) return *c; } From 17d29107b1aaf873c3536336772a54ec75a6b7db Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 21 Mar 2023 09:03:14 -0500 Subject: [PATCH 4/4] Update release notes --- releasenotes.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/releasenotes.txt b/releasenotes.txt index 0b97397acaa..e9eaafa6bc3 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -4,4 +4,12 @@ release notes for cppcheck-2.11 - It is no longer necessary to run "--check-config" to get detailed "missingInclude" and "missingIncludeSystem" messages. They will always be issued in the regular analysis if "missingInclude" is enabled. - "missingInclude" and "missingIncludeSystem" are reported with "-j" is > 1 and processes are used in the backend (default in non-Windows binaries) - "missingInclude" and "missingIncludeSystem" will now cause the "--error-exitcode" to be applied -- "--enable=information" will no longer implicitly enable "missingInclude" starting with 2.16. Please enable it explicitly if you require it. \ No newline at end of file +- "--enable=information" will no longer implicitly enable "missingInclude" starting with 2.16. Please enable it explicitly if you require it. +- The `constParameter` and `constVariable` checks have been split into 3 different IDs based on if the variable is a pointer, a reference, or local. The different IDs will allow users to suppress different const warning based on variable type. + - `constParameter` + - `constParameterReference` + - `constParameterPointer` + - `constVariable` + - `constVariableReference` + - `constVariablePointer` +