Skip to content

Commit 5caaf9c

Browse files
Fix #11420 FN returnDanglingLifetime with auto (regression) (danmar#7228)
1 parent d777511 commit 5caaf9c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/valueflow.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,11 +1792,12 @@ static bool isDifferentType(const Token* src, const Token* dst)
17921792
} else {
17931793
std::pair<const Token*, const Token*> decl = Token::typeDecl(src);
17941794
std::pair<const Token*, const Token*> parentdecl = Token::typeDecl(dst);
1795-
if (isNotEqual(decl, parentdecl))
1795+
const bool isCpp = (src && src->isCpp()) || (dst && dst->isCpp());
1796+
if (isNotEqual(decl, parentdecl) && !(isCpp && (Token::simpleMatch(decl.first, "auto") || Token::simpleMatch(parentdecl.first, "auto"))))
17961797
return true;
1797-
if (isNotEqual(decl, dst->valueType(), dst->isCpp()))
1798+
if (isNotEqual(decl, dst->valueType(), isCpp))
17981799
return true;
1799-
if (isNotEqual(parentdecl, src->valueType(), src->isCpp()))
1800+
if (isNotEqual(parentdecl, src->valueType(), isCpp))
18001801
return true;
18011802
}
18021803
return false;

test/testautovariables.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,6 +2864,13 @@ class TestAutoVariables : public TestFixture {
28642864
" std::cerr << str;\n"
28652865
"}\n");
28662866
ASSERT_EQUALS("", errout_str());
2867+
2868+
check("auto f() {\n" // #11420
2869+
" std::vector<int> x;\n"
2870+
" std::vector<int>::iterator it = x.begin();\n"
2871+
" return it;\n"
2872+
"}\n");
2873+
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());
28672874
}
28682875

28692876
void danglingLifetimeContainerView()
@@ -3083,6 +3090,14 @@ class TestAutoVariables : public TestFixture {
30833090
ASSERT_EQUALS(
30843091
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning pointer to local variable 'a' that will be invalid when returning.\n",
30853092
errout_str());
3093+
3094+
check("std::string_view f() {\n" // #10995
3095+
" char a[10]{};\n"
3096+
" return a;\n"
3097+
"}\n");
3098+
ASSERT_EQUALS(
3099+
"[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning pointer to local variable 'a' that will be invalid when returning.\n",
3100+
errout_str());
30863101
}
30873102

30883103
void danglingLifetimeUniquePtr()

0 commit comments

Comments
 (0)