Skip to content

Commit 8ef14da

Browse files
authored
fixed and enabled performance-faster-string-find clang-tidy warning (#4769)
1 parent 14e78e1 commit 8ef14da

16 files changed

Lines changed: 57 additions & 58 deletions

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: '*,-abseil-*,-altera-*,-android-*,-boost-*,-cert-*,-cppcoreguidelines-*,-darwin-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,google-explicit-constructor,-readability-braces-around-statements,-readability-magic-numbers,-bugprone-macro-parentheses,-readability-isolate-declaration,-readability-function-size,-modernize-use-trailing-return-type,-readability-implicit-bool-conversion,-readability-uppercase-literal-suffix,-modernize-use-auto,-readability-else-after-return,-modernize-use-default-member-init,-readability-redundant-member-init,-performance-faster-string-find,-modernize-avoid-c-arrays,-modernize-use-equals-default,-readability-container-size-empty,-readability-simplify-boolean-expr,-bugprone-branch-clone,-bugprone-narrowing-conversions,-modernize-raw-string-literal,-readability-convert-member-functions-to-static,-modernize-loop-convert,-readability-const-return-type,-modernize-return-braced-init-list,-performance-inefficient-string-concatenation,-misc-throw-by-value-catch-by-reference,-readability-avoid-const-params-in-decls,-misc-non-private-member-variables-in-classes,-clang-analyzer-*,-bugprone-signed-char-misuse,-misc-no-recursion,-readability-use-anyofallof,-performance-no-automatic-move,-readability-function-cognitive-complexity,-readability-redundant-access-specifiers,-performance-noexcept-move-constructor,-concurrency-mt-unsafe,-bugprone-easily-swappable-parameters,-readability-suspicious-call-argument,-readability-identifier-length,-readability-container-data-pointer,-bugprone-assignment-in-if-condition,-misc-const-correctness,-portability-std-allocator-const,-modernize-deprecated-ios-base-aliases,-bugprone-unchecked-optional-access,-modernize-replace-auto-ptr,-readability-identifier-naming,-portability-simd-intrinsics'
2+
Checks: '*,-abseil-*,-altera-*,-android-*,-boost-*,-cert-*,-cppcoreguidelines-*,-darwin-*,-fuchsia-*,-google-*,-hicpp-*,-linuxkernel-*,-llvm-*,-llvmlibc-*,-mpi-*,-objc-*,-openmp-*,-zircon-*,google-explicit-constructor,-readability-braces-around-statements,-readability-magic-numbers,-bugprone-macro-parentheses,-readability-isolate-declaration,-readability-function-size,-modernize-use-trailing-return-type,-readability-implicit-bool-conversion,-readability-uppercase-literal-suffix,-modernize-use-auto,-readability-else-after-return,-modernize-use-default-member-init,-readability-redundant-member-init,-modernize-avoid-c-arrays,-modernize-use-equals-default,-readability-container-size-empty,-readability-simplify-boolean-expr,-bugprone-branch-clone,-bugprone-narrowing-conversions,-modernize-raw-string-literal,-readability-convert-member-functions-to-static,-modernize-loop-convert,-readability-const-return-type,-modernize-return-braced-init-list,-performance-inefficient-string-concatenation,-misc-throw-by-value-catch-by-reference,-readability-avoid-const-params-in-decls,-misc-non-private-member-variables-in-classes,-clang-analyzer-*,-bugprone-signed-char-misuse,-misc-no-recursion,-readability-use-anyofallof,-performance-no-automatic-move,-readability-function-cognitive-complexity,-readability-redundant-access-specifiers,-performance-noexcept-move-constructor,-concurrency-mt-unsafe,-bugprone-easily-swappable-parameters,-readability-suspicious-call-argument,-readability-identifier-length,-readability-container-data-pointer,-bugprone-assignment-in-if-condition,-misc-const-correctness,-portability-std-allocator-const,-modernize-deprecated-ios-base-aliases,-bugprone-unchecked-optional-access,-modernize-replace-auto-ptr,-readability-identifier-naming,-portability-simd-intrinsics'
33
WarningsAsErrors: '*'
44
HeaderFilterRegex: '(cli|gui|lib|oss-fuzz|test|triage)\/[a-z]+\.h'
55
CheckOptions:

clang-tidy.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ These do not (always) increase readability.
5858

5959
To be documented.
6060

61-
`performance-faster-string-find`<br>
6261
`bugprone-narrowing-conversions`<br>
6362
`performance-no-automatic-move`<br>
6463

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ bool CppCheckExecutor::executeCommand(std::string exe, std::vector<std::string>
581581
for (const std::string &arg : args) {
582582
if (!joinedArgs.empty())
583583
joinedArgs += " ";
584-
if (arg.find(" ") != std::string::npos)
584+
if (arg.find(' ') != std::string::npos)
585585
joinedArgs += '"' + arg + '"';
586586
else
587587
joinedArgs += arg;

gui/projectfiledialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ void ProjectFileDialog::addSingleSuppression(const Suppressions::Suppression &su
739739
bool found_relative = false;
740740

741741
// Replace relative file path in the suppression with the absolute one
742-
if ((suppression.fileName.find("*") == std::string::npos) &&
742+
if ((suppression.fileName.find('*') == std::string::npos) &&
743743
(suppression.fileName.find(sep) == std::string::npos)) {
744744
QFileInfo inf(mProjectFile->getFilename());
745745
QString rootpath = inf.absolutePath();

lib/checkunusedvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ void CheckUnusedVar::checkStructMemberUsage()
14471447
continue;
14481448

14491449
// Bail out for template struct, members might be used in non-matching instantiations
1450-
if (scope.className.find("<") != std::string::npos)
1450+
if (scope.className.find('<') != std::string::npos)
14511451
continue;
14521452

14531453
// bail out if struct is inherited

lib/clangimport.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,22 @@ static std::string unquote(const std::string &s)
124124
static std::vector<std::string> splitString(const std::string &line)
125125
{
126126
std::vector<std::string> ret;
127-
std::string::size_type pos1 = line.find_first_not_of(" ");
127+
std::string::size_type pos1 = line.find_first_not_of(' ');
128128
while (pos1 < line.size()) {
129129
std::string::size_type pos2;
130130
if (std::strchr("*()", line[pos1])) {
131131
ret.push_back(line.substr(pos1,1));
132-
pos1 = line.find_first_not_of(" ", pos1 + 1);
132+
pos1 = line.find_first_not_of(' ', pos1 + 1);
133133
continue;
134134
}
135135
if (line[pos1] == '<')
136-
pos2 = line.find(">", pos1);
136+
pos2 = line.find('>', pos1);
137137
else if (line[pos1] == '\"')
138-
pos2 = line.find("\"", pos1+1);
138+
pos2 = line.find('\"', pos1+1);
139139
else if (line[pos1] == '\'') {
140-
pos2 = line.find("\'", pos1+1);
140+
pos2 = line.find('\'', pos1+1);
141141
if (pos2 < (int)line.size() - 3 && line.compare(pos2, 3, "\':\'", 0, 3) == 0)
142-
pos2 = line.find("\'", pos2 + 3);
142+
pos2 = line.find('\'', pos2 + 3);
143143
} else {
144144
pos2 = pos1;
145145
while (pos2 < line.size() && (line[pos2] == '_' || line[pos2] == ':' || std::isalnum((unsigned char)line[pos2])))
@@ -159,21 +159,21 @@ static std::vector<std::string> splitString(const std::string &line)
159159
}
160160
}
161161

162-
pos2 = line.find(" ", pos1) - 1;
162+
pos2 = line.find(' ', pos1) - 1;
163163
if ((std::isalpha(line[pos1]) || line[pos1] == '_') &&
164164
line.find("::", pos1) < pos2 &&
165-
line.find("::", pos1) < line.find("<", pos1)) {
165+
line.find("::", pos1) < line.find('<', pos1)) {
166166
pos2 = line.find("::", pos1);
167167
ret.push_back(line.substr(pos1, pos2-pos1));
168168
ret.emplace_back("::");
169169
pos1 = pos2 + 2;
170170
continue;
171171
}
172172
if ((std::isalpha(line[pos1]) || line[pos1] == '_') &&
173-
line.find("<", pos1) < pos2 &&
174-
line.find("<<",pos1) != line.find("<",pos1) &&
175-
line.find(">", pos1) != std::string::npos &&
176-
line.find(">", pos1) > pos2) {
173+
line.find('<', pos1) < pos2 &&
174+
line.find("<<",pos1) != line.find('<',pos1) &&
175+
line.find('>', pos1) != std::string::npos &&
176+
line.find('>', pos1) > pos2) {
177177
int level = 0;
178178
for (pos2 = pos1; pos2 < line.size(); ++pos2) {
179179
if (line[pos2] == '<')
@@ -186,7 +186,7 @@ static std::vector<std::string> splitString(const std::string &line)
186186
}
187187
if (level > 1 && pos2 + 1 >= line.size())
188188
return std::vector<std::string> {};
189-
pos2 = line.find(" ", pos2);
189+
pos2 = line.find(' ', pos2);
190190
if (pos2 != std::string::npos)
191191
--pos2;
192192
}
@@ -196,7 +196,7 @@ static std::vector<std::string> splitString(const std::string &line)
196196
break;
197197
}
198198
ret.push_back(line.substr(pos1, pos2+1-pos1));
199-
pos1 = line.find_first_not_of(" ", pos2 + 1);
199+
pos1 = line.find_first_not_of(' ', pos2 + 1);
200200
}
201201
return ret;
202202
}
@@ -546,8 +546,8 @@ const ::Type * clangimport::AstNode::addTypeTokens(TokenList *tokenList, const s
546546

547547
std::string type;
548548
if (str.find(" (") != std::string::npos) {
549-
if (str.find("<") != std::string::npos)
550-
type = str.substr(1, str.find("<")) + "...>";
549+
if (str.find('<') != std::string::npos)
550+
type = str.substr(1, str.find('<')) + "...>";
551551
else
552552
type = str.substr(1,str.find(" (")-1);
553553
} else
@@ -557,8 +557,8 @@ const ::Type * clangimport::AstNode::addTypeTokens(TokenList *tokenList, const s
557557
type.erase(type.find("(*)("));
558558
type += "*";
559559
}
560-
if (type.find("(") != std::string::npos)
561-
type.erase(type.find("("));
560+
if (type.find('(') != std::string::npos)
561+
type.erase(type.find('('));
562562

563563
std::stack<Token *> lpar;
564564
for (const std::string &s: splitString(type)) {
@@ -620,7 +620,7 @@ void clangimport::AstNode::setValueType(Token *tok)
620620
for (int i = 0; i < 2; i++) {
621621
const std::string &type = getType(i);
622622

623-
if (type.find("<") != std::string::npos)
623+
if (type.find('<') != std::string::npos)
624624
// TODO
625625
continue;
626626

@@ -897,8 +897,8 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
897897
return newtok;
898898
}
899899
std::string type = getType();
900-
if (type.find("*") != std::string::npos)
901-
type = type.erase(type.rfind("*"));
900+
if (type.find('*') != std::string::npos)
901+
type = type.erase(type.rfind('*'));
902902
addTypeTokens(tokenList, type);
903903
if (!children.empty()) {
904904
Token *bracket1 = addtoken(tokenList, "[");
@@ -1577,15 +1577,15 @@ void clangimport::parseClangAstDump(Tokenizer *tokenizer, std::istream &f)
15771577
std::string line;
15781578
std::vector<AstNodePtr> tree;
15791579
while (std::getline(f,line)) {
1580-
const std::string::size_type pos1 = line.find("-");
1580+
const std::string::size_type pos1 = line.find('-');
15811581
if (pos1 == std::string::npos)
15821582
continue;
15831583
if (!tree.empty() && line.substr(pos1) == "-<<<NULL>>>") {
15841584
const int level = (pos1 - 1) / 2;
15851585
tree[level - 1]->children.push_back(nullptr);
15861586
continue;
15871587
}
1588-
const std::string::size_type pos2 = line.find(" ", pos1);
1588+
const std::string::size_type pos2 = line.find(' ', pos1);
15891589
if (pos2 < pos1 + 4 || pos2 == std::string::npos)
15901590
continue;
15911591
const std::string nodeType = line.substr(pos1+1, pos2 - pos1 - 1);

lib/cppcheck.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,20 @@ namespace {
162162
in >> json;
163163
return parseAddonInfo(json, fileName, exename);
164164
}
165-
if (fileName.find(".") == std::string::npos)
165+
if (fileName.find('.') == std::string::npos)
166166
return getAddonInfo(fileName + ".py", exename);
167167

168168
if (endsWith(fileName, ".py")) {
169169
scriptFile = getFullPath(fileName, exename);
170170
if (scriptFile.empty())
171171
return "Did not find addon " + fileName;
172172

173-
std::string::size_type pos1 = scriptFile.rfind("/");
173+
std::string::size_type pos1 = scriptFile.rfind('/');
174174
if (pos1 == std::string::npos)
175175
pos1 = 0;
176176
else
177177
pos1++;
178-
std::string::size_type pos2 = scriptFile.rfind(".");
178+
std::string::size_type pos2 = scriptFile.rfind('.');
179179
if (pos2 < pos1)
180180
pos2 = std::string::npos;
181181
name = scriptFile.substr(pos1, pos2 - pos1);
@@ -201,7 +201,7 @@ namespace {
201201
static std::string cmdFileName(std::string f)
202202
{
203203
f = Path::toNativeSeparators(f);
204-
if (f.find(" ") != std::string::npos)
204+
if (f.find(' ') != std::string::npos)
205205
return "\"" + f + "\"";
206206
return f;
207207
}
@@ -215,7 +215,7 @@ static std::vector<std::string> split(const std::string &str, const std::string
215215
break;
216216

217217
if (str[startPos] == '\"') {
218-
const std::string::size_type endPos = str.find("\"", startPos + 1);
218+
const std::string::size_type endPos = str.find('\"', startPos + 1);
219219
ret.push_back(str.substr(startPos + 1, endPos - startPos - 1));
220220
startPos = (endPos < str.size()) ? (endPos + 1) : endPos;
221221
continue;
@@ -415,16 +415,16 @@ static bool reportClangErrors(std::istream &is, const std::function<void(const E
415415
continue;
416416

417417
// file:line:column: error: ....
418-
const std::string::size_type pos2 = line.rfind(":", pos3 - 1);
419-
const std::string::size_type pos1 = line.rfind(":", pos2 - 1);
418+
const std::string::size_type pos2 = line.rfind(':', pos3 - 1);
419+
const std::string::size_type pos1 = line.rfind(':', pos2 - 1);
420420

421421
if (pos1 >= pos2 || pos2 >= pos3)
422422
continue;
423423

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

429429
const std::string locFile = Path::toNativeSeparators(filename);
430430
ErrorMessage::FileLocation loc;
@@ -1701,8 +1701,8 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings)
17011701
endColumnPos = line.find(": warning:");
17021702
}
17031703

1704-
const std::size_t endLinePos = line.rfind(":", endColumnPos-1);
1705-
const std::size_t endNamePos = line.rfind(":", endLinePos - 1);
1704+
const std::size_t endLinePos = line.rfind(':', endColumnPos-1);
1705+
const std::size_t endNamePos = line.rfind(':', endLinePos - 1);
17061706
const std::size_t endMsgTypePos = line.find(':', endColumnPos + 2);
17071707
const std::size_t endErrorPos = line.rfind('[', std::string::npos);
17081708
if (endLinePos==std::string::npos || endNamePos==std::string::npos || endMsgTypePos==std::string::npos || endErrorPos==std::string::npos)

lib/errorlogger.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis
118118
const Token *tok = e.first;
119119
std::string info = e.second;
120120

121-
if (info.compare(0,8,"$symbol:") == 0 && info.find("\n") < info.size()) {
122-
const std::string::size_type pos = info.find("\n");
121+
if (info.compare(0,8,"$symbol:") == 0 && info.find('\n') < info.size()) {
122+
const std::string::size_type pos = info.find('\n');
123123
const std::string &symbolName = info.substr(8, pos - 8);
124124
info = replaceStr(info.substr(pos+1), "$symbol", symbolName);
125125
}
@@ -361,7 +361,7 @@ void ErrorMessage::deserialize(const std::string &data)
361361
break;
362362
}
363363
const std::string::size_type start = pos;
364-
pos = temp.find("\t", pos);
364+
pos = temp.find('\t', pos);
365365
if (pos == std::string::npos) {
366366
substrings.push_back(temp.substr(start));
367367
break;

lib/fwdanalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
264264
// TODO: This is a quick bailout to avoid FP #9420, there are false negatives (TODO_ASSERT_EQUALS)
265265
return Result(Result::Type::BAILOUT);
266266

267-
if (expr->isName() && Token::Match(tok, "%name% (") && tok->str().find("<") != std::string::npos && tok->str().find(expr->str()) != std::string::npos)
267+
if (expr->isName() && Token::Match(tok, "%name% (") && tok->str().find('<') != std::string::npos && tok->str().find(expr->str()) != std::string::npos)
268268
return Result(Result::Type::BAILOUT);
269269

270270
if (exprVarIds.find(tok->varId()) != exprVarIds.end()) {

lib/importproject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ bool ImportProject::importCompileCommands(std::istream &istr)
393393
for (const picojson::value& arg : obj["arguments"].get<picojson::array>()) {
394394
if (arg.is<std::string>()) {
395395
std::string str = arg.get<std::string>();
396-
if (str.find(" ") != std::string::npos)
396+
if (str.find(' ') != std::string::npos)
397397
str = "\"" + str + "\"";
398398
command += str + " ";
399399
}

0 commit comments

Comments
 (0)