Skip to content

Commit 269850a

Browse files
Fix autoNoType with multiple auto variables (#5012)
1 parent 2bc4ee9 commit 269850a

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

lib/token.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,8 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
22862286
varTok = varTok->next();
22872287
while (Token::Match(varTok, "%name% ::"))
22882288
varTok = varTok->tokAt(2);
2289+
if (Token::simpleMatch(varTok, "(") && Token::simpleMatch(varTok->astOperand1(), "."))
2290+
varTok = varTok->astOperand1()->astOperand1();
22892291
std::pair<const Token*, const Token*> r = typeDecl(varTok);
22902292
if (r.first)
22912293
return r;

test/testautovariables.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,7 @@ class TestAutoVariables : public TestFixture {
22742274
"}");
22752275
ASSERT_EQUALS("", errout.str());
22762276

2277-
check("auto f() {\n"
2277+
check("std::vector<int>::iterator f() {\n"
22782278
" std::vector<int> x;\n"
22792279
" auto it = x.begin();\n"
22802280
" return it;\n"
@@ -2288,7 +2288,7 @@ class TestAutoVariables : public TestFixture {
22882288
"}");
22892289
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2] -> [test.cpp:4]: (error) Returning iterator to local container 'x' that will be invalid when returning.\n", errout.str());
22902290

2291-
check("auto f() {\n"
2291+
check("int* f() {\n"
22922292
" std::vector<int> x;\n"
22932293
" auto p = x.data();\n"
22942294
" return p;\n"
@@ -2315,7 +2315,7 @@ class TestAutoVariables : public TestFixture {
23152315
"[test.cpp:3] -> [test.cpp:4] -> [test.cpp:2] -> [test.cpp:4]: (error) Returning pointer to local variable 'v' that will be invalid when returning.\n",
23162316
errout.str());
23172317

2318-
check("auto f(std::vector<int> x) {\n"
2318+
check("std::vector<int>::iterator f(std::vector<int> x) {\n"
23192319
" auto it = x.begin();\n"
23202320
" return it;\n"
23212321
"}");
@@ -2447,11 +2447,12 @@ class TestAutoVariables : public TestFixture {
24472447
errout.str());
24482448

24492449
check("std::vector<int> g();\n"
2450-
"auto f() {\n"
2450+
"std::vector<int>::iterator f() {\n"
24512451
" auto it = g().begin();\n"
24522452
" return it;\n"
24532453
"}");
2454-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Returning iterator that will be invalid when returning.\n",
2454+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3] -> [test.cpp:4]: (error) Using iterator that is a temporary.\n"
2455+
"[test.cpp:3] -> [test.cpp:4]: (error) Returning iterator that will be invalid when returning.\n",
24552456
errout.str());
24562457

24572458
check("std::vector<int> g();\n"

test/testsymboldatabase.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8451,6 +8451,19 @@ class TestSymbolDatabase : public TestFixture {
84518451
ASSERT_EQUALS(3, tok->variable()->valueType()->constness);
84528452
ASSERT_EQUALS(1, tok->variable()->valueType()->pointer);
84538453
}
8454+
{
8455+
GET_SYMBOL_DB("std::string f(const std::string& s) {\n"
8456+
" auto t = s.substr(3, 7);\n"
8457+
" auto r = t.substr(1, 2);\n"
8458+
" return r;\n"
8459+
"}\n");
8460+
ASSERT_EQUALS("", errout.str());
8461+
8462+
const Token* tok = tokenizer.tokens();
8463+
tok = Token::findsimplematch(tok, "auto r");
8464+
ASSERT(tok && tok->valueType());
8465+
ASSERT_EQUALS("container(std :: string|wstring|u16string|u32string)", tok->valueType()->str());
8466+
}
84548467
}
84558468

84568469
void valueTypeThis() {

0 commit comments

Comments
 (0)