[LyX/master] Use auto const & when possible to avoid copies

Jean-Marc Lasgouttes lasgouttes at lyx.org
Tue Aug 30 14:47:40 UTC 2022


commit 337cc97174e63dfbcdfdf9dd063b425882399b64
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Tue Aug 30 17:31:37 2022 +0200

    Use auto const & when possible to avoid copies
    
    In several range-based for loops, implicit copies are made. Remove
    that when possible, and try to shut converity up otherwise.
    
    Fixes issues found by coverity.
---
 src/BufferParams.cpp                |    2 ++
 src/frontends/qt/GuiApplication.cpp |    2 +-
 src/insets/InsetInfo.cpp            |    2 ++
 src/lyxfind.cpp                     |    2 +-
 src/support/os.cpp                  |    8 ++++----
 5 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index d25dd05..74270bb 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -1979,6 +1979,8 @@ bool BufferParams::writeLaTeX(otexstream & os, LaTeXFeatures & features,
 	if (!features.runparams().includeall && !included_children_.empty()) {
 		os << "\\includeonly{";
 		bool first = true;
+		// we do not use "auto const &" here, because incfile is modified later
+		// coverity[auto_causes_copy]
 		for (auto incfile : included_children_) {
 			FileName inc = makeAbsPath(incfile, filepath.absFileName());
 			string mangled = DocFileName(changeExtension(inc.absFileName(), ".tex")).
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index 127a847..ff33065 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -556,7 +556,7 @@ QString getAlias(QString name) {
 		has_aliases = true;
 	}
 	// check for the following aliases
-	for (auto alias : aliases) {
+	for (auto const & alias : aliases) {
 		if (name.contains(alias.first))
 			name.replace(alias.first, alias.second);
 	}
diff --git a/src/insets/InsetInfo.cpp b/src/insets/InsetInfo.cpp
index 67aca81..ed2566e 100644
--- a/src/insets/InsetInfo.cpp
+++ b/src/insets/InsetInfo.cpp
@@ -159,6 +159,8 @@ set<string> getTexFileList(string const & filename)
 		getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
 
 	// Normalise paths like /foo//bar ==> /foo/bar
+	// No "auto const &" because doc is modified later
+	// coverity[auto_causes_copy]
 	for (auto doc : doclist) {
 		doc = subst(doc, from_ascii("\r"), docstring());
 		while (contains(doc, from_ascii("//")))
diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp
index bce889b..c8fc409 100644
--- a/src/lyxfind.cpp
+++ b/src/lyxfind.cpp
@@ -2424,7 +2424,7 @@ void LatexInfo::buildEntries(bool isPatternString)
 	}
 	// Ignore language if there is math somewhere in pattern-string
 	if (isPatternString) {
-		for (auto s: usedText) {
+		for (auto const & s: usedText) {
 			// Remove entries created in previous search runs
 			keys.erase(s);
 		}
diff --git a/src/support/os.cpp b/src/support/os.cpp
index 6561fbc..407e1de 100644
--- a/src/support/os.cpp
+++ b/src/support/os.cpp
@@ -111,13 +111,13 @@ static string const find_python_binary()
 	// but we are trying hard to find a valid python binary
 	vector<string> const path = getEnvPath("PATH");
 	lyxerr << "Looking for python 3.x ...\n";
-	for (auto bin : path) {
+	for (auto const & bin : path) {
 		QString const dir = toqstr(bin);
 		string const localdir = dir.toLocal8Bit().constData();
 		QDir qdir(dir);
 		qdir.setFilter(QDir::Files | QDir::Executable);
 		QStringList list = qdir.entryList(QStringList("python3*"));
-		for (auto bin2 : list) {
+		for (auto const & bin2 : list) {
 			string const binary = "\"" + addName(localdir,
 				bin2.toLocal8Bit().constData()) + "\"";
 			command = python23_call(binary, true);
@@ -155,13 +155,13 @@ static string const find_python_binary()
 	QString const exeName = "python2*";
 #endif // _WIN32
 
-	for (auto bin : path) {
+	for (auto const & bin : path) {
 		QString const dir = toqstr(bin);
 		string const localdir = dir.toLocal8Bit().constData();
 		QDir qdir(dir);
 		qdir.setFilter(QDir::Files | QDir::Executable);
 		QStringList list = qdir.entryList(QStringList(exeName));
-		for (auto bin2 : list) {
+		for (auto const & bin2 : list) {
 			string const binary = "\"" + addName(localdir,
 				bin2.toLocal8Bit().constData()) + "\"";
 			command = python23_call(binary, true);


More information about the lyx-cvs mailing list