Skip to content

Commit

Permalink
removed some unnecessary checks not detected by tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Sep 1, 2024
1 parent a1a957d commit f7bc833
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ static const Token * getVariableInitExpression(const Variable * var)
const Token* isInLoopCondition(const Token* tok)
{
const Token* top = tok->astTop();
return top && Token::Match(top->previous(), "for|while (") ? top : nullptr;
return Token::Match(top->previous(), "for|while (") ? top : nullptr;
}

/// If tok2 comes after tok1
Expand Down
2 changes: 1 addition & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ static std::string conditionString(const Token * tok)

static bool isIfConstexpr(const Token* tok) {
const Token* const top = tok->astTop();
return top && Token::simpleMatch(top->astOperand1(), "if") && top->astOperand1()->isConstexpr();
return Token::simpleMatch(top->astOperand1(), "if") && top->astOperand1()->isConstexpr();
}

void CheckCondition::checkIncorrectLogicOperator()
Expand Down
2 changes: 0 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2307,8 +2307,6 @@ static const Token * getSingleExpressionInBlock(const Token * tok)
if (!tok)
return nullptr;
const Token * top = tok->astTop();
if (!top)
return nullptr;
const Token * nextExpression = nextAfterAstRightmostLeaf(top);
if (!Token::simpleMatch(nextExpression, "; }"))
return nullptr;
Expand Down
16 changes: 1 addition & 15 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,22 +1613,8 @@ static const Token* skipLocalVars(const Token* const tok)
if (Token::simpleMatch(tok, "{"))
return skipLocalVars(tok->next());

const Token *top = tok->astTop();
if (!top) {
const Token *semi = Token::findsimplematch(tok, ";");
if (!semi)
return tok;
if (!Token::Match(semi->previous(), "%var% ;"))
return tok;
const Token *varTok = semi->previous();
const Variable *var = varTok->variable();
if (!var)
return tok;
if (var->nameToken() != varTok)
return tok;
return skipLocalVars(semi->next());
}
if (tok->isAssignmentOp()) {
const Token *top = tok->astTop();
const Token *varTok = top->astOperand1();
const Variable *var = varTok->variable();
if (!var)
Expand Down
2 changes: 1 addition & 1 deletion lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ namespace {
return Break(Analyzer::Terminate::Bail);
} else if (tok->str() == ";" && tok->astParent()) {
Token* top = tok->astTop();
if (top && Token::Match(top->previous(), "for|while (") && Token::simpleMatch(top->link(), ") {")) {
if (Token::Match(top->previous(), "for|while (") && Token::simpleMatch(top->link(), ") {")) {
Token* endCond = top->link();
Token* endBlock = endCond->linkAt(1);
Token* condTok = getCondTok(top);
Expand Down
4 changes: 1 addition & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty)
programMemoryParseCondition(pm, tok, nullptr, *settings, b);
const Token* origin = tok;
const Token* top = tok->astTop();
if (top && Token::Match(top->previous(), "for|while|if (") && !Token::simpleMatch(tok->astParent(), "?")) {
if (Token::Match(top->previous(), "for|while|if (") && !Token::simpleMatch(tok->astParent(), "?")) {
origin = top->link()->next();
if (!b && origin->link()) {
origin = origin->link();
Expand Down Expand Up @@ -1727,8 +1727,6 @@ namespace {
return {unknown()};
for (const Token* tok = scope->bodyStart->next(); precedes(tok, scope->bodyEnd); tok = tok->next()) {
const Token* top = tok->astTop();
if (!top)
return {unknown()};

if (Token::simpleMatch(top, "return") && top->astOperand1())
return {execute(top->astOperand1())};
Expand Down
11 changes: 3 additions & 8 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ static bool isConditionKnown(const Token* tok, bool then)
while (parent && (parent->str() == op || parent->str() == "!" || parent->isCast()))
parent = parent->astParent();
const Token* top = tok->astTop();
if (top && Token::Match(top->previous(), "if|while|for ("))
if (Token::Match(top->previous(), "if|while|for ("))
return parent == top || Token::simpleMatch(parent, ";");
return parent && parent->str() != op;
}
Expand Down Expand Up @@ -2372,7 +2372,7 @@ const Token* ValueFlow::getEndOfExprScope(const Token* tok, const Scope* default
end = varEnd;

const Token* top = var->nameToken()->astTop();
if (top && Token::simpleMatch(top->tokAt(-1), "if (")) { // variable declared in if (...)
if (Token::simpleMatch(top->tokAt(-1), "if (")) { // variable declared in if (...)
const Token* elseTok = top->link()->linkAt(1);
if (Token::simpleMatch(elseTok, "} else {") && tok->scope()->isNestedIn(elseTok->tokAt(2)->scope()))
end = tok->scope()->bodyEnd;
Expand Down Expand Up @@ -4751,8 +4751,6 @@ struct ConditionHandler {
continue;

const Token* top = tok->astTop();
if (!top)
continue;

if (!Token::Match(top->previous(), "if|while|for (") && !Token::Match(tok->astParent(), "&&|%oror%|?|!"))
continue;
Expand Down Expand Up @@ -4927,8 +4925,6 @@ struct ConditionHandler {
const Settings& settings,
const std::set<const Scope*>& skippedFunctions) const {
traverseCondition(symboldatabase, settings, skippedFunctions, [&](const Condition& cond, Token* condTok, const Scope* scope) {
Token* top = condTok->astTop();

const MathLib::bigint path = cond.getPath();
const bool allowKnown = path == 0;

Expand Down Expand Up @@ -5003,8 +4999,7 @@ struct ConditionHandler {
}
}

if (!top)
return;
Token* top = condTok->astTop();

if (top->previous()->isExpandedMacro()) {
for (std::list<ValueFlow::Value>* values : {&thenValues, &elseValues}) {
Expand Down

0 comments on commit f7bc833

Please sign in to comment.