diff --git a/src/tokenize.cpp b/src/tokenize.cpp index dd9b4fe3b..0d6fe3c4f 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -519,6 +519,9 @@ void Tokenizer::setVarId() if (Token::Match(tok, "[;{}(] %any%")) tok = tok->next(); + if (Token::Match(tok, "else|return")) + continue; + if (!(firstMatch = Token::Match(tok, "%type% *| %var%")) && !Token::Match(tok, "%type% %type% *| %var%")) continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 61ace53aa..456eac129 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -99,6 +99,7 @@ private: TEST_CASE(varid4); TEST_CASE(varid5); // TODO TEST_CASE(varid6); // Function parameters aren't handled well yet + TEST_CASE(varidReturn); TEST_CASE(file1); TEST_CASE(file2); @@ -876,6 +877,32 @@ private: ASSERT_EQUALS(expected, actual); } + void varidReturn() + { + const std::string code("int f()\n" + "{\n" + " int a;\n" + " return a;\n" + "}\n"); + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.simplifyTokenList(); + + // result.. + const std::string actual(tokenizer.tokens()->stringifyList(true)); + const std::string expected("\n" + "1: int f ( )\n" + "2: {\n" + "3: int a@1 ;\n" + "4: return a@1 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } +