From 52f507d1fb71418eae989a9d199b697cee9aebf7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 13 May 2022 06:54:02 +0200 Subject: [PATCH] Fix #8004 unintentional semicolon in expression '.. ; +dostuff();' (#4109) * Fix #8004 unintentional semicolon in expression '.. ; +dostuff();' * Improve error message * Don't remove single unary + in front of variables --- lib/checkother.cpp | 2 +- lib/tokenize.cpp | 2 +- test/testincompletestatement.cpp | 11 +++++----- test/testother.cpp | 36 +++++++++++++++++++++++++------- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d2a9cf7f4..25cba3ef4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1920,7 +1920,7 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, if (Token::simpleMatch(tok, "==")) msg = "Found suspicious equality comparison. Did you intend to assign a value instead?"; else if (Token::Match(tok, ",|!|~|%cop%")) - msg = "Found suspicious operator '" + tok->str() + "'"; + msg = "Found suspicious operator '" + tok->str() + "', result is not used."; else if (Token::Match(tok, "%var%")) msg = "Unused variable value '" + tok->str() + "'"; else if (Token::Match(valueTok, "%str%|%num%|%bool%|%char%")) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f0cd14080..1cfd6e6a6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2948,7 +2948,7 @@ void Tokenizer::concatenateNegativeNumberAndAnyPositive() if (!Token::Match(tok, "?|:|,|(|[|{|return|case|sizeof|%op% +|-") || tok->tokType() == Token::eIncDecOp) continue; - while (tok->str() != ">" && tok->next() && tok->next()->str() == "+") + while (tok->str() != ">" && tok->next() && tok->next()->str() == "+" && (!Token::Match(tok->tokAt(2), "%name% (|;") || Token::Match(tok, "%op%"))) tok->deleteNext(); if (Token::Match(tok->next(), "- %num%")) { diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index eefe58854..ab9bd7deb 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -363,7 +363,7 @@ private: "void f(int value) {\n" " foo(42,\"test\",42),(value&42);\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (warning) Found suspicious operator ','\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (warning) Found suspicious operator ',', result is not used.\n", errout.str()); } void commaoperator2() { @@ -430,10 +430,11 @@ private: "[test.cpp:3]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[test.cpp:4]: (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:7]: (warning, inconclusive) Found suspicious operator '!'\n" + "[test.cpp:6]: (warning, inconclusive) Found suspicious operator '!', result is not used.\n" + "[test.cpp:7]: (warning, inconclusive) Found suspicious operator '!', result is not used.\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 '~', result is not used.\n", + errout.str()); check("void f1(int x) { x; }", true); ASSERT_EQUALS("[test.cpp:1]: (warning) Unused variable value 'x'\n", errout.str()); @@ -686,7 +687,7 @@ private: check("void f(int ar) {\n" " ar & x;\n" "}", true); - ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '&'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '&', result is not used.\n", errout.str()); } void ast() { diff --git a/test/testother.cpp b/test/testother.cpp index c9bb1a14e..a5fef1852 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -117,6 +117,7 @@ private: TEST_CASE(suspiciousCase); TEST_CASE(suspiciousEqualityComparison); + TEST_CASE(suspiciousUnaryPlusMinus); // #8004 TEST_CASE(selfAssignment); TEST_CASE(trac1132); @@ -4374,6 +4375,25 @@ private: ASSERT_EQUALS("", errout.str()); } + void suspiciousUnaryPlusMinus() { // #8004 + check("int g() { return 1; }\n" + "void f() {\n" + " +g();\n" + " -g();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n" + "[test.cpp:4]: (warning, inconclusive) Found suspicious operator '-', result is not used.\n", + errout.str()); + + check("void f(int i) {\n" + " +i;\n" + " -i;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n" + "[test.cpp:3]: (warning, inconclusive) Found suspicious operator '-', result is not used.\n", + errout.str()); + } + void selfAssignment() { check("void foo()\n" "{\n" @@ -4839,7 +4859,7 @@ private: " return c;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -4848,7 +4868,7 @@ private: " return *c;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -4856,7 +4876,7 @@ private: " *f.a++;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -4864,7 +4884,7 @@ private: " *f.a[5].v[3]++;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -4872,7 +4892,7 @@ private: " *f.a(1, 5).v[x + y]++;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -4892,7 +4912,7 @@ private: " return c;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -4901,7 +4921,7 @@ private: " return **c;\n" "}"); ASSERT_EQUALS( - "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*'\n" + "[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n" "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n", errout.str()); @@ -7147,7 +7167,7 @@ private: checkP("void f(int a, int b) {\n" " a > b;\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '>'\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '>', result is not used.\n", errout.str()); checkP("void f() {\n" // #10607 " for (auto p : m)\n"