Skip to content

Commit 60f22bd

Browse files
committed
Fixed cppcheck-opensource#7477 (False positive 'Assigned value is never used' in multithreaded context)
1 parent b97bdb5 commit 60f22bd

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10701070
variables.use(tok->next()->varId(), tok); // use = read + write
10711071
} else if (Token::Match(tok, "[(,] %var% [,)]") && tok->previous()->str() != "*") {
10721072
variables.use(tok->next()->varId(), tok); // use = read + write
1073+
} else if (Token::Match(tok, "[(,] & %var% [,)]")) {
1074+
variables.eraseAll(tok->tokAt(2)->varId());
10731075
} else if (Token::Match(tok, "[(,] (") &&
10741076
Token::Match(tok->next()->link(), ") %var% [,)]")) {
10751077
variables.use(tok->next()->link()->next()->varId(), tok); // use = read + write
@@ -1083,6 +1085,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10831085
// function
10841086
else if (Token::Match(tok, "%var% (")) {
10851087
variables.read(tok->varId(), tok);
1088+
} else if (Token::Match(tok, "std :: ref ( %var% )")) {
1089+
variables.eraseAll(tok->tokAt(4)->varId());
10861090
}
10871091

10881092
else if (Token::Match(tok->previous(), "[{,] %var% [,}]")) {

test/testunusedvar.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class TestUnusedVar : public TestFixture {
161161
TEST_CASE(localvarAssignInWhile);
162162
TEST_CASE(localvarTemplate); // #4955 - variable is used as template parameter
163163
TEST_CASE(localvarFuncPtr); // #7194
164+
TEST_CASE(localvarAddr); // #7477
164165

165166
TEST_CASE(localvarCppInitialization);
166167
TEST_CASE(localvarCpp11Initialization);
@@ -3971,6 +3972,22 @@ class TestUnusedVar : public TestFixture {
39713972
ASSERT_EQUALS("", errout.str());
39723973
}
39733974

3975+
void localvarAddr() { // #7747
3976+
functionVariableUsage("void f() {\n"
3977+
" int x = 0;\n"
3978+
" dostuff(&x);\n"
3979+
" x = 1;\n"
3980+
"}");
3981+
ASSERT_EQUALS("", errout.str());
3982+
3983+
functionVariableUsage("void f() {\n"
3984+
" int x = 0;\n"
3985+
" dostuff(std::ref(x));\n"
3986+
" x = 1;\n"
3987+
"}");
3988+
ASSERT_EQUALS("", errout.str());
3989+
}
3990+
39743991
void chainedAssignment() {
39753992
// #5466
39763993
functionVariableUsage("void NotUsed(double* pdD, int n) {\n"

0 commit comments

Comments
 (0)