Skip to content

Commit b57120c

Browse files
committed
Fix #13944 FN constParameterPointer in method in derived class
1 parent 49f7cd2 commit b57120c

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
@@ -2030,8 +2030,11 @@ void CheckOtherImpl::checkConstPointer()
20302030
nonConstPointers.emplace(var);
20312031
}
20322032
for (const Variable *p: pointers) {
2033+
bool foundAllBaseClasses = true;
20332034
if (p->isArgument()) {
2034-
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier())
2035+
if (!p->scope() || !p->scope()->function || p->scope()->function->hasVirtualSpecifier())
2036+
continue;
2037+
if (p->scope()->function->isImplicitlyVirtual(true, &foundAllBaseClasses) && foundAllBaseClasses)
20352038
continue;
20362039
if (p->isMaybeUnused())
20372040
continue;
@@ -2048,12 +2051,12 @@ void CheckOtherImpl::checkConstPointer()
20482051
continue;
20492052
if (p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
20502053
continue;
2051-
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
2054+
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr, foundAllBaseClasses);
20522055
}
20532056
}
20542057
}
20552058

2056-
void CheckOtherImpl::constVariableError(const Variable *var, const Function *function)
2059+
void CheckOtherImpl::constVariableError(const Variable *var, const Function *function, bool foundAllBaseClasses)
20572060
{
20582061
if (!var) {
20592062
reportError(nullptr, Severity::style, "constParameter", "Parameter 'x' can be declared with const");
@@ -2066,13 +2069,18 @@ void CheckOtherImpl::constVariableError(const Variable *var, const Function *fun
20662069
return;
20672070
}
20682071

2069-
const std::string vartype(var->isArgument() ? "Parameter" : "Variable");
2072+
std::string vartype(var->isArgument() ? "Parameter" : "Variable");
20702073
const std::string& varname(var->name());
20712074
const std::string ptrRefArray = var->isArray() ? "const array" : (var->isPointer() ? "pointer to const" : "reference to const");
20722075

20732076
ErrorPath errorPath;
20742077
std::string id = "const" + vartype;
2075-
std::string message = "$symbol:" + varname + "\n" + vartype + " '$symbol' can be declared as " + ptrRefArray;
2078+
std::string message = "$symbol:" + varname + "\n";
2079+
if (!foundAllBaseClasses) {
2080+
message += "Either there is a missing override/final keyword, or the ";
2081+
vartype[0] = std::tolower(vartype[0]);
2082+
}
2083+
message += vartype + " '$symbol' can be declared as " + ptrRefArray;
20762084
errorPath.emplace_back(var->nameToken(), message);
20772085
if (var->isArgument() && function && function->functionPointerUsage) {
20782086
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
@@ -4872,6 +4872,18 @@ class TestOther : public TestFixture {
48724872
" return *p->cbegin();\n"
48734873
"}\n");
48744874
ASSERT_EQUALS("[test.cpp:1:25]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());
4875+
4876+
check("struct S : U {\n"
4877+
"void f(int* p) const {\n"
4878+
" if (m == p) {}\n"
4879+
"}\n"
4880+
"void g(int* p) final {\n"
4881+
" if (m == p) {}\n"
4882+
"}\n"
4883+
"int* m;\n"
4884+
"};\n");
4885+
ASSERT_EQUALS("[test.cpp:2:13]: (style) Either there is a missing override/final keyword, or the parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
4886+
errout_str());
48754887
}
48764888

48774889
void constArray() {

0 commit comments

Comments
 (0)