Enable followVar for duplicate ternary expressions (#1406)
This commit is contained in:
parent
b3fef7957a
commit
4598995564
|
@ -2030,8 +2030,8 @@ void CheckOther::checkDuplicateExpression()
|
||||||
} else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
|
} else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
|
||||||
if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()))
|
if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()))
|
||||||
duplicateValueTernaryError(tok);
|
duplicateValueTernaryError(tok);
|
||||||
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, false))
|
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath))
|
||||||
duplicateExpressionTernaryError(tok);
|
duplicateExpressionTernaryError(tok, errorPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2086,9 +2086,10 @@ void CheckOther::duplicateAssignExpressionError(const Token *tok1, const Token *
|
||||||
"determine if it is correct.", CWE398, false);
|
"determine if it is correct.", CWE398, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckOther::duplicateExpressionTernaryError(const Token *tok)
|
void CheckOther::duplicateExpressionTernaryError(const Token *tok, ErrorPath errors)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n"
|
errors.emplace_back(tok, "");
|
||||||
|
reportError(errors, Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n"
|
||||||
"Finding the same expression in both branches of ternary operator is suspicious as "
|
"Finding the same expression in both branches of ternary operator is suspicious as "
|
||||||
"the same code is executed regardless of the condition.", CWE398, false);
|
"the same code is executed regardless of the condition.", CWE398, false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,7 +242,7 @@ private:
|
||||||
void oppositeExpressionError(const Token *tok1, const Token *tok2, const std::string &op, ErrorPath errors);
|
void oppositeExpressionError(const Token *tok1, const Token *tok2, const std::string &op, ErrorPath errors);
|
||||||
void duplicateExpressionError(const Token *tok1, const Token *tok2, const Token *opTok, ErrorPath errors);
|
void duplicateExpressionError(const Token *tok1, const Token *tok2, const Token *opTok, ErrorPath errors);
|
||||||
void duplicateValueTernaryError(const Token *tok);
|
void duplicateValueTernaryError(const Token *tok);
|
||||||
void duplicateExpressionTernaryError(const Token *tok);
|
void duplicateExpressionTernaryError(const Token *tok, ErrorPath errors);
|
||||||
void duplicateBreakError(const Token *tok, bool inconclusive);
|
void duplicateBreakError(const Token *tok, bool inconclusive);
|
||||||
void unreachableCodeError(const Token* tok, bool inconclusive);
|
void unreachableCodeError(const Token* tok, bool inconclusive);
|
||||||
void unsignedLessThanZeroError(const Token *tok, const std::string &varname, bool inconclusive);
|
void unsignedLessThanZeroError(const Token *tok, const std::string &varname, bool inconclusive);
|
||||||
|
@ -305,7 +305,7 @@ private:
|
||||||
c.oppositeExpressionError(nullptr, nullptr, "&&", errorPath);
|
c.oppositeExpressionError(nullptr, nullptr, "&&", errorPath);
|
||||||
c.duplicateExpressionError(nullptr, nullptr, nullptr, errorPath);
|
c.duplicateExpressionError(nullptr, nullptr, nullptr, errorPath);
|
||||||
c.duplicateValueTernaryError(nullptr);
|
c.duplicateValueTernaryError(nullptr);
|
||||||
c.duplicateExpressionTernaryError(nullptr);
|
c.duplicateExpressionTernaryError(nullptr, errorPath);
|
||||||
c.duplicateBreakError(nullptr, false);
|
c.duplicateBreakError(nullptr, false);
|
||||||
c.unreachableCodeError(nullptr, false);
|
c.unreachableCodeError(nullptr, false);
|
||||||
c.unsignedLessThanZeroError(nullptr, "varname", false);
|
c.unsignedLessThanZeroError(nullptr, "varname", false);
|
||||||
|
|
|
@ -4162,6 +4162,12 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) Same expression in both branches of ternary operator.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (style) Same expression in both branches of ternary operator.\n", errout.str());
|
||||||
|
|
||||||
|
check("int f(bool b, int a) {\n"
|
||||||
|
" const int c = a;\n"
|
||||||
|
" return b ? a : c;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Same expression in both branches of ternary operator.\n", errout.str());
|
||||||
|
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" return A ? x : z;\n"
|
" return A ? x : z;\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
Loading…
Reference in New Issue