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

Method checkRedundantCopy

lib/checkother.cpp:3386–3422  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3384}
3385
3386void CheckOtherImpl::checkRedundantCopy()
3387{
3388 if (!mSettings.severity.isEnabled(Severity::performance) || mTokenizer->isC() || !mSettings.certainty.isEnabled(Certainty::inconclusive))
3389 return;
3390
3391 logChecker("CheckOther::checkRedundantCopy"); // c++,performance,inconclusive
3392
3393 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
3394
3395 for (const Variable* var : symbolDatabase->variableList()) {
3396 if (!var || var->isReference() || var->isPointer() ||
3397 (!var->type() && !var->isStlType() && !(var->valueType() && var->valueType()->container)) || // bailout if var is of standard type, if it is a pointer or non-const
3398 (!var->isConst() && isVariableChanged(var, mSettings)))
3399 continue;
3400
3401 const Token* startTok = var->nameToken();
3402 if (startTok->strAt(1) == "=") // %type% %name% = ... ;
3403 ;
3404 else if (Token::Match(startTok->next(), "(|{") && var->isClass()) {
3405 if (!var->typeScope() && !(var->valueType() && var->valueType()->container))
3406 continue;
3407 // Object is instantiated. Warn if constructor takes arguments by value.
3408 if (var->typeScope() && constructorTakesReference(var->typeScope()))
3409 continue;
3410 } else if (Token::simpleMatch(startTok->next(), ";") && startTok->next()->isSplittedVarDeclEq()) {
3411 startTok = startTok->tokAt(2);
3412 } else
3413 continue;
3414
3415 const Token* tok = startTok->next()->astOperand2();
3416 if (!tok)
3417 continue;
3418 if (!checkFunctionReturnsRef(tok, mSettings) && !checkVariableAssignment(tok, var->valueType(), mSettings))
3419 continue;
3420 redundantCopyError(startTok, startTok->str());
3421 }
3422}
3423
3424void CheckOtherImpl::redundantCopyError(const Token *tok,const std::string& varname)
3425{

Callers 1

runChecksMethod · 0.45

Calls 15

isVariableChangedFunction · 0.85
checkFunctionReturnsRefFunction · 0.85
checkVariableAssignmentFunction · 0.85
isCMethod · 0.80
typeMethod · 0.80
isConstMethod · 0.80
nextMethod · 0.80
isClassMethod · 0.80
astOperand2Method · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45

Tested by

no test coverage detected