Skip to content

Commit

Permalink
Fix redundant diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jun 26, 2024
1 parent d772188 commit 6c3685e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
13 changes: 9 additions & 4 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ void CheckOther::checkRedundantAssignment()
tokenToCheck = tempToken;
}

if (start->hasKnownSymbolicValue(tokenToCheck) && Token::simpleMatch(start->astParent(), "=")) {
if (start->hasKnownSymbolicValue(tokenToCheck) && Token::simpleMatch(start->astParent(), "=") && !diag(tok)) {
redundantAssignmentSameValueError(start, tokenToCheck, tok->astOperand1()->expressionString());
}

Expand All @@ -545,8 +545,10 @@ void CheckOther::checkRedundantAssignment()
redundantAssignmentInSwitchError(tok, nextAssign, tok->astOperand1()->expressionString());
else if (isInitialization)
redundantInitializationError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
else
else {
diag(nextAssign);
redundantAssignmentError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
}
}
}
}
Expand Down Expand Up @@ -2499,7 +2501,8 @@ void CheckOther::checkDuplicateExpression()
tok->astOperand2()->expressionString() == nextAssign->astOperand2()->expressionString()) {
bool differentDomain = false;
const Scope * varScope = var1->scope() ? var1->scope() : scope;
for (const Token *assignTok = Token::findsimplematch(var2, ";"); assignTok && assignTok != varScope->bodyEnd; assignTok = assignTok->next()) {
const Token* assignTok = Token::findsimplematch(var2, ";");
for (; assignTok && assignTok != varScope->bodyEnd; assignTok = assignTok->next()) {
if (!Token::Match(assignTok, "%assign%|%comp%"))
continue;
if (!assignTok->astOperand1())
Expand Down Expand Up @@ -2530,8 +2533,10 @@ void CheckOther::checkDuplicateExpression()
}
if (!differentDomain && !isUniqueExpression(tok->astOperand2()))
duplicateAssignExpressionError(var1, var2, false);
else if (mSettings->certainty.isEnabled(Certainty::inconclusive))
else if (mSettings->certainty.isEnabled(Certainty::inconclusive)) {
diag(assignTok);
duplicateAssignExpressionError(var1, var2, true);
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "errortypes.h"
#include "tokenize.h"

#include <set>
#include <string>
#include <vector>

Expand Down Expand Up @@ -75,11 +76,11 @@ class CPPCHECKLIB CheckOther : public Check {
checkOther.warningOldStylePointerCast();
checkOther.invalidPointerCast();
checkOther.checkCharVariable();
checkOther.checkRedundantAssignment();
checkOther.redundantBitwiseOperationInSwitchError();
checkOther.checkSuspiciousCaseInSwitch();
checkOther.checkDuplicateBranch();
checkOther.checkDuplicateExpression();
checkOther.checkRedundantAssignment();
checkOther.checkUnreachableCode();
checkOther.checkSuspiciousSemicolon();
checkOther.checkVariableScope();
Expand Down Expand Up @@ -427,6 +428,9 @@ class CPPCHECKLIB CheckOther : public Check {
"- calculating modulo of one.\n"
"- known function argument, suspicious calculation.\n";
}

bool diag(const Token* tok) { return !mRedundantAssignmentDiag.emplace(tok).second; }
std::set<const Token*> mRedundantAssignmentDiag;
};
/// @}
//---------------------------------------------------------------------------
Expand Down
11 changes: 3 additions & 8 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8030,8 +8030,7 @@ class TestOther : public TestFixture {
" use(i);\n"
" i = j;\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (style) Variable 'i' is assigned an expression that holds the same value.\n"
"[test.cpp:4] -> [test.cpp:3]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'.\n",
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'.\n",
errout_str());

check("struct A { int x; int y; };"
Expand All @@ -8042,8 +8041,7 @@ class TestOther : public TestFixture {
" use(j);\n"
" j = i;\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) Variable 'j' is assigned an expression that holds the same value.\n"
"[test.cpp:4] -> [test.cpp:3]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'.\n",
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'.\n",
errout_str());

check("struct A { int x; int y; };"
Expand Down Expand Up @@ -9712,10 +9710,7 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("test.cpp:3:style:Variable 'm[key]' is reassigned a value before the old one has been used.\n"
"test.cpp:2:note:m[key] is assigned\n"
"test.cpp:3:note:m[key] is overwritten\n"
"test.cpp:3:style:Variable 'm[key]' is assigned an expression that holds the same value.\n"
"test.cpp:2:note:m[key] is assigned 'value' here.\n"
"test.cpp:3:note:Variable 'm[key]' is assigned an expression that holds the same value.\n",
"test.cpp:3:note:m[key] is overwritten\n",
errout_str());
}

Expand Down

0 comments on commit 6c3685e

Please sign in to comment.