Fix false positive in constArgument when passing struct member (#1845)

This commit is contained in:
Paul Fultz II 2019-05-21 03:41:16 -05:00 committed by Daniel Marjamäki
parent 9949ae1b4f
commit 9838bfa79f
2 changed files with 20 additions and 1 deletions

View File

@ -2820,6 +2820,16 @@ void CheckOther::shadowError(const Token *var, const Token *shadowed, bool shado
reportError(errorPath, Severity::style, id, message, CWE398, false);
}
static bool isVariableExpression(const Token* tok)
{
if (Token::Match(tok, "%var%"))
return true;
if (Token::simpleMatch(tok, "."))
return isVariableExpression(tok->astOperand1()) &&
isVariableExpression(tok->astOperand2());
return false;
}
void CheckOther::checkConstArgument()
{
if (!mSettings->isEnabled(Settings::STYLE))
@ -2844,7 +2854,7 @@ void CheckOther::checkConstArgument()
const Token * tok2 = tok;
if (isCPPCast(tok2))
tok2 = tok2->astOperand2();
if (Token::Match(tok2, "%var%"))
if (isVariableExpression(tok2))
continue;
constArgumentError(tok, tok->astParent()->previous(), &tok->values().front());
}

View File

@ -7637,6 +7637,15 @@ private:
" f(x[0]);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct A { int x; };"
"void g(int);\n"
"void f(int x) {\n"
" A y;\n"
" y.x = 1;\n"
" g(y.x);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkComparePointers() {