Skip to content

Commit fe7d0ee

Browse files
Fix #10919 FP: constStatement with template type (cppcheck-opensource#3941)
1 parent 8d7fe70 commit fe7d0ee

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/checkother.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,20 @@ static bool isVarDeclOp(const Token* tok)
17401740
return isType(typetok, Token::Match(vartok, "%var%"));
17411741
}
17421742

1743+
static bool isBracketAccess(const Token* tok)
1744+
{
1745+
if (!Token::simpleMatch(tok, "[") || !tok->astOperand1())
1746+
return false;
1747+
tok = tok->astOperand1();
1748+
if (tok->str() == ".")
1749+
tok = tok->astOperand2();
1750+
while (Token::simpleMatch(tok, "["))
1751+
tok = tok->astOperand1();
1752+
if (!tok || !tok->variable())
1753+
return false;
1754+
return tok->variable()->nameToken() != tok;
1755+
}
1756+
17431757
static bool isConstStatement(const Token *tok, bool cpp)
17441758
{
17451759
if (!tok)
@@ -1769,7 +1783,7 @@ static bool isConstStatement(const Token *tok, bool cpp)
17691783
return tok->astParent() ? isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2(), cpp) : isConstStatement(tok->astOperand2(), cpp);
17701784
if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) // ternary operator
17711785
return isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2()->astOperand1(), cpp) && isConstStatement(tok->astOperand2()->astOperand2(), cpp);
1772-
if (Token::simpleMatch(tok, "[") && !Token::Match(tok->tokAt(-2), "%type% %name%") && isWithoutSideEffects(cpp, tok->astOperand1(), /*checkArrayAccess*/ true)) {
1786+
if (isBracketAccess(tok) && isWithoutSideEffects(cpp, tok->astOperand1(), /*checkArrayAccess*/ true)) {
17731787
if (Token::simpleMatch(tok->astParent(), "["))
17741788
return isConstStatement(tok->astOperand2(), cpp) && isConstStatement(tok->astParent(), cpp);
17751789
return isConstStatement(tok->astOperand2(), cpp);

test/testincompletestatement.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ class TestIncompleteStatement : public TestFixture {
531531
" j[0][0][h()];\n"
532532
" std::map<std::string, int> M;\n"
533533
" M[\"abc\"];\n"
534+
" std::auto_ptr<Int> app[4];" // #10919
534535
"}\n");
535536
ASSERT_EQUALS("", errout.str());
536537

0 commit comments

Comments
 (0)