Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1792,11 +1792,12 @@ static bool isDifferentType(const Token* src, const Token* dst)
} else {
std::pair<const Token*, const Token*> decl = Token::typeDecl(src);
std::pair<const Token*, const Token*> parentdecl = Token::typeDecl(dst);
if (isNotEqual(decl, parentdecl))
const bool isCpp = (src && src->isCpp()) || (dst && dst->isCpp());
if (isNotEqual(decl, parentdecl) && !(isCpp && (Token::simpleMatch(decl.first, "auto") || Token::simpleMatch(parentdecl.first, "auto"))))
return true;
if (isNotEqual(decl, dst->valueType(), dst->isCpp()))
if (isNotEqual(decl, dst->valueType(), isCpp))
return true;
if (isNotEqual(parentdecl, src->valueType(), src->isCpp()))
if (isNotEqual(parentdecl, src->valueType(), isCpp))
return true;
}
return false;
Expand Down
15 changes: 15 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2864,6 +2864,13 @@ class TestAutoVariables : public TestFixture {
" std::cerr << str;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("auto f() {\n" // #11420
" std::vector<int> x;\n"
" std::vector<int>::iterator it = x.begin();\n"
" return it;\n"
"}\n");
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());
}

void danglingLifetimeContainerView()
Expand Down Expand Up @@ -3083,6 +3090,14 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning pointer to local variable 'a' that will be invalid when returning.\n",
errout_str());

check("std::string_view f() {\n" // #10995
" char a[10]{};\n"
" return a;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning pointer to local variable 'a' that will be invalid when returning.\n",
errout_str());
}

void danglingLifetimeUniquePtr()
Expand Down
Loading