Enable followVar for duplicate ternary expressions (#1406)

This commit is contained in:
Paul Fultz II 2018-10-01 07:31:06 -05:00 committed by Daniel Marjamäki
parent b3fef7957a
commit 4598995564
3 changed files with 13 additions and 6 deletions

View File

@ -2030,8 +2030,8 @@ void CheckOther::checkDuplicateExpression()
} 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()))
duplicateValueTernaryError(tok);
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, false))
duplicateExpressionTernaryError(tok);
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath))
duplicateExpressionTernaryError(tok, errorPath);
}
}
}
@ -2086,9 +2086,10 @@ void CheckOther::duplicateAssignExpressionError(const Token *tok1, const Token *
"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 "
"the same code is executed regardless of the condition.", CWE398, false);
}

View File

@ -242,7 +242,7 @@ private:
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 duplicateValueTernaryError(const Token *tok);
void duplicateExpressionTernaryError(const Token *tok);
void duplicateExpressionTernaryError(const Token *tok, ErrorPath errors);
void duplicateBreakError(const Token *tok, bool inconclusive);
void unreachableCodeError(const Token* tok, bool inconclusive);
void unsignedLessThanZeroError(const Token *tok, const std::string &varname, bool inconclusive);
@ -305,7 +305,7 @@ private:
c.oppositeExpressionError(nullptr, nullptr, "&&", errorPath);
c.duplicateExpressionError(nullptr, nullptr, nullptr, errorPath);
c.duplicateValueTernaryError(nullptr);
c.duplicateExpressionTernaryError(nullptr);
c.duplicateExpressionTernaryError(nullptr, errorPath);
c.duplicateBreakError(nullptr, false);
c.unreachableCodeError(nullptr, false);
c.unsignedLessThanZeroError(nullptr, "varname", false);

View File

@ -4162,6 +4162,12 @@ private:
"}");
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"
" return A ? x : z;\n"
"}");