diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1d5214cf7..9da474b1c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8044,6 +8044,8 @@ void Tokenizer::findGarbageCode() const if (!isCPP() || mSettings->standards.cpp < Standards::CPP20 || !Token::Match(tok->previous(), "%name% : %num% =")) syntaxError(tok, tok->next()->str() + " " + tok->strAt(2)); } + else if (Token::simpleMatch(tok, ") return") && !Token::Match(tok->link()->previous(), "if|while|for (")) + syntaxError(tok); if (tok->isControlFlowKeyword() && Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... } if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 52b960ed7..fbd036a1c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6782,6 +6782,8 @@ private: ASSERT_THROW_EQUALS(tokenizeAndStringify("enum : { };"), InternalError, "syntax error: Unexpected token '{'"); ASSERT_THROW_EQUALS(tokenizeAndStringify("enum : 3 { };"), InternalError, "syntax error: Unexpected token '3'"); + + ASSERT_THROW_EQUALS(tokenizeAndStringify("int a() { b((c)return 0) }"), InternalError, "syntax error"); }