From 6cb8f877981a491465aba2f9ffb00d6142f6f852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Jul 2021 19:57:43 +0200 Subject: [PATCH] missing return; fixed false positive for 'return {};' --- lib/checkfunctions.cpp | 4 ++++ test/testfunctions.cpp | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 2e3e0dbfb..c6744b591 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -274,6 +274,10 @@ static const Token *checkMissingReturnScope(const Token *tok) if (tok->str() == "{") return tok->next(); if (tok->str() == "}") { + for (const Token *prev = tok->link()->previous(); prev && prev->scope() == tok->scope() && !Token::Match(prev, "[;{}]"); prev = prev->previous()) { + if (prev->isKeyword() && Token::Match(prev, "return|throw")) + return nullptr; + } if (tok->scope()->type == Scope::ScopeType::eSwitch) { // find break/default bool hasDefault = false; diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 06b5f8a32..96b954e6c 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1397,6 +1397,15 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + // return {..} + check("std::pair typeDecl(int tok) {\n" + " if (!tok)\n" + " return {};\n" + " else\n" + " return {1, 2};\n" + "}"); + ASSERT_EQUALS("", errout.str()); } };