Skip to content

Commit bd078ff

Browse files
committed
Fix
1 parent 932f541 commit bd078ff

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/checkautovariables.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
676676
const Token* nextTok = nextAfterAstRightmostLeaf(tok->astTop());
677677
if (!nextTok)
678678
nextTok = tok->next();
679-
if (var && (!var->isLocal() || var->isStatic()) && (!var->isArgument() || var->isReference()) && !(val.tokvalue && val.tokvalue->variable() && val.tokvalue->variable()->isStatic()) &&
679+
if (var && (!var->isLocal() || var->isStatic()) && (!var->isArgument() || (var->isReference() && isInScope(val.tokvalue, var->scope()))) &&
680+
!(val.tokvalue && val.tokvalue->variable() && val.tokvalue->variable()->isStatic()) &&
680681
!isVariableChanged(nextTok,
681682
tok->scope()->bodyEnd,
682683
var->valueType() ? var->valueType()->pointer : 0,

test/testautovariables.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,24 @@ class TestAutoVariables : public TestFixture {
29292929
" m.push_back(&x);\n"
29302930
"}\n");
29312931
ASSERT_EQUALS("[test.cpp:3:17] -> [test.cpp:3:17] -> [test.cpp:2:9] -> [test.cpp:3:5]: (error) Non-local variable 'm' will use object that points to local variable 'x'. [danglingLifetime]\n", errout_str());
2932+
2933+
check("struct P {\n"
2934+
" int h() const;\n"
2935+
" int x;\n"
2936+
" int &r;\n"
2937+
"};\n"
2938+
"int f(const P &p) {\n"
2939+
" return p.h();\n"
2940+
"}\n"
2941+
"struct C {\n"
2942+
" void g() {\n"
2943+
" int i = 1;\n"
2944+
" P q(m, i);\n"
2945+
" f(q);\n"
2946+
" }\n"
2947+
" int m;\n"
2948+
"}\n");
2949+
ASSERT_EQUALS("", errout_str());
29322950
}
29332951

29342952
void danglingLifetimeContainerView()

0 commit comments

Comments
 (0)