From 3df170c191e75b047751ce024adb111f0aae2fec Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 12 Mar 2022 14:52:18 +0100 Subject: [PATCH] Fix #10865 FN: constStatementError, streamline error messages (#3892) --- lib/checkother.cpp | 6 +++++- test/testincompletestatement.cpp | 23 ++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 690094586..4f0ffd413 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1766,6 +1766,8 @@ static bool isConstStatement(const Token *tok, bool cpp) return isConstStatement(tok->astOperand1(), cpp); if (Token::simpleMatch(tok, ",")) 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; } @@ -1879,9 +1881,11 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, else if (!tok) msg = "Redundant code: Found a statement that begins with " + type + " constant."; 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."; } + else if (tok->str() == "?" && tok->tokType() == Token::Type::eExtendedOp) + msg = "Redundant code: Found unused result of ternary operator."; else { reportError(tok, Severity::debug, "debug", "constStatementError not handled."); return; diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index a6f1ab3de..c16b713a2 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -375,7 +375,7 @@ private: "[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: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()); check("void f1(int x) { x; }", true); @@ -385,7 +385,7 @@ private: ASSERT_EQUALS("", errout.str()); check("void f(int x) { static_cast(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" " static_cast(x);\n" @@ -403,9 +403,9 @@ private: " static_cast((char)i);\n" " (char)static_cast(i);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Found unused cast of expression 'i'.\n" - "[test.cpp:3]: (warning) Found unused cast of expression 'i'.\n" - "[test.cpp:4]: (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) Redundant code: Found unused cast of expression 'i'.\n" + "[test.cpp:4]: (warning) Redundant code: Found unused cast of expression 'i'.\n", errout.str()); check("struct S; struct T; struct U;\n" @@ -416,6 +416,19 @@ private: " static_cast((U)t);\n" "}\n"); 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() {