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

Method assignFunctionArg

lib/checkautovariables.cpp:205–234  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

203}
204
205void CheckAutoVariablesImpl::assignFunctionArg()
206{
207 const bool printStyle = mSettings.severity.isEnabled(Severity::style);
208 const bool printWarning = mSettings.severity.isEnabled(Severity::warning);
209 if (!printStyle && !printWarning && !mSettings.isPremiumEnabled("uselessAssignmentPtrArg"))
210 return;
211
212 logChecker("CheckAutoVariables::assignFunctionArg"); // style,warning
213
214 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
215 for (const Scope * scope : symbolDatabase->functionScopes) {
216 for (const Token *tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) {
217 // TODO: What happens if this is removed?
218 if (tok->astParent())
219 continue;
220 if (!(tok->isAssignmentOp() || tok->tokType() == Token::eIncDecOp) || !Token::Match(tok->astOperand1(), "%var%"))
221 continue;
222 const Token* const vartok = tok->astOperand1();
223 if (isNonReferenceArg(vartok) &&
224 !Token::Match(vartok->next(), "= %varid% ;", vartok->varId()) &&
225 !variableIsUsedInScope(Token::findsimplematch(vartok->next(), ";"), vartok->varId(), scope) &&
226 !Token::findsimplematch(vartok, "goto", scope->bodyEnd)) {
227 if (vartok->variable()->isPointer() && printWarning)
228 errorUselessAssignmentPtrArg(vartok);
229 else if (printStyle)
230 errorUselessAssignmentArg(vartok);
231 }
232 }
233 }
234}
235
236static bool isAutoVariableRHS(const Token* tok, const Settings& settings) {
237 return isAddressOfLocalVariable(tok) || isAutoVarArray(tok) || isLocalContainerBuffer(tok, settings);

Callers 1

runChecksMethod · 0.80

Calls 10

isNonReferenceArgFunction · 0.85
variableIsUsedInScopeFunction · 0.85
findsimplematchFunction · 0.85
isPremiumEnabledMethod · 0.80
nextMethod · 0.80
astParentMethod · 0.80
isAssignmentOpMethod · 0.80
astOperand1Method · 0.80
variableMethod · 0.80
isEnabledMethod · 0.45

Tested by

no test coverage detected