Fixed #9929 (False positive: suspicious semicolon after macro)

This commit is contained in:
Daniel Marjamäki 2020-10-09 20:34:05 +02:00
parent 047c3ed6ba
commit f1ce5a9101
2 changed files with 20 additions and 10 deletions

View File

@ -262,8 +262,9 @@ void CheckOther::checkSuspiciousSemicolon()
// Ensure the semicolon is at the same line number as the if/for/while statement
// and the {..} block follows it without an extra empty line.
if (Token::simpleMatch(scope.bodyStart, "{ ; } {") &&
scope.bodyStart->previous()->linenr() == scope.bodyStart->tokAt(2)->linenr()
&& scope.bodyStart->linenr()+1 >= scope.bodyStart->tokAt(3)->linenr()) {
scope.bodyStart->previous()->linenr() == scope.bodyStart->tokAt(2)->linenr() &&
scope.bodyStart->linenr()+1 >= scope.bodyStart->tokAt(3)->linenr() &&
!scope.bodyStart->tokAt(3)->isExpandedMacro()) {
suspiciousSemicolonError(scope.classDef);
}
}
@ -273,7 +274,7 @@ void CheckOther::checkSuspiciousSemicolon()
void CheckOther::suspiciousSemicolonError(const Token* tok)
{
reportError(tok, Severity::warning, "suspiciousSemicolon",
"Suspicious use of ; at the end of '" + (tok ? tok->str() : std::string()) + "' statement.", CWE398, true);
"Suspicious use of ; at the end of '" + (tok ? tok->str() : std::string()) + "' statement.", CWE398, false);
}

View File

@ -161,8 +161,9 @@ private:
TEST_CASE(checkSignOfUnsignedVariable);
TEST_CASE(checkSignOfPointer);
TEST_CASE(checkForSuspiciousSemicolon1);
TEST_CASE(checkForSuspiciousSemicolon2);
TEST_CASE(checkSuspiciousSemicolon1);
TEST_CASE(checkSuspiciousSemicolon2);
TEST_CASE(checkSuspiciousSemicolon3);
TEST_CASE(checkInvalidFree);
@ -6312,7 +6313,7 @@ private:
ASSERT_EQUALS("", errout.str());
}
void checkForSuspiciousSemicolon1() {
void checkSuspiciousSemicolon1() {
check("void foo() {\n"
" for(int i = 0; i < 10; ++i);\n"
"}");
@ -6323,23 +6324,23 @@ private:
" for(int i = 0; i < 10; ++i); {\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'for' statement.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious use of ; at the end of 'for' statement.\n", errout.str());
check("void foo() {\n"
" while (!quit); {\n"
" do_something();\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'while' statement.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious use of ; at the end of 'while' statement.\n", errout.str());
}
void checkForSuspiciousSemicolon2() {
void checkSuspiciousSemicolon2() {
check("void foo() {\n"
" if (i == 1); {\n"
" do_something();\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if' statement.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious use of ; at the end of 'if' statement.\n", errout.str());
// Seen this in the wild
check("void foo() {\n"
@ -6374,6 +6375,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
void checkSuspiciousSemicolon3() {
checkP("#define REQUIRE(code) {code}\n"
"void foo() {\n"
" if (x == 123);\n"
" REQUIRE(y=z);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void checkInvalidFree() {
check("void foo(char *p) {\n"