From 93b4de36cd48a47806d3e4ad8d96c4cb396045c7 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 7 Apr 2023 13:14:53 -0500 Subject: [PATCH] Update constVariable IDs for references and pointers (#4904) --- lib/checkother.cpp | 8 ++++++++ releasenotes.txt | 10 +++++++++- test/cfg/std.cpp | 2 +- test/fixture.h | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 78b557dfb..b559682db 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1628,6 +1628,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; } @@ -1644,6 +1648,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/releasenotes.txt b/releasenotes.txt index 0b97397ac..e9eaafa6b 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` + diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index f29c40234..66b86264b 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 diff --git a/test/fixture.h b/test/fixture.h index 690959f98..47fc5e20e 100644 --- a/test/fixture.h +++ b/test/fixture.h @@ -114,7 +114,7 @@ protected: 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; }