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

Function checkMissingReturnScope

lib/checkfunctions.cpp:351–432  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

349}
350
351static const Token *checkMissingReturnScope(const Token *tok, const Library &library)
352{
353 const Token *lastStatement = nullptr;
354 while ((tok = tok->previous()) != nullptr) {
355 if (tok->str() == ")")
356 tok = tok->link();
357 if (tok->str() == "{")
358 return lastStatement ? lastStatement : tok->next();
359 if (tok->str() == "}") {
360 for (const Token *prev = tok->link()->previous(); prev && prev->scope() == tok->scope() && !Token::Match(prev, "[;{}]"); prev = prev->previous()) {
361 if (prev->isKeyword() && Token::Match(prev, "return|throw"))
362 return nullptr;
363 if (prev->str() == "goto" && !isForwardJump(prev))
364 return nullptr;
365 }
366 if (tok->scope()->type == ScopeType::eSwitch) {
367 bool reachable = false;
368 for (const Token *switchToken = tok->link()->next(); switchToken != tok; switchToken = switchToken->next()) {
369 if (reachable && Token::simpleMatch(switchToken, "break ;")) {
370 if (Token::simpleMatch(switchToken->previous(), "}") && !checkMissingReturnScope(switchToken->previous(), library))
371 reachable = false;
372 else
373 return switchToken;
374 }
375 if (switchToken->isKeyword() && Token::Match(switchToken, "return|throw"))
376 reachable = false;
377 if (Token::Match(switchToken, "%name% (") && library.isnoreturn(switchToken))
378 reachable = false;
379 if (Token::Match(switchToken, "case|default"))
380 reachable = true;
381 else if (switchToken->str() == "{" && (switchToken->scope()->isLoopScope() || switchToken->scope()->type == ScopeType::eSwitch))
382 switchToken = switchToken->link();
383 }
384 if (!isExhaustiveSwitch(tok->link()))
385 return tok->link();
386 } else if (tok->scope()->type == ScopeType::eIf) {
387 const Token *condition = tok->scope()->classDef->next()->astOperand2();
388 if (condition && condition->hasKnownIntValue() && condition->getKnownIntValue() == 1)
389 return checkMissingReturnScope(tok, library);
390 return tok;
391 } else if (tok->scope()->type == ScopeType::eElse) {
392 const Token *errorToken = checkMissingReturnScope(tok, library);
393 if (errorToken)
394 return errorToken;
395 tok = tok->link();
396 if (Token::simpleMatch(tok->tokAt(-2), "} else {"))
397 return checkMissingReturnScope(tok->tokAt(-2), library);
398 return tok;
399 } else if (tok->scope()->type == ScopeType::eCatch) {
400 while (tok->str() == "}") {
401 const Token *errorToken = checkMissingReturnScope(tok, library);
402 if (errorToken || tok->scope()->type == ScopeType::eTry)
403 return errorToken;
404 tok = tok->link();
405 if (Token::simpleMatch(tok->previous(), ") {") && Token::simpleMatch(tok->linkAt(-1)->tokAt(-2), "} catch ("))
406 tok = tok->linkAt(-1)->tokAt(-2);
407 else
408 break;

Callers 1

checkMissingReturnMethod · 0.85

Calls 14

isForwardJumpFunction · 0.85
isExhaustiveSwitchFunction · 0.85
nextMethod · 0.80
scopeMethod · 0.80
isnoreturnMethod · 0.80
astOperand2Method · 0.80
getKnownIntValueMethod · 0.80
linkAtMethod · 0.80
isnotnoreturnMethod · 0.80
simpleMatchFunction · 0.70
strMethod · 0.45
isKeywordMethod · 0.45

Tested by

no test coverage detected