Skip to content

Commit cc24161

Browse files
chrchr-githubweb-flowdanmar
authored
Fix #13671 duplicated unreadVariable with assignment in variable declaration (danmar#8139)
Co-authored-by: chrchr-github <noreply@github.com> Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
1 parent 792a761 commit cc24161

File tree

3 files changed

+46
-86
lines changed

3 files changed

+46
-86
lines changed

lib/checkunusedvar.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
11981198
const Token *nextStructuredBindingTok = nullptr;
11991199
std::vector<std::pair<const Token*, const Token*>> unusedStructuredBindingTokens;
12001200
size_t structuredBindingTokCount = 0;
1201+
std::set<const Variable*> diagUnreadVariable; // prevent duplicate warnings
12011202

12021203
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
12031204
if (nextStructuredBindingTok) {
@@ -1366,8 +1367,10 @@ void CheckUnusedVar::checkFunctionVariableUsage()
13661367
if (!expr->variable() || !expr->variable()->isMaybeUnused()) {
13671368
if (structuredBindingTokCount > 0)
13681369
unusedStructuredBindingTokens.emplace_back(tok, expr);
1369-
else
1370+
else {
13701371
unreadVariableError(tok, expr->expressionString(), false);
1372+
diagUnreadVariable.emplace(expr->variable());
1373+
}
13711374
}
13721375
}
13731376
}
@@ -1420,7 +1423,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
14201423
!(var->valueType() && var->valueType()->container) &&
14211424
!(var->isStatic() && isReturnedByRef(var, scope->function)))
14221425
unassignedVariableError(usage._var->nameToken(), varname);
1423-
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) {
1426+
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var && diagUnreadVariable.count(usage._var) == 0) {
14241427
const Token* vnt = var->nameToken();
14251428
bool error = false;
14261429
if (vnt->next()->isSplittedVarDeclEq() || (!var->isReference() && vnt->strAt(1) == "=")) {

samples/unreadVariable/out.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
samples\unreadVariable\bad.cpp:5:34: style: Variable 's2' is assigned a value that is never used. [unreadVariable]
22
std::string s1 = "test1", s2 = "test2";
33
^
4-
samples\unreadVariable\bad.cpp:5:31: style: Variable 's2' is assigned a value that is never used. [unreadVariable]
5-
std::string s1 = "test1", s2 = "test2";
6-
^

0 commit comments

Comments
 (0)