| 196 | } |
| 197 | |
| 198 | void 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 | |
| 242 | void CheckBoolImpl::comparisonOfFuncReturningBoolError(const Token *tok, const std::string &expression) |
| 243 | { |
no test coverage detected