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

Method checkComparisonOfBoolWithInt

lib/checkbool.cpp:147–172  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

145//---------------------------------------------------------------------------
146
147void CheckBoolImpl::checkComparisonOfBoolWithInt()
148{
149 if (!mSettings.severity.isEnabled(Severity::warning) || !mTokenizer->isCPP())
150 return;
151
152 logChecker("CheckBool::checkComparisonOfBoolWithInt"); // warning,c++
153
154 const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase();
155 for (const Scope * scope : symbolDatabase->functionScopes) {
156 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
157 if (!tok->isComparisonOp() || !tok->isBinaryOp())
158 continue;
159 const Token* const left = tok->astOperand1();
160 const Token* const right = tok->astOperand2();
161 if (left->isBoolean() && right->varId()) { // Comparing boolean constant with variable
162 if (tok->str() != "==" && tok->str() != "!=") {
163 comparisonOfBoolWithInvalidComparator(right, left->str());
164 }
165 } else if (left->varId() && right->isBoolean()) { // Comparing variable with boolean constant
166 if (tok->str() != "==" && tok->str() != "!=") {
167 comparisonOfBoolWithInvalidComparator(right, left->str());
168 }
169 }
170 }
171 }
172}
173
174void CheckBoolImpl::comparisonOfBoolWithInvalidComparator(const Token *tok, const std::string &expression)
175{

Callers 1

runChecksMethod · 0.80

Calls 7

isCPPMethod · 0.80
nextMethod · 0.80
isBinaryOpMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
isEnabledMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected