Fix #10928, #10930 FP constStatement (#3946)

* Fix #10928, #10930 FP constStatement

* Fix test cases (first one did not compile)
This commit is contained in:
chrchr-github 2022-03-27 07:59:29 +02:00 committed by GitHub
parent 401f0de18b
commit 63d96e49fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 6 deletions

View File

@ -1748,7 +1748,7 @@ bool isConstExpression(const Token *tok, const Library& library, bool pure, bool
return isConstExpression(tok->astOperand1(), library, pure, cpp) && isConstExpression(tok->astOperand2(), library, pure, cpp);
}
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess)
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess, bool checkReference)
{
if (!cpp)
return true;
@ -1757,7 +1757,7 @@ bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess)
tok = tok->astOperand2();
if (tok && tok->varId()) {
const Variable* var = tok->variable();
return var && (!var->isClass() || var->isPointer() || (checkArrayAccess ? var->isStlType() && !var->isStlType(CheckClass::stl_containers_not_const) : var->isStlType()));
return var && ((!var->isClass() && (checkReference || !var->isReference())) || var->isPointer() || (checkArrayAccess ? var->isStlType() && !var->isStlType(CheckClass::stl_containers_not_const) : var->isStlType()));
}
return true;
}

View File

@ -227,7 +227,7 @@ bool isConstFunctionCall(const Token* ftok, const Library& library);
bool isConstExpression(const Token *tok, const Library& library, bool pure, bool cpp);
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess = false);
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess = false, bool checkReference = true);
bool isUniqueExpression(const Token* tok);

View File

@ -1771,6 +1771,8 @@ static bool isConstStatement(const Token *tok, bool cpp)
return false;
if (tok->astTop() && Token::simpleMatch(tok->astTop()->astOperand1(), "delete"))
return false;
if (Token::Match(tok, "&&|%oror%"))
return isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2(), cpp);
if (Token::Match(tok, "!|~|%cop%") && (tok->astOperand1() || tok->astOperand2()))
return true;
if (Token::simpleMatch(tok->previous(), "sizeof ("))
@ -1785,7 +1787,7 @@ static bool isConstStatement(const Token *tok, bool cpp)
return tok->astParent() ? isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2(), cpp) : isConstStatement(tok->astOperand2(), cpp);
if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) // ternary operator
return isConstStatement(tok->astOperand1(), cpp) && isConstStatement(tok->astOperand2()->astOperand1(), cpp) && isConstStatement(tok->astOperand2()->astOperand2(), cpp);
if (isBracketAccess(tok) && isWithoutSideEffects(cpp, tok->astOperand1(), /*checkArrayAccess*/ true)) {
if (isBracketAccess(tok) && isWithoutSideEffects(cpp, tok->astOperand1(), /*checkArrayAccess*/ true, /*checkReference*/ false)) {
if (Token::simpleMatch(tok->astParent(), "["))
return isConstStatement(tok->astOperand2(), cpp) && isConstStatement(tok->astParent(), cpp);
return isConstStatement(tok->astOperand2(), cpp);

View File

@ -517,7 +517,7 @@ private:
"[test.cpp:12]: (warning) Redundant code: Found unused array access.\n",
errout.str());
check("void g() {\n"
check("void g(std::map<std::string, std::string>& map) {\n"
" int j[2]{};\n"
" int k[2] = {};\n"
" int l[]{ 1, 2 };\n"
@ -531,6 +531,7 @@ private:
" j[0][0][h()];\n"
" std::map<std::string, int> M;\n"
" M[\"abc\"];\n"
" map[\"abc\"];\n" // #10928
" std::auto_ptr<Int> app[4];" // #10919
"}\n");
ASSERT_EQUALS("", errout.str());
@ -608,6 +609,13 @@ private:
" params_given (params, \"overrides\") || (overrides = \"1\");\n"
"}", true);
ASSERT_EQUALS("", errout.str());
check("void f(std::ifstream& file) {\n" // #10930
" int a{}, b{};\n"
" (file >> a) || (file >> b);\n"
" (file >> a) && (file >> b);\n"
"}\n", true);
ASSERT_EQUALS("", errout.str());
}
};

View File

@ -4396,7 +4396,7 @@ private:
" {\n"
" x = y = z = 0.0;\n"
" }\n"
" V( double x, const double y, const double &z )\n"
" V( double x, const double y_, const double &z_)\n"
" {\n"
" x = x; y = y; z = z;\n"
" }\n"