Fix #10865 FN: constStatementError, streamline error messages (#3892)

This commit is contained in:
chrchr-github 2022-03-12 14:52:18 +01:00 committed by GitHub
parent d1f740a289
commit 3df170c191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -1766,6 +1766,8 @@ static bool isConstStatement(const Token *tok, bool cpp)
return isConstStatement(tok->astOperand1(), cpp); return isConstStatement(tok->astOperand1(), cpp);
if (Token::simpleMatch(tok, ",")) if (Token::simpleMatch(tok, ","))
return isConstStatement(tok->astOperand2(), cpp); return 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);
return false; return false;
} }
@ -1879,9 +1881,11 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type,
else if (!tok) else if (!tok)
msg = "Redundant code: Found a statement that begins with " + type + " constant."; msg = "Redundant code: Found a statement that begins with " + type + " constant.";
else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp) { else if (tok->isCast() && tok->tokType() == Token::Type::eExtendedOp) {
msg = "Found unused cast "; msg = "Redundant code: Found unused cast ";
msg += valueTok ? "of expression '" + valueTok->expressionString() + "'." : "expression."; msg += valueTok ? "of expression '" + valueTok->expressionString() + "'." : "expression.";
} }
else if (tok->str() == "?" && tok->tokType() == Token::Type::eExtendedOp)
msg = "Redundant code: Found unused result of ternary operator.";
else { else {
reportError(tok, Severity::debug, "debug", "constStatementError not handled."); reportError(tok, Severity::debug, "debug", "constStatementError not handled.");
return; return;

View File

@ -375,7 +375,7 @@ private:
"[test.cpp:5]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:5]: (warning) Redundant code: Found a statement that begins with numeric constant.\n"
"[test.cpp:6]: (warning, inconclusive) Found suspicious operator '!'\n" "[test.cpp:6]: (warning, inconclusive) Found suspicious operator '!'\n"
"[test.cpp:7]: (warning, inconclusive) Found suspicious operator '!'\n" "[test.cpp:7]: (warning, inconclusive) Found suspicious operator '!'\n"
"[test.cpp:8]: (warning) Found unused cast of expression '!x'.\n" "[test.cpp:8]: (warning) Redundant code: Found unused cast of expression '!x'.\n"
"[test.cpp:9]: (warning, inconclusive) Found suspicious operator '~'\n", errout.str()); "[test.cpp:9]: (warning, inconclusive) Found suspicious operator '~'\n", errout.str());
check("void f1(int x) { x; }", true); check("void f1(int x) { x; }", true);
@ -385,7 +385,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f(int x) { static_cast<unsigned>(x); }"); check("void f(int x) { static_cast<unsigned>(x); }");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found unused cast of expression 'x'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant code: Found unused cast of expression 'x'.\n", errout.str());
check("void f(int x, int* p) {\n" check("void f(int x, int* p) {\n"
" static_cast<void>(x);\n" " static_cast<void>(x);\n"
@ -403,9 +403,9 @@ private:
" static_cast<float>((char)i);\n" " static_cast<float>((char)i);\n"
" (char)static_cast<float>(i);\n" " (char)static_cast<float>(i);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Found unused cast of expression 'i'.\n" ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant code: Found unused cast of expression 'i'.\n"
"[test.cpp:3]: (warning) Found unused cast of expression 'i'.\n" "[test.cpp:3]: (warning) Redundant code: Found unused cast of expression 'i'.\n"
"[test.cpp:4]: (warning) Found unused cast of expression 'i'.\n", "[test.cpp:4]: (warning) Redundant code: Found unused cast of expression 'i'.\n",
errout.str()); errout.str());
check("struct S; struct T; struct U;\n" check("struct S; struct T; struct U;\n"
@ -416,6 +416,19 @@ private:
" static_cast<S>((U)t);\n" " static_cast<S>((U)t);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f(bool b) { b ? true : false; }\n"); // #10865
ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant code: Found unused result of ternary operator.\n", errout.str());
check("void f(bool b) {\n"
" g() ? true : false;\n"
" true ? g() : false;\n"
" false ? true : g();\n"
" g(b ? true : false, 1);\n"
" C c{ b ? true : false, 1 };\n"
" b = (b ? true : false);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void vardecl() { void vardecl() {