Skip to content

Commit 449ee24

Browse files
Fix #13944 FN constParameterPointer in method in derived class (#8673)
#8240 seems to have stalled. Co-authored-by: ceJce <noreply@github.com> --------- Co-authored-by: chrchr-github <noreply@github.com>
1 parent 9162aa7 commit 449ee24

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

lib/checkother.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,8 +2033,11 @@ void CheckOtherImpl::checkConstPointer()
20332033
nonConstPointers.emplace(var);
20342034
}
20352035
for (const Variable *p: pointers) {
2036+
bool foundAllBaseClasses = true;
20362037
if (p->isArgument()) {
2037-
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier())
2038+
if (!p->scope() || !p->scope()->function || p->scope()->function->hasVirtualSpecifier())
2039+
continue;
2040+
if (p->scope()->function->isImplicitlyVirtual(true, &foundAllBaseClasses) && foundAllBaseClasses)
20382041
continue;
20392042
if (p->isMaybeUnused())
20402043
continue;
@@ -2051,12 +2054,12 @@ void CheckOtherImpl::checkConstPointer()
20512054
continue;
20522055
if (p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
20532056
continue;
2054-
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
2057+
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr, foundAllBaseClasses);
20552058
}
20562059
}
20572060
}
20582061

2059-
void CheckOtherImpl::constVariableError(const Variable *var, const Function *function)
2062+
void CheckOtherImpl::constVariableError(const Variable *var, const Function *function, bool foundAllBaseClasses)
20602063
{
20612064
if (!var) {
20622065
reportError(nullptr, Severity::style, "constParameter", "Parameter 'x' can be declared with const");
@@ -2069,13 +2072,18 @@ void CheckOtherImpl::constVariableError(const Variable *var, const Function *fun
20692072
return;
20702073
}
20712074

2072-
const std::string vartype(var->isArgument() ? "Parameter" : "Variable");
2075+
std::string vartype(var->isArgument() ? "Parameter" : "Variable");
20732076
const std::string& varname(var->name());
20742077
const std::string ptrRefArray = var->isArray() ? "const array" : (var->isPointer() ? "pointer to const" : "reference to const");
20752078

20762079
ErrorPath errorPath;
20772080
std::string id = "const" + vartype;
2078-
std::string message = "$symbol:" + varname + "\n" + vartype + " '$symbol' can be declared as " + ptrRefArray;
2081+
std::string message = "$symbol:" + varname + "\n";
2082+
if (!foundAllBaseClasses) {
2083+
message += "Either there is a missing override/final keyword, or the ";
2084+
vartype[0] = std::tolower(vartype[0]);
2085+
}
2086+
message += vartype + " '$symbol' can be declared as " + ptrRefArray;
20792087
errorPath.emplace_back(var->nameToken(), message);
20802088
if (var->isArgument() && function && function->functionPointerUsage) {
20812089
errorPath.emplace_front(function->functionPointerUsage, "You might need to cast the function pointer here");

lib/checkother.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class CPPCHECKLIB CheckOtherImpl : public CheckImpl {
275275
void suspiciousFloatingPointCastError(const Token *tok);
276276
void invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to, bool inconclusive, bool toIsInt);
277277
void passedByValueError(const Variable* var, bool inconclusive, bool isRangeBasedFor = false);
278-
void constVariableError(const Variable *var, const Function *function);
278+
void constVariableError(const Variable *var, const Function *function, bool foundAllBaseClasses = true);
279279
void constStatementError(const Token *tok, const std::string &type, bool inconclusive);
280280
void signedCharArrayIndexError(const Token *tok);
281281
void unknownSignCharArrayIndexError(const Token *tok);

test/testother.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4914,6 +4914,18 @@ class TestOther : public TestFixture {
49144914
ASSERT_EQUALS("[test.cpp:2:14]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n"
49154915
"[test.cpp:2:46]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
49164916
errout_str());
4917+
4918+
check("struct S : U {\n" // #13944
4919+
" void f(int* p) const {\n"
4920+
" if (m == p) {}\n"
4921+
" }\n"
4922+
" void g(int* p) final {\n"
4923+
" if (m == p) {}\n"
4924+
" }\n"
4925+
" int* m;\n"
4926+
"};\n");
4927+
ASSERT_EQUALS("[test.cpp:2:17]: (style) Either there is a missing override/final keyword, or the parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
4928+
errout_str());
49174929
}
49184930

49194931
void constArray() {

0 commit comments

Comments
 (0)