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
This commit is contained in:
chrchr-github 2022-05-13 06:54:02 +02:00 committed by GitHub
parent 1f9929c65f
commit 52f507d1fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 15 deletions

View File

@ -1920,7 +1920,7 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type,
if (Token::simpleMatch(tok, "==")) if (Token::simpleMatch(tok, "=="))
msg = "Found suspicious equality comparison. Did you intend to assign a value instead?"; msg = "Found suspicious equality comparison. Did you intend to assign a value instead?";
else if (Token::Match(tok, ",|!|~|%cop%")) 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%")) else if (Token::Match(tok, "%var%"))
msg = "Unused variable value '" + tok->str() + "'"; msg = "Unused variable value '" + tok->str() + "'";
else if (Token::Match(valueTok, "%str%|%num%|%bool%|%char%")) { else if (Token::Match(valueTok, "%str%|%num%|%bool%|%char%")) {

View File

@ -2948,7 +2948,7 @@ void Tokenizer::concatenateNegativeNumberAndAnyPositive()
if (!Token::Match(tok, "?|:|,|(|[|{|return|case|sizeof|%op% +|-") || tok->tokType() == Token::eIncDecOp) if (!Token::Match(tok, "?|:|,|(|[|{|return|case|sizeof|%op% +|-") || tok->tokType() == Token::eIncDecOp)
continue; 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(); tok->deleteNext();
if (Token::Match(tok->next(), "- %num%")) { if (Token::Match(tok->next(), "- %num%")) {

View File

@ -363,7 +363,7 @@ private:
"void f(int value) {\n" "void f(int value) {\n"
" foo(42,\"test\",42),(value&42);\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() { void commaoperator2() {
@ -430,10 +430,11 @@ private:
"[test.cpp:3]: (warning) Redundant code: Found a statement that begins with numeric constant.\n" "[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: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: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 '!', result is not used.\n"
"[test.cpp:7]: (warning, inconclusive) Found suspicious operator '!'\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: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); check("void f1(int x) { x; }", true);
ASSERT_EQUALS("[test.cpp:1]: (warning) Unused variable value 'x'\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (warning) Unused variable value 'x'\n", errout.str());
@ -686,7 +687,7 @@ private:
check("void f(int ar) {\n" check("void f(int ar) {\n"
" ar & x;\n" " ar & x;\n"
"}", true); "}", 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() { void ast() {

View File

@ -117,6 +117,7 @@ private:
TEST_CASE(suspiciousCase); TEST_CASE(suspiciousCase);
TEST_CASE(suspiciousEqualityComparison); TEST_CASE(suspiciousEqualityComparison);
TEST_CASE(suspiciousUnaryPlusMinus); // #8004
TEST_CASE(selfAssignment); TEST_CASE(selfAssignment);
TEST_CASE(trac1132); TEST_CASE(trac1132);
@ -4374,6 +4375,25 @@ private:
ASSERT_EQUALS("", errout.str()); 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() { void selfAssignment() {
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
@ -4839,7 +4859,7 @@ private:
" return c;\n" " return c;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -4848,7 +4868,7 @@ private:
" return *c;\n" " return *c;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -4856,7 +4876,7 @@ private:
" *f.a++;\n" " *f.a++;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -4864,7 +4884,7 @@ private:
" *f.a[5].v[3]++;\n" " *f.a[5].v[3]++;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -4872,7 +4892,7 @@ private:
" *f.a(1, 5).v[x + y]++;\n" " *f.a(1, 5).v[x + y]++;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -4892,7 +4912,7 @@ private:
" return c;\n" " return c;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -4901,7 +4921,7 @@ private:
" return **c;\n" " return **c;\n"
"}"); "}");
ASSERT_EQUALS( 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", "[test.cpp:2]: (warning) In expression like '*A++' the result of '*' is unused. Did you intend to write '(*A)++;'?\n",
errout.str()); errout.str());
@ -7147,7 +7167,7 @@ private:
checkP("void f(int a, int b) {\n" checkP("void f(int a, int b) {\n"
" a > 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 checkP("void f() {\n" // #10607
" for (auto p : m)\n" " for (auto p : m)\n"