From c71e2da5a87dbd24f3af953541317cd355548985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0imovec?= Date: Fri, 16 Jul 2021 18:50:16 +0200 Subject: [PATCH] Remove a false positive - main function has no return value (#3335) --- lib/checkfunctions.cpp | 2 ++ test/testfunctions.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index d10e2b6e1..831103759 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -257,6 +257,8 @@ void CheckFunctions::checkMissingReturn() const Function *function = scope->function; if (!function || !function->hasBody()) continue; + if (function->name() == "main" && !(mSettings->standards.c < Standards::C99 && mTokenizer->isC())) + continue; if (function->type != Function::Type::eFunction && function->type != Function::Type::eOperatorEqual) continue; if (Token::Match(function->retDef, "%name% (") && function->retDef->isUpperCaseName()) diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 3230829f6..61d51e8fb 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1373,6 +1373,21 @@ private: 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()); + { + 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; }"); ASSERT_EQUALS("", errout.str());