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
| 704 | // Technically we have different guarantees between standard versions |
| 705 | // details: https://en.cppreference.com/w/cpp/language/copy_elision |
| 706 | void 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 | |
| 736 | void CheckFunctionsImpl::copyElisionError(const Token *tok) |
| 737 | { |
no test coverage detected