Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed some Variable copied when it could be moved Coverity warnings #7278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
for (;;) {
const std::string::size_type pos = paths.find(';');
if (pos == std::string::npos) {
mSettings.basePaths.emplace_back(Path::fromNativeSeparators(paths));
mSettings.basePaths.emplace_back(Path::fromNativeSeparators(std::move(paths)));
break;
}
mSettings.basePaths.emplace_back(Path::fromNativeSeparators(paths.substr(0, pos)));
Expand Down
2 changes: 1 addition & 1 deletion cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ void StdLogger::reportErr(const ErrorMessage &msg)
msgCopy.classification = getClassification(msgCopy.guideline, mSettings.reportType);

if (mSettings.outputFormat == Settings::OutputFormat::sarif)
mSarifReport.addFinding(msgCopy);
mSarifReport.addFinding(std::move(msgCopy));
else if (mSettings.xml)
reportErr(msgCopy.toXML());
else
Expand Down
4 changes: 2 additions & 2 deletions lib/addoninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &j
const auto& val = it->second;
if (!val.is<std::string>())
return "Loading " + fileName + " failed. 'executable' must be a string.";
const std::string e = val.get<std::string>();
std::string e = val.get<std::string>();
addoninfo.executable = getFullPath(e, fileName);
if (addoninfo.executable.empty())
addoninfo.executable = e;
addoninfo.executable = std::move(e);
return ""; // <- do not load both "executable" and "script".
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1438,8 +1438,8 @@ void CheckClass::checkMemset()
alloc = mSettings->library.getReallocFuncInfo(tok->tokAt(2));
if (!alloc || alloc->bufferSize == Library::AllocFunc::BufferSize::none)
continue;
const std::set<const Scope *> parsedTypes;
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes);
std::set<const Scope *> parsedTypes;
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, std::move(parsedTypes));

