This commit is contained in:
parent
a52d2a23a0
commit
787da43dc9
|
@ -1403,10 +1403,10 @@ static inline bool isSameConstantValue(bool macro, const Token* tok1, const Toke
|
||||||
};
|
};
|
||||||
|
|
||||||
tok1 = adjustForCast(tok1);
|
tok1 = adjustForCast(tok1);
|
||||||
if (!tok1->isNumber())
|
if (!tok1->isNumber() && !tok1->enumerator())
|
||||||
return false;
|
return false;
|
||||||
tok2 = adjustForCast(tok2);
|
tok2 = adjustForCast(tok2);
|
||||||
if (!tok2->isNumber())
|
if (!tok2->isNumber() && !tok2->enumerator())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
|
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
|
||||||
|
@ -1523,7 +1523,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Follow variable
|
// Follow variable
|
||||||
if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId())) {
|
if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId() || tok1->enumerator() || tok2->enumerator())) {
|
||||||
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
|
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
|
||||||
if ((varTok1->str() == tok2->str()) || isSameConstantValue(macro, varTok1, tok2)) {
|
if ((varTok1->str() == tok2->str()) || isSameConstantValue(macro, varTok1, tok2)) {
|
||||||
followVariableExpressionError(tok1, varTok1, errors);
|
followVariableExpressionError(tok1, varTok1, errors);
|
||||||
|
|
|
@ -2595,10 +2595,11 @@ void CheckOther::checkDuplicateExpression()
|
||||||
if (isWithoutSideEffects(cpp, tok->astOperand1())) {
|
if (isWithoutSideEffects(cpp, tok->astOperand1())) {
|
||||||
const Token* loopTok = isInLoopCondition(tok);
|
const Token* loopTok = isInLoopCondition(tok);
|
||||||
if (!loopTok || !isExpressionChanged(tok, tok, loopTok->link()->next()->link(), mSettings, cpp)) {
|
if (!loopTok || !isExpressionChanged(tok, tok, loopTok->link()->next()->link(), mSettings, cpp)) {
|
||||||
const bool assignment = tok->str() == "=";
|
const bool isEnum = tok->scope()->type == Scope::eEnum;
|
||||||
|
const bool assignment = !isEnum && tok->str() == "=";
|
||||||
if (assignment && warningEnabled)
|
if (assignment && warningEnabled)
|
||||||
selfAssignmentError(tok, tok->astOperand1()->expressionString());
|
selfAssignmentError(tok, tok->astOperand1()->expressionString());
|
||||||
else if (styleEnabled) {
|
else if (styleEnabled && !isEnum) {
|
||||||
if (cpp && mSettings->standards.cpp >= Standards::CPP11 && tok->str() == "==") {
|
if (cpp && mSettings->standards.cpp >= Standards::CPP11 && tok->str() == "==") {
|
||||||
const Token* parent = tok->astParent();
|
const Token* parent = tok->astParent();
|
||||||
while (parent && parent->astParent()) {
|
while (parent && parent->astParent()) {
|
||||||
|
|
|
@ -175,6 +175,7 @@ private:
|
||||||
TEST_CASE(duplicateExpression14); // #9871
|
TEST_CASE(duplicateExpression14); // #9871
|
||||||
TEST_CASE(duplicateExpression15); // #10650
|
TEST_CASE(duplicateExpression15); // #10650
|
||||||
TEST_CASE(duplicateExpression16); // #10569
|
TEST_CASE(duplicateExpression16); // #10569
|
||||||
|
TEST_CASE(duplicateExpression17); // #12036
|
||||||
TEST_CASE(duplicateExpressionLoop);
|
TEST_CASE(duplicateExpressionLoop);
|
||||||
TEST_CASE(duplicateValueTernary);
|
TEST_CASE(duplicateValueTernary);
|
||||||
TEST_CASE(duplicateExpressionTernary); // #6391
|
TEST_CASE(duplicateExpressionTernary); // #6391
|
||||||
|
@ -6291,7 +6292,8 @@ private:
|
||||||
" enum { Four = 4 };\n"
|
" enum { Four = 4 };\n"
|
||||||
" if (Four == 4) {}"
|
" if (Four == 4) {}"
|
||||||
"}", nullptr, true, false);
|
"}", nullptr, true, false);
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (style) The comparison 'Four == 4' is always true.\n",
|
||||||
|
errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" enum { Four = 4 };\n"
|
" enum { Four = 4 };\n"
|
||||||
|
@ -6310,7 +6312,8 @@ private:
|
||||||
" enum { FourInEnumTwo = 4 };\n"
|
" enum { FourInEnumTwo = 4 };\n"
|
||||||
" if (FourInEnumOne == FourInEnumTwo) {}\n"
|
" if (FourInEnumOne == FourInEnumTwo) {}\n"
|
||||||
"}", nullptr, true, false);
|
"}", nullptr, true, false);
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (style) The comparison 'FourInEnumOne == FourInEnumTwo' is always true because 'FourInEnumOne' and 'FourInEnumTwo' represent the same value.\n",
|
||||||
|
errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" enum { FourInEnumOne = 4 };\n"
|
" enum { FourInEnumOne = 4 };\n"
|
||||||
|
@ -6853,6 +6856,19 @@ private:
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Same expression '!s[1]' found multiple times in chain of '||' operators.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (style) Same expression '!s[1]' found multiple times in chain of '||' operators.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void duplicateExpression17() {
|
||||||
|
check("enum { E0 };\n" // #12036
|
||||||
|
"void f() {\n"
|
||||||
|
" if (0 > E0) {}\n"
|
||||||
|
" if (E0 > 0) {}\n"
|
||||||
|
" if (E0 == 0) {}\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:3]: (style) The comparison '0 > E0' is always false.\n"
|
||||||
|
"[test.cpp:4]: (style) The comparison 'E0 > 0' is always false.\n"
|
||||||
|
"[test.cpp:5]: (style) The comparison 'E0 == 0' is always true.\n",
|
||||||
|
errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
void duplicateExpressionLoop() {
|
void duplicateExpressionLoop() {
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" int a = 1;\n"
|
" int a = 1;\n"
|
||||||
|
|
Loading…
Reference in New Issue