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

Method returnLocalStdMove

lib/checkfunctions.cpp:706–734  ·  view source on GitHub ↗

Check for problems to compiler apply (Named) Return Value Optimization for local variable Technically we have different guarantees between standard versions details: https://en.cppreference.com/w/cpp/language/copy_elision

Source from the content-addressed store, hash-verified

704// Technically we have different guarantees between standard versions
705// details: https://en.cppreference.com/w/cpp/language/copy_elision
706void CheckFunctionsImpl::returnLocalStdMove()
707{
708 if (!mTokenizer->isCPP() || mSettings.standards.cpp < Standards::CPP11)
709 return;
710
711 if (!mSettings.severity.isEnabled(Severity::performance))
712 return;
713
714 logChecker("CheckFunctions::returnLocalStdMove"); // performance,c++11
715
716 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
717 for (const Scope *scope : symbolDatabase->functionScopes) {
718 // Expect return by-value
719 if (Function::returnsReference(scope->function, /*unknown*/ true, /*includeRValueRef*/ true))
720 continue;
721 const auto rets = Function::findReturns(scope->function);
722 for (const Token* ret : rets) {
723 if (!Token::simpleMatch(ret->tokAt(-3), "std :: move ("))
724 continue;
725 const Token* retval = ret->astOperand2();
726 // NRVO
727 if (retval->variable() && retval->variable()->isLocal() && !retval->variable()->isVolatile())
728 copyElisionError(retval);
729 // RVO
730 if (Token::Match(retval, "(|{") && !retval->isCast() && !(retval->valueType() && retval->valueType()->reference != Reference::None))
731 copyElisionError(retval);
732 }
733 }
734}
735
736void CheckFunctionsImpl::copyElisionError(const Token *tok)
737{

Callers 1

runChecksMethod · 0.80

Calls 8

isCPPMethod · 0.80
astOperand2Method · 0.80
variableMethod · 0.80
isVolatileMethod · 0.80
isCastMethod · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected