Fix autoNoType with multiple auto variables (#5012)

This commit is contained in:
chrchr-github 2023-04-30 20:36:27 +02:00 committed by GitHub
parent 2bc4ee925e
commit 269850a62d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View File

@ -2286,6 +2286,8 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
varTok = varTok->next(); varTok = varTok->next();
while (Token::Match(varTok, "%name% ::")) while (Token::Match(varTok, "%name% ::"))
varTok = varTok->tokAt(2); varTok = varTok->tokAt(2);
if (Token::simpleMatch(varTok, "(") && Token::simpleMatch(varTok->astOperand1(), "."))
varTok = varTok->astOperand1()->astOperand1();
std::pair<const Token*, const Token*> r = typeDecl(varTok); std::pair<const Token*, const Token*> r = typeDecl(varTok);
if (r.first) if (r.first)
return r; return r;

View File

@ -2274,7 +2274,7 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("auto f() {\n" check("std::vector<int>::iterator f() {\n"
" std::vector<int> x;\n" " std::vector<int> x;\n"
" auto it = x.begin();\n" " auto it = x.begin();\n"
" return it;\n" " return it;\n"
@ -2288,7 +2288,7 @@ private:
"}"); "}");
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()); 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());
check("auto f() {\n" check("int* f() {\n"
" std::vector<int> x;\n" " std::vector<int> x;\n"
" auto p = x.data();\n" " auto p = x.data();\n"
" return p;\n" " return p;\n"
@ -2315,7 +2315,7 @@ private:
"[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", "[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",
errout.str()); errout.str());
check("auto f(std::vector<int> x) {\n" check("std::vector<int>::iterator f(std::vector<int> x) {\n"
" auto it = x.begin();\n" " auto it = x.begin();\n"
" return it;\n" " return it;\n"
"}"); "}");
@ -2447,11 +2447,12 @@ private:
errout.str()); errout.str());
check("std::vector<int> g();\n" check("std::vector<int> g();\n"
"auto f() {\n" "std::vector<int>::iterator f() {\n"
" auto it = g().begin();\n" " auto it = g().begin();\n"
" return it;\n" " return it;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Returning iterator that will be invalid when returning.\n", ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3] -> [test.cpp:4]: (error) Using iterator that is a temporary.\n"
"[test.cpp:3] -> [test.cpp:4]: (error) Returning iterator that will be invalid when returning.\n",
errout.str()); errout.str());
check("std::vector<int> g();\n" check("std::vector<int> g();\n"

View File

@ -8451,6 +8451,19 @@ private:
ASSERT_EQUALS(3, tok->variable()->valueType()->constness); ASSERT_EQUALS(3, tok->variable()->valueType()->constness);
ASSERT_EQUALS(1, tok->variable()->valueType()->pointer); ASSERT_EQUALS(1, tok->variable()->valueType()->pointer);
} }
{
GET_SYMBOL_DB("std::string f(const std::string& s) {\n"
" auto t = s.substr(3, 7);\n"
" auto r = t.substr(1, 2);\n"
" return r;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
const Token* tok = tokenizer.tokens();
tok = Token::findsimplematch(tok, "auto r");
ASSERT(tok && tok->valueType());
ASSERT_EQUALS("container(std :: string|wstring|u16string|u32string)", tok->valueType()->str());
}
} }
void valueTypeThis() { void valueTypeThis() {