Skip to content

Commit

Permalink
Fix FN constParameterReference
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 5, 2024
1 parent 14833a4 commit 4da68b4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
16 changes: 0 additions & 16 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4da68b4

Please sign in to comment.