Skip to content

Commit de1a969

Browse files
authored
fixed some Variable copied when it could be moved Coverity warnings (danmar#7278)
1 parent 08ae2de commit de1a969

17 files changed

+43
-44
lines changed

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12231223
for (;;) {
12241224
const std::string::size_type pos = paths.find(';');
12251225
if (pos == std::string::npos) {
1226-
mSettings.basePaths.emplace_back(Path::fromNativeSeparators(paths));
1226+
mSettings.basePaths.emplace_back(Path::fromNativeSeparators(std::move(paths)));
12271227
break;
12281228
}
12291229
mSettings.basePaths.emplace_back(Path::fromNativeSeparators(paths.substr(0, pos)));

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ void StdLogger::reportErr(const ErrorMessage &msg)
653653
msgCopy.classification = getClassification(msgCopy.guideline, mSettings.reportType);
654654

655655
if (mSettings.outputFormat == Settings::OutputFormat::sarif)
656-
mSarifReport.addFinding(msgCopy);
656+
mSarifReport.addFinding(std::move(msgCopy));
657657
else if (mSettings.outputFormat == Settings::OutputFormat::xml)
658658
reportErr(msgCopy.toXML());
659659
else

lib/addoninfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &j
115115
const auto& val = it->second;
116116
if (!val.is<std::string>())
117117
return "Loading " + fileName + " failed. 'executable' must be a string.";
118-
const std::string e = val.get<std::string>();
118+
std::string e = val.get<std::string>();
119119
addoninfo.executable = getFullPath(e, fileName);
120120
if (addoninfo.executable.empty())
121-
addoninfo.executable = e;
121+
addoninfo.executable = std::move(e);
122122
return ""; // <- do not load both "executable" and "script".
123123
}
124124
}

lib/checkclass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,8 @@ void CheckClass::checkMemset()
14381438
alloc = mSettings->library.getReallocFuncInfo(tok->tokAt(2));
14391439
if (!alloc || alloc->bufferSize == Library::AllocFunc::BufferSize::none)
14401440
continue;
1441-
const std::set<const Scope *> parsedTypes;
1442-
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes);
1441+
std::set<const Scope *> parsedTypes;
1442+
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, std::move(parsedTypes));
14431443

14441444
if (printWarnings && tok->variable()->typeScope()->numConstructors > 0)
14451445
mallocOnClassWarning(tok, tok->strAt(2), tok->variable()->typeScope()->classDef);

lib/checkother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,13 @@ void CheckOther::redundantBitwiseOperationInSwitchError()
735735
else if (Token::Match(tok2->previous(), ";|{|}|: %var% %assign% %num% ;") &&
736736
(tok2->strAt(1) == "|=" || tok2->strAt(1) == "&=") &&
737737
Token::Match(tok2->next()->astOperand2(), "%num%")) {
738-
const std::string bitOp = tok2->strAt(1)[0] + tok2->strAt(2);
738+
std::string bitOp = tok2->strAt(1)[0] + tok2->strAt(2);
739739
const auto i2 = utils::as_const(varsWithBitsSet).find(tok2->varId());
740740

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

747747
// The same bit operation has been performed on the same variable twice, so report an error

lib/checkunusedfunctions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Setting
277277
if (funcname) {
278278
if (isRecursiveCall(funcname))
279279
continue;
280-
const auto baseName = stripTemplateParameters(funcname->str());
280+
auto baseName = stripTemplateParameters(funcname->str());
281281
FunctionUsage &func = mFunctions[baseName];
282282
const std::string& called_from_file = tokenizer.list.getFiles()[funcname->fileIndex()];
283283

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

289-
mFunctionCalls.insert(baseName);
289+
mFunctionCalls.insert(std::move(baseName));
290290
}
291291
}
292292
}

lib/checkunusedvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
12981298
typeName.erase(typeName.begin(), typeName.begin() + 2);
12991299
switch (mSettings->library.getTypeCheck("unusedvar", typeName)) {
13001300
case Library::TypeCheck::def:
1301-
bailoutTypeName = typeName;
1301+
bailoutTypeName = std::move(typeName);
13021302
break;
13031303
case Library::TypeCheck::check:
13041304
break;

lib/clangimport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,9 @@ void clangimport::AstNode::setValueType(Token *tok)
678678
if (!decl.front())
679679
break;
680680

681-
const ValueType valueType = ValueType::parseDecl(decl.front(), *mData->mSettings);
681+
ValueType valueType = ValueType::parseDecl(decl.front(), *mData->mSettings);
682682
if (valueType.type != ValueType::Type::UNKNOWN_TYPE) {
683-
tok->setValueType(new ValueType(valueType));
683+
tok->setValueType(new ValueType(std::move(valueType)));
684684
break;
685685
}
686686
}

