Remove a false positive - main function has no return value (#3335)

This commit is contained in:
Pavel Šimovec 2021-07-16 18:50:16 +02:00 committed by GitHub
parent 942202aede
commit c71e2da5a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -257,6 +257,8 @@ void CheckFunctions::checkMissingReturn()
const Function *function = scope->function; const Function *function = scope->function;
if (!function || !function->hasBody()) if (!function || !function->hasBody())
continue; continue;
if (function->name() == "main" && !(mSettings->standards.c < Standards::C99 && mTokenizer->isC()))
continue;
if (function->type != Function::Type::eFunction && function->type != Function::Type::eOperatorEqual) if (function->type != Function::Type::eFunction && function->type != Function::Type::eOperatorEqual)
continue; continue;
if (Token::Match(function->retDef, "%name% (") && function->retDef->isUpperCaseName()) if (Token::Match(function->retDef, "%name% (") && function->retDef->isUpperCaseName())

View File

@ -1373,6 +1373,21 @@ private:
check("int f() {}"); check("int f() {}");
ASSERT_EQUALS("[test.cpp:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str());
{
const char code[] = "int main(void) {}";
Settings s;
s.standards.c = Standards::C89;
check(code, "test.c", &s); // c code (c89)
ASSERT_EQUALS("[test.c:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n", errout.str());
s.standards.c = Standards::C99; check(code, "test.c", &s); // c code (c99)
ASSERT_EQUALS("", errout.str());
check(code, "test.cpp", &s); // c++ code
ASSERT_EQUALS("", errout.str());
}
check("F(A,B) { x=1; }"); check("F(A,B) { x=1; }");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());