--------------------------------------------------------------------------- Check for ignored return values. ---------------------------------------------------------------------------
| 246 | // Check for ignored return values. |
| 247 | //--------------------------------------------------------------------------- |
| 248 | void CheckFunctionsImpl::checkIgnoredReturnValue() |
| 249 | { |
| 250 | if (!mSettings.severity.isEnabled(Severity::warning) && |
| 251 | !mSettings.severity.isEnabled(Severity::style) && |
| 252 | !mSettings.isPremiumEnabled("ignoredReturnValue")) |
| 253 | return; |
| 254 | |
| 255 | logChecker("CheckFunctions::checkIgnoredReturnValue"); // style,warning |
| 256 | |
| 257 | const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); |
| 258 | for (const Scope *scope : symbolDatabase->functionScopes) { |
| 259 | for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { |
| 260 | // skip c++11 initialization, ({...}) |
| 261 | if (Token::Match(tok, "%var%|(|,|return {")) |
| 262 | tok = tok->linkAt(1); |
| 263 | else if (Token::Match(tok, "[(<]") && tok->link()) |
| 264 | tok = tok->link(); |
| 265 | |
| 266 | if (tok->varId() || tok->isKeyword() || tok->isStandardType() || !Token::Match(tok, "%name% (")) |
| 267 | continue; |
| 268 | |
| 269 | const Token *parent = tok->next()->astParent(); |
| 270 | while (Token::Match(parent, "%cop%")) { |
| 271 | if (Token::Match(parent, "<<|>>|*") && !parent->astParent()) |
| 272 | break; |
| 273 | parent = parent->astParent(); |
| 274 | } |
| 275 | if (parent) |
| 276 | continue; |
| 277 | |
| 278 | if (!tok->scope()->isExecutable()) { |
| 279 | tok = tok->scope()->bodyEnd; |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) && |
| 284 | tok->next()->astOperand1()) { |
| 285 | const Library::UseRetValType retvalTy = mSettings.library.getUseRetValType(tok); |
| 286 | const bool warn = (tok->function() && (tok->function()->isAttributeNodiscard() || tok->function()->isAttributePure() || tok->function()->isAttributeConst())) || |
| 287 | // avoid duplicate warnings for resource-allocating functions |
| 288 | (retvalTy == Library::UseRetValType::DEFAULT && mSettings.library.getAllocFuncInfo(tok) == nullptr); |
| 289 | if (mSettings.severity.isEnabled(Severity::warning) && warn) |
| 290 | ignoredReturnValueError(tok, tok->next()->astOperand1()->expressionString()); |
| 291 | else if (mSettings.severity.isEnabled(Severity::style) && |
| 292 | retvalTy == Library::UseRetValType::ERROR_CODE) |
| 293 | ignoredReturnErrorCode(tok, tok->next()->astOperand1()->expressionString()); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void CheckFunctionsImpl::ignoredReturnValueError(const Token* tok, const std::string& function) |
| 300 | { |
no test coverage detected