Fixed #9043 (false positive & regression: Variable '*s' is reassigned a value before the old one has been used.)

This commit is contained in:
Daniel Marjamäki 2019-04-16 19:07:26 +02:00
parent 5579ea3338
commit 253f2c9e9d
2 changed files with 14 additions and 0 deletions

View File

@ -1424,6 +1424,9 @@ FwdAnalysis::Result FwdAnalysis::check(const Token *expr, const Token *startToke
if (unknownVarId) if (unknownVarId)
return Result(FwdAnalysis::Result::Type::BAILOUT); return Result(FwdAnalysis::Result::Type::BAILOUT);
if (mWhat == What::Reassign && isGlobalData(expr))
local = false;
// In unused values checking we do not want to check assignments to // In unused values checking we do not want to check assignments to
// global data. // global data.
if (mWhat == What::UnusedValue && isGlobalData(expr)) if (mWhat == What::UnusedValue && isGlobalData(expr))

View File

@ -167,6 +167,7 @@ private:
TEST_CASE(redundantVarAssignment_lambda); TEST_CASE(redundantVarAssignment_lambda);
TEST_CASE(redundantVarAssignment_for); TEST_CASE(redundantVarAssignment_for);
TEST_CASE(redundantVarAssignment_after_switch); TEST_CASE(redundantVarAssignment_after_switch);
TEST_CASE(redundantVarAssignment_pointer);
TEST_CASE(redundantVarAssignment_pointer_parameter); TEST_CASE(redundantVarAssignment_pointer_parameter);
TEST_CASE(redundantMemWrite); TEST_CASE(redundantMemWrite);
@ -6130,6 +6131,16 @@ private:
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n", errout.str());
} }
void redundantVarAssignment_pointer() {
check("void f(int *ptr) {\n"
" int *x = ptr + 1;\n"
" *x = 23;\n"
" foo(ptr);\n"
" *x = 32;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void redundantVarAssignment_pointer_parameter() { void redundantVarAssignment_pointer_parameter() {
check("void f(int *p) {\n" check("void f(int *p) {\n"
" *p = 1;\n" " *p = 1;\n"