MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / alwaysTrueFalse

Method alwaysTrueFalse

lib/checkcondition.cpp:1517–1665  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1515}
1516
1517void CheckConditionImpl::alwaysTrueFalse()
1518{
1519 const bool pedantic = mSettings.isPremiumEnabled("alwaysTrue") ||
1520 mSettings.isPremiumEnabled("alwaysFalse") ||
1521 mSettings.isPremiumEnabled("knownConditionTrueFalse");
1522
1523 if (!pedantic && !mSettings.severity.isEnabled(Severity::style))
1524 return;
1525
1526 logChecker("CheckCondition::alwaysTrueFalse"); // style
1527
1528 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
1529 for (const Scope * scope : symbolDatabase->functionScopes) {
1530 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
1531 // don't write false positives when templates are used or inside of asserts or non-evaluated contexts
1532 if (tok->link() && (Token::simpleMatch(tok, "<") ||
1533 Token::Match(tok->previous(), "static_assert|assert|ASSERT|sizeof|decltype ("))) {
1534 tok = tok->link();
1535 continue;
1536 }
1537 if (!tok->hasKnownIntValue())
1538 continue;
1539 const Token* condition = nullptr;
1540 {
1541 // is this a condition..
1542 const Token *parent = tok->astParent();
1543 bool hasComp = false;
1544 while (Token::Match(parent, "%oror%|&&")) {
1545 hasComp = true;
1546 parent = parent->astParent();
1547 }
1548 if (!parent)
1549 continue;
1550 if (parent->str() == "?" && precedes(tok, parent))
1551 condition = parent;
1552 else if (Token::Match(parent->previous(), "if|while ("))
1553 condition = parent->previous();
1554 else if (Token::simpleMatch(parent, "return"))
1555 condition = parent;
1556 else if (parent->str() == ";" && parent->astParent() && parent->astParent()->astParent() &&
1557 Token::simpleMatch(parent->astParent()->astParent()->previous(), "for ("))
1558 condition = parent->astParent()->astParent()->previous();
1559 else if (Token::Match(tok, "%comp%"))
1560 condition = tok;
1561 else if ((tok->str() == "(" || (hasComp && Token::Match(tok, "!|%var%"))) && astIsBool(parent) && Token::Match(parent, "%assign%"))
1562 condition = tok;
1563 else
1564 continue;
1565
1566 }
1567 // Skip already diagnosed values
1568 if (diag(tok, false))
1569 continue;
1570 if (condition->isConstexpr())
1571 continue;
1572 if (!isUsedAsBool(tok, mSettings))
1573 continue;
1574 if (Token::simpleMatch(condition, "return") && Token::Match(tok, "%assign%"))

Callers 1

runChecksMethod · 0.80

Calls 15

precedesFunction · 0.85
astIsBoolFunction · 0.85
isUsedAsBoolFunction · 0.85
isWithoutSideEffectsFunction · 0.85
isSameExpressionFunction · 0.85
isConstVarExpressionFunction · 0.85
visitAstNodesFunction · 0.85
isPremiumEnabledMethod · 0.80
nextMethod · 0.80
astParentMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80

Tested by

no test coverage detected