if (printWarnings && tok->variable()->typeScope()->numConstructors > 0)
mallocOnClassWarning(tok, tok->strAt(2), tok->variable()->typeScope()->classDef);
Expand Down
4 changes: 2 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,13 +735,13 @@ void CheckOther::redundantBitwiseOperationInSwitchError()
else if (Token::Match(tok2->previous(), ";|{|}|: %var% %assign% %num% ;") &&
(tok2->strAt(1) == "|=" || tok2->strAt(1) == "&=") &&
Token::Match(tok2->next()->astOperand2(), "%num%")) {
const std::string bitOp = tok2->strAt(1)[0] + tok2->strAt(2);
std::string bitOp = tok2->strAt(1)[0] + tok2->strAt(2);
const auto i2 = utils::as_const(varsWithBitsSet).find(tok2->varId());

// This variable has not had a bit operation performed on it yet, so just make a note of it
if (i2 == varsWithBitsSet.end()) {
varsWithBitsSet[tok2->varId()] = tok2;
bitOperations[tok2->varId()] = bitOp;
bitOperations[tok2->varId()] = std::move(bitOp);
}

// The same bit operation has been performed on the same variable twice, so report an error
Expand Down
4 changes: 2 additions & 2 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Setting
if (funcname) {
if (isRecursiveCall(funcname))
continue;
const auto baseName = stripTemplateParameters(funcname->str());
auto baseName = stripTemplateParameters(funcname->str());
FunctionUsage &func = mFunctions[baseName];
const std::string& called_from_file = tokenizer.list.getFiles()[funcname->fileIndex()];

Expand All @@ -286,7 +286,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Setting
else
func.usedSameFile = true;

mFunctionCalls.insert(baseName);
mFunctionCalls.insert(std::move(baseName));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
typeName.erase(typeName.begin(), typeName.begin() + 2);
switch (mSettings->library.getTypeCheck("unusedvar", typeName)) {
case Library::TypeCheck::def:
bailoutTypeName = typeName;
bailoutTypeName = std::move(typeName);
break;
case Library::TypeCheck::check:
break;
Expand Down
4 changes: 2 additions & 2 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,9 @@ void clangimport::AstNode::setValueType(Token *tok)
if (!decl.front())
break;

const ValueType valueType = ValueType::parseDecl(decl.front(), *mData->mSettings);
ValueType valueType = ValueType::parseDecl(decl.front(), *mData->mSettings);
if (valueType.type != ValueType::Type::UNKNOWN_TYPE) {
tok->setValueType(new ValueType(valueType));
tok->setValueType(new ValueType(std::move(valueType)));
break;
}
}
Expand Down
18 changes: 8 additions & 10 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,6 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
const std::string &premiumArgs,
const CppCheck::ExecuteCmdFn &executeCommand)
{
const std::string redirect = "2>&1";

std::string pythonExe;

if (!addonInfo.executable.empty())
Expand Down Expand Up @@ -462,7 +460,7 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
args += fileArg;

std::string result;
if (const int exitcode = executeCommand(pythonExe, split(args), redirect, result)) {
if (const int exitcode = executeCommand(pythonExe, split(args), "2>&1", result)) {
std::string message("Failed to execute addon '" + addonInfo.name + "' - exitcode is " + std::to_string(exitcode));
std::string details = pythonExe + " " + args;
if (result.size() > 2) {
Expand Down Expand Up @@ -580,17 +578,17 @@ static bool reportClangErrors(std::istream &is, const std::function<void(const E
if (pos1 >= pos2 || pos2 >= pos3)
continue;

const std::string filename = line.substr(0, pos1);
std::string filename = line.substr(0, pos1);
const std::string linenr = line.substr(pos1+1, pos2-pos1-1);
const std::string colnr = line.substr(pos2+1, pos3-pos2-1);
const std::string msg = line.substr(line.find(':', pos3+1) + 2);

const std::string locFile = Path::toNativeSeparators(filename);
std::string locFile = Path::toNativeSeparators(std::move(filename));
const int line_i = strToInt<int>(linenr);
const int column = strToInt<unsigned int>(colnr);
ErrorMessage::FileLocation loc(locFile, line_i, column);
ErrorMessage errmsg({std::move(loc)},
locFile,
std::move(locFile),
Severity::error,
msg,
"syntaxError",
Expand Down Expand Up @@ -1230,10 +1228,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
for (const std::string &s : configurationError)
msg += '\n' + s;

const std::string locFile = Path::toNativeSeparators(file.spath());
std::string locFile = Path::toNativeSeparators(file.spath());
ErrorMessage::FileLocation loc(locFile, 0, 0);
ErrorMessage errmsg({std::move(loc)},
locFile,
std::move(locFile),
Severity::information,
msg,
"noValidConfiguration",
Expand Down Expand Up @@ -1777,13 +1775,13 @@ void CppCheck::executeAddonsWholeProgram(const std::list<FileWithDetails> &files
return;

if (mSettings.buildDir.empty()) {
const std::string fileName = std::to_string(mSettings.pid) + ".ctu-info";
std::string fileName = std::to_string(mSettings.pid) + ".ctu-info";
FilesDeleter filesDeleter;
filesDeleter.addFile(fileName);
std::ofstream fout(fileName);
fout << ctuInfo;
fout.close();
executeAddons({fileName}, "");
executeAddons({std::move(fileName)}, "");
return;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
while (pos < command.size() && command[pos] == ' ')
++pos;
}
const std::string fval = readUntil(command, &pos, " =");
std::string fval = readUntil(command, &pos, " =");
if (F=='D') {
std::string defval = readUntil(command, &pos, " ");
defs += fval;
Expand All @@ -304,7 +304,7 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
} else if (F=='U')
fs.undefs.insert(fval);
else if (F=='I') {
std::string i = fval;
std::string i = std::move(fval);
if (i.size() > 1 && i[0] == '\"' && i.back() == '\"')
i = unescape(i.substr(1, i.size() - 2));
if (std::find(fs.includePaths.cbegin(), fs.includePaths.cend(), i) == fs.includePaths.cend())
Expand Down Expand Up @@ -394,15 +394,15 @@ bool ImportProject::importCompileCommands(std::istream &istr)
continue;
}

const std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());
std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());

// Accept file?
if (!Path::acceptFile(file))
continue;

std::string path;
if (Path::isAbsolute(file))
path = Path::simplifyPath(file);
path = Path::simplifyPath(std::move(file));
#ifdef _WIN32
else if (file[0] == '/' && directory.size() > 2 && std::isalpha(directory[0]) && directory[1] == ':')
// directory: C:\foo\bar
Expand Down Expand Up @@ -1009,7 +1009,7 @@ bool ImportProject::importBcb6Prj(const std::string &projectFilename)
}

if (!arg.empty()) {
cflags.insert(arg);
cflags.insert(std::move(arg));
}

// cleanup: -t is "An alternate name for the -Wxxx switches; there is no difference"
Expand Down
4 changes: 2 additions & 2 deletions lib/summaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ void Summaries::loadReturn(const std::string &buildDir, std::set<std::string> &s
// Get function name
constexpr std::string::size_type pos1 = 0;
const std::string::size_type pos2 = line.find(' ', pos1);
const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
std::vector<std::string> call = getSummaryData(line, "call");
functionCalls[functionName] = call;
if (call.empty())
return1.push_back(functionName);
else {
for (const std::string &c: call) {
functionCalledBy[c].push_back(functionName);
}
}
functionCalls[functionName] = std::move(call);
}
}
summaryReturn.insert(return1.cbegin(), return1.cend());
Expand Down
2 changes: 1 addition & 1 deletion lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ std::string SuppressionList::parseXmlFile(const char *filename)
return std::string("unknown element '") + name + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash.";
}

const std::string err = addSuppression(std::move(s));
std::string err = addSuppression(std::move(s));
if (!err.empty())
return err;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5956,7 +5956,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst, Referen
// sort according to matching arguments
std::sort(fallback1Func.begin(), fallback1Func.end(), fb_pred);
std::sort(fallback2Func.begin(), fallback2Func.end(), fb_pred);
for (const auto& fb : { fallback1Func, fallback2Func }) {
const auto fallbackFuncs = { std::move(fallback1Func), std::move(fallback2Func) };
for (const auto& fb : fallbackFuncs) {
if (fb.size() == 1)
return fb[0].first;
if (fb.size() >= 2) {
Expand Down Expand Up @@ -5985,7 +5986,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst, Referen
return matches[0];

// Prioritize matches in derived scopes
for (const auto& fb : { fallback1Func, fallback2Func }) {
for (const auto& fb : fallbackFuncs) {
const Function* ret = nullptr;
for (std::size_t i = 0; i < fb.size(); ++i) {
if (std::find(matches.cbegin(), matches.cend(), fb[i].first) == matches.cend())
Expand Down
10 changes: 5 additions & 5 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,9 @@ void TemplateSimplifier::getTemplateInstantiations()
// Add outer template..
if (templateParameters(tok->next()) || tok->strAt(2) == ">") {
while (true) {
const std::string fullName = scopeName + (scopeName.empty()?"":" :: ") +
qualification + (qualification.empty()?"":" :: ") + tok->str();
const auto it = std::find_if(mTemplateDeclarations.cbegin(), mTemplateDeclarations.cend(), FindFullName(fullName));
std::string fullName = scopeName + (scopeName.empty()?"":" :: ") +
qualification + (qualification.empty()?"":" :: ") + tok->str();
const auto it = std::find_if(mTemplateDeclarations.cbegin(), mTemplateDeclarations.cend(), FindFullName(std::move(fullName)));
if (it != mTemplateDeclarations.end()) {
// full name matches
addInstantiation(tok, it->scope());
Expand Down Expand Up @@ -3314,9 +3314,9 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(

// New classname/funcname..
const std::string newName(templateDeclaration.name() + " < " + typeForNewName + " >");
const std::string newFullName(templateDeclaration.scope() + (templateDeclaration.scope().empty() ? "" : " :: ") + newName);
std::string newFullName(templateDeclaration.scope() + (templateDeclaration.scope().empty() ? "" : " :: ") + newName);

if (expandedtemplates.insert(newFullName).second) {
if (expandedtemplates.insert(std::move(newFullName)).second) {
expandTemplate(templateDeclaration, templateDeclaration, typeParametersInDeclaration, newName, !specialized && !isVar);
instantiated = true;
mChanged = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin
nameSpace += tok1->str();
tok1 = tok1->next();
}
mImpl->mScopeInfo->usingNamespaces.insert(nameSpace);
mImpl->mScopeInfo->usingNamespaces.insert(std::move(nameSpace));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5177,9 +5177,9 @@ void Tokenizer::setVarIdPass2()
}
std::string scopeName3(scopeName2);
while (!scopeName3.empty()) {
const std::string name = scopeName3 + baseClassName;
std::string name = scopeName3 + baseClassName;
if (varsByClass.find(name) != varsByClass.end()) {
baseClassName = name;
baseClassName = std::move(name);
break;
}
// Remove last scope name
Expand Down
6 changes: 3 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
ValueFlow::Value symValue{};
symValue.valueType = ValueFlow::Value::ValueType::SYMBOLIC;
symValue.tokvalue = tok2;
values.push_back(symValue);
values.push_back(std::move(symValue));
std::copy_if(tok2->values().cbegin(),
tok2->values().cend(),
std::back_inserter(values),
Expand Down Expand Up @@ -7155,10 +7155,10 @@ struct ValueFlowPassRunner {

if (state.settings.severity.isEnabled(Severity::information)) {
const std::string& functionName = functionScope->className;
const std::list<ErrorMessage::FileLocation> callstack(
std::list<ErrorMessage::FileLocation> callstack(
1,
ErrorMessage::FileLocation(functionScope->bodyStart, &state.tokenlist));
const ErrorMessage errmsg(callstack,
const ErrorMessage errmsg(std::move(callstack),
state.tokenlist.getSourceFilePath(),
Severity::information,
"Limiting ValueFlow analysis in function '" + functionName + "' since it is too complex. "
Expand Down
Loading