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

Method checkIgnoredReturnValue

lib/checkfunctions.cpp:248–297  ·  view source on GitHub ↗

--------------------------------------------------------------------------- Check for ignored return values. ---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

246// Check for ignored return values.
247//---------------------------------------------------------------------------
248void 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
299void CheckFunctionsImpl::ignoredReturnValueError(const Token* tok, const std::string& function)
300{

Callers 1

runChecksMethod · 0.45

Calls 13

isPremiumEnabledMethod · 0.80
nextMethod · 0.80
linkAtMethod · 0.80
astParentMethod · 0.80
scopeMethod · 0.80
astOperand1Method · 0.80
getUseRetValTypeMethod · 0.80
getAllocFuncInfoMethod · 0.80
isEnabledMethod · 0.45
isKeywordMethod · 0.45
isStandardTypeMethod · 0.45
functionMethod · 0.45

Tested by

no test coverage detected