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

Method checkComparisonOfFuncReturningBool

lib/checkbool.cpp:198–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

196}
197
198void CheckBoolImpl::checkComparisonOfFuncReturningBool()
199{
200 if (!mSettings.severity.isEnabled(Severity::style))
201 return;
202
203 if (!mTokenizer->isCPP())
204 return;
205
206 logChecker("CheckBool::checkComparisonOfFuncReturningBool"); // style,c++
207
208 const SymbolDatabase * const symbolDatabase = mTokenizer->getSymbolDatabase();
209 auto getFunctionTok = [](const Token* tok) -> const Token* {
210 while (Token::simpleMatch(tok, "!") || (tok && tok->isCast() && !isCPPCast(tok)))
211 tok = tok->astOperand1();
212 if (isCPPCast(tok))
213 tok = tok->astOperand2();
214 if (tok)
215 return tok->previous();
216 return nullptr;
217 };
218
219 for (const Scope * scope : symbolDatabase->functionScopes) {
220 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
221 if (!tok->isComparisonOp() || tok->str() == "==" || tok->str() == "!=")
222 continue;
223
224 const Token* firstToken = getFunctionTok(tok->astOperand1());
225 const Token* secondToken = getFunctionTok(tok->astOperand2());
226 if (!firstToken || !secondToken)
227 continue;
228
229 const bool firstIsFunctionReturningBool = tokenIsFunctionReturningBool(firstToken);
230 const bool secondIsFunctionReturningBool = tokenIsFunctionReturningBool(secondToken);
231 if (firstIsFunctionReturningBool && secondIsFunctionReturningBool) {
232 comparisonOfTwoFuncsReturningBoolError(firstToken->next(), firstToken->str(), secondToken->str());
233 } else if (firstIsFunctionReturningBool) {
234 comparisonOfFuncReturningBoolError(firstToken->next(), firstToken->str());
235 } else if (secondIsFunctionReturningBool) {
236 comparisonOfFuncReturningBoolError(secondToken->previous(), secondToken->str());
237 }
238 }
239 }
240}
241
242void CheckBoolImpl::comparisonOfFuncReturningBoolError(const Token *tok, const std::string &expression)
243{

Callers 1

runChecksMethod · 0.80

Calls 10

isCPPCastFunction · 0.85
isCPPMethod · 0.80
isCastMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
nextMethod · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected