Skip to content

Commit 1015d71

Browse files
authored
fix #12302: internalAstError using lambda (danmar#7589)
Fixes the case `case (int)1:`.
1 parent d0c44c2 commit 1015d71

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/tokenlist.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ static bool iscast(const Token *tok, bool cpp)
470470
if (Token::Match(tok->link(), ") %assign%|,|..."))
471471
return false;
472472

473-
if (tok->previous() && tok->previous()->isName() && tok->strAt(-1) != "return" &&
473+
if (tok->previous() && tok->previous()->isName() && !Token::Match(tok->previous(), "return|case") &&
474474
(!cpp || !Token::Match(tok->previous(), "delete|throw")))
475475
return false;
476476

@@ -600,6 +600,10 @@ static bool iscpp11init_impl(const Token * const tok)
600600
if (Token::simpleMatch(castTok->astParent(), "case"))
601601
return true;
602602
}
603+
if (findParent(colonTok->previous(), [](const Token *parent){
604+
return parent->str() == "case";
605+
}))
606+
return true;
603607
const Token* caseTok = colonTok->tokAt(-2);
604608
while (caseTok && Token::Match(caseTok->tokAt(-1), "::|%name%"))
605609
caseTok = caseTok->tokAt(-1);

test/testtokenize.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7071,6 +7071,23 @@ class TestTokenizer : public TestFixture {
70717071
"}\n"));
70727072
ASSERT_EQUALS("", errout_str());
70737073

7074+
ASSERT_NO_THROW(tokenizeAndStringify("int foo() {\n"
7075+
" connect([]( const int& f ) {\n"
7076+
" switch( f )\n"
7077+
" {\n"
7078+
" case (int)1:\n"
7079+
" {\n"
7080+
" A r(f);\n"
7081+
" if (1) {}\n"
7082+
" }\n"
7083+
" break;\n"
7084+
" }\n"
7085+
" return 0;\n"
7086+
" });\n"
7087+
" return 0;\n"
7088+
"}\n"));
7089+
ASSERT_EQUALS("", errout_str());
7090+
70747091
// #11378
70757092
ASSERT_EQUALS("gT{(&[{= 0return", testAst("auto g = T{ [&]() noexcept -> int { return 0; } };"));
70767093

0 commit comments

Comments
 (0)