lib/cppcheck.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,6 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
433433
const std::string &premiumArgs,
434434
const CppCheck::ExecuteCmdFn &executeCommand)
435435
{
436-
const std::string redirect = "2>&1";
437-
438436
std::string pythonExe;
439437

440438
if (!addonInfo.executable.empty())
@@ -463,7 +461,7 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
463461
args += fileArg;
464462

465463
std::string result;
466-
if (const int exitcode = executeCommand(pythonExe, split(args), redirect, result)) {
464+
if (const int exitcode = executeCommand(pythonExe, split(args), "2>&1", result)) {
467465
std::string message("Failed to execute addon '" + addonInfo.name + "' - exitcode is " + std::to_string(exitcode));
468466
std::string details = pythonExe + " " + args;
469467
if (result.size() > 2) {
@@ -585,17 +583,17 @@ static bool reportClangErrors(std::istream &is, const std::function<void(const E
585583
if (pos1 >= pos2 || pos2 >= pos3)
586584
continue;
587585

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

593-
const std::string locFile = Path::toNativeSeparators(filename);
591+
std::string locFile = Path::toNativeSeparators(std::move(filename));
594592
const int line_i = strToInt<int>(linenr);
595593
const int column = strToInt<unsigned int>(colnr);
596594
ErrorMessage::FileLocation loc(locFile, line_i, column);
597595
ErrorMessage errmsg({std::move(loc)},
598-
locFile,
596+
std::move(locFile),
599597
Severity::error,
600598
msg,
601599
"syntaxError",
@@ -1228,10 +1226,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
12281226
for (const std::string &s : configurationError)
12291227
msg += '\n' + s;
12301228

1231-
const std::string locFile = Path::toNativeSeparators(file.spath());
1229+
std::string locFile = Path::toNativeSeparators(file.spath());
12321230
ErrorMessage::FileLocation loc(locFile, 0, 0);
12331231
ErrorMessage errmsg({std::move(loc)},
1234-
locFile,
1232+
std::move(locFile),
12351233
Severity::information,
12361234
msg,
12371235
"noValidConfiguration",
@@ -1779,13 +1777,13 @@ void CppCheck::executeAddonsWholeProgram(const std::list<FileWithDetails> &files
17791777
return;
17801778

17811779
if (mSettings.buildDir.empty()) {
1782-
const std::string fileName = std::to_string(mSettings.pid) + ".ctu-info";
1780+
std::string fileName = std::to_string(mSettings.pid) + ".ctu-info";
17831781
FilesDeleter filesDeleter;
17841782
filesDeleter.addFile(fileName);
17851783
std::ofstream fout(fileName);
17861784
fout << ctuInfo;
17871785
fout.close();
1788-
executeAddons({fileName}, "");
1786+
executeAddons({std::move(fileName)}, "");
17891787
return;
17901788
}
17911789

lib/importproject.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
293293
while (pos < command.size() && command[pos] == ' ')
294294
++pos;
295295
}
296-
const std::string fval = readUntil(command, &pos, " =");
296+
std::string fval = readUntil(command, &pos, " =");
297297
if (F=='D') {
298298
std::string defval = readUntil(command, &pos, " ");
299299
defs += fval;
@@ -307,7 +307,7 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
307307
} else if (F=='U')
308308
fs.undefs.insert(fval);
309309
else if (F=='I') {
310-
std::string i = fval;
310+
std::string i = std::move(fval);
311311
if (i.size() > 1 && i[0] == '\"' && i.back() == '\"')
312312
i = unescape(i.substr(1, i.size() - 2));
313313
if (std::find(fs.includePaths.cbegin(), fs.includePaths.cend(), i) == fs.includePaths.cend())
@@ -397,15 +397,15 @@ bool ImportProject::importCompileCommands(std::istream &istr)
397397
continue;
398398
}
399399

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

402402
// Accept file?
403403
if (!Path::acceptFile(file))
404404
continue;
405405

406406
std::string path;
407407
if (Path::isAbsolute(file))
408-
path = Path::simplifyPath(file);
408+
path = Path::simplifyPath(std::move(file));
409409
#ifdef _WIN32
410410
else if (file[0] == '/' && directory.size() > 2 && std::isalpha(directory[0]) && directory[1] == ':')
411411
// directory: C:\foo\bar
@@ -1012,7 +1012,7 @@ bool ImportProject::importBcb6Prj(const std::string &projectFilename)
10121012
}
10131013

10141014
if (!arg.empty()) {
1015-
cflags.insert(arg);
1015+
cflags.insert(std::move(arg));
10161016
}
10171017

10181018
// cleanup: -t is "An alternate name for the -Wxxx switches; there is no difference"

0 commit comments

Comments
 (0)