Skip to content

Commit caf1a55

Browse files
Merge pull request #35229 from mininny/switch-find-to-contains
[NFC] Replace uses of find(x) != end() idiom with contains(x) for StringRef and Set-like types
2 parents 7fe3187 + 53d1fc4 commit caf1a55

File tree

27 files changed

+50
-59
lines changed

27 files changed

+50
-59
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ struct CommonDiffItem: public APIDiffItem {
162162

163163
bool rightCommentUnderscored() const {
164164
DeclNameViewer Viewer(RightComment);
165-
auto HasUnderScore =
166-
[](StringRef S) { return S.find('_') != StringRef::npos; };
165+
auto HasUnderScore = [](StringRef S) { return S.contains('_'); };
167166
auto Args = Viewer.args();
168167
return HasUnderScore(Viewer.base()) ||
169168
std::any_of(Args.begin(), Args.end(), HasUnderScore);

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ class swift::MemberLookupTable {
10591059
/// Returns \c true if the lookup table has a complete accounting of the
10601060
/// given name.
10611061
bool isLazilyComplete(DeclBaseName name) const {
1062-
return LazilyCompleteNames.find(name) != LazilyCompleteNames.end();
1062+
return LazilyCompleteNames.contains(name);
10631063
}
10641064

10651065
/// Mark a given lazily-loaded name as being complete.

lib/ClangImporter/ImportMacro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
8585
Impl.getClangPreprocessor().getSpelling(tok, SpellingBuffer, &Invalid);
8686
if (Invalid)
8787
return nullptr;
88-
if (TokSpelling.find('_') != StringRef::npos)
88+
if (TokSpelling.contains('_'))
8989
return nullptr;
9090
}
9191

lib/Driver/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static void validateCompilationConditionArgs(DiagnosticEngine &diags,
224224
const ArgList &args) {
225225
for (const Arg *A : args.filtered(options::OPT_D)) {
226226
StringRef name = A->getValue();
227-
if (name.find('=') != StringRef::npos) {
227+
if (name.contains('=')) {
228228
diags.diagnose(SourceLoc(),
229229
diag::cannot_assign_value_to_conditional_compilation_flag,
230230
name);
@@ -660,7 +660,7 @@ static SmallVector<StringRef, 8> findRemovedInputs(
660660
SmallVector<StringRef, 8> missingInputs;
661661
for (auto &previousInput : previousInputs) {
662662
auto previousInputArg = previousInput.getKey();
663-
if (inputArgs.find(previousInputArg) == inputArgs.end()) {
663+
if (!inputArgs.contains(previousInputArg)) {
664664
missingInputs.push_back(previousInputArg);
665665
}
666666
}

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ static void annotateSnippetWithInfo(SourceManager &SM,
911911
for (auto fixIt : Info.FixIts) {
912912
// Don't print multi-line fix-its inline, only include them at the end of
913913
// the message.
914-
if (fixIt.getText().find("\n") == std::string::npos)
914+
if (!fixIt.getText().contains("\n"))
915915
Snippet.addFixIt(fixIt.getRange(), fixIt.getText());
916916
}
917917
}

lib/FrontendTool/TBD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static bool validateSymbols(DiagnosticEngine &diags,
9393
irNotTBD.push_back(unmangledName);
9494
}
9595
} else {
96-
assert(symbolSet.find(name) == symbolSet.end() &&
96+
assert(!symbolSet.contains(name) &&
9797
"non-global value in value symbol table");
9898
}
9999
}

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ getDeclsFromCrossImportOverlay(ModuleDecl *Overlay, ModuleDecl *Declaring,
424424
return false;
425425

426426
// Ignore an imports of modules also imported by the underlying module.
427-
if (PrevImported.find(Imported) != PrevImported.end())
427+
if (PrevImported.contains(Imported))
428428
return false;
429429
}
430430
if (auto *VD = dyn_cast<ValueDecl>(D)) {

lib/IDE/SyntaxModel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,8 +1620,7 @@ class DocFieldParser {
16201620

16211621
public:
16221622
DocFieldParser(StringRef text) : ptr(text.begin()), end(text.end()) {
1623-
assert(text.rtrim().find('\n') == StringRef::npos &&
1624-
"expected single line");
1623+
assert(!text.rtrim().contains('\n') && "expected single line");
16251624
}
16261625

16271626
// Case-insensitively match one of the following patterns:

lib/IDE/Utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,8 @@ void ide::collectModuleNames(StringRef SDKPath,
921921
std::vector<std::string> &Modules) {
922922
std::string SDKName = getSDKName(SDKPath);
923923
std::string lowerSDKName = StringRef(SDKName).lower();
924-
bool isOSXSDK = StringRef(lowerSDKName).find("macosx") != StringRef::npos;
925-
bool isDeviceOnly = StringRef(lowerSDKName).find("iphoneos") != StringRef::npos;
924+
bool isOSXSDK = StringRef(lowerSDKName).contains("macosx");
925+
bool isDeviceOnly = StringRef(lowerSDKName).contains("iphoneos");
926926
auto Mods = isOSXSDK ? getOSXModuleList() : getiOSModuleList();
927927
Modules.insert(Modules.end(), Mods.begin(), Mods.end());
928928
if (isDeviceOnly) {

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ llvm::SmallString<32> getTargetDependentLibraryOption(const llvm::Triple &T,
11631163
llvm::SmallString<32> buffer;
11641164

11651165
if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
1166-
bool quote = library.find(' ') != StringRef::npos;
1166+
bool quote = library.contains(' ');
11671167

11681168
buffer += "/DEFAULTLIB:";
11691169
if (quote)
@@ -1174,7 +1174,7 @@ llvm::SmallString<32> getTargetDependentLibraryOption(const llvm::Triple &T,
11741174
if (quote)
11751175
buffer += '"';
11761176
} else if (T.isPS4()) {
1177-
bool quote = library.find(' ') != StringRef::npos;
1177+
bool quote = library.contains(' ');
11781178

11791179
buffer += "\01";
11801180
if (quote)

lib/LLVMPasses/ARCEntryPointBuilder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ class ARCEntryPointBuilder {
193193
// intrinsics are atomic today.
194194
if (I->getIntrinsicID() != llvm::Intrinsic::not_intrinsic)
195195
return false;
196-
return (I->getCalledFunction()->getName().find("nonatomic") !=
197-
llvm::StringRef::npos);
196+
return (I->getCalledFunction()->getName().contains("nonatomic"));
198197
}
199198

200199
bool isAtomic(CallInst *I) {

lib/LLVMPasses/LLVMARCOpts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ static bool canonicalizeInputFunction(Function &F, ARCEntryPointBuilder &B,
142142
// Have not encountered a strong retain/release. keep it in the
143143
// unknown retain/release list for now. It might get replaced
144144
// later.
145-
if (NativeRefs.find(ArgVal) == NativeRefs.end()) {
146-
UnknownObjectRetains[ArgVal].push_back(&CI);
145+
if (!NativeRefs.contains(ArgVal)) {
146+
UnknownObjectRetains[ArgVal].push_back(&CI);
147147
} else {
148148
B.setInsertPoint(&CI);
149149
B.createRetain(ArgVal, &CI);
@@ -189,7 +189,7 @@ static bool canonicalizeInputFunction(Function &F, ARCEntryPointBuilder &B,
189189
// Have not encountered a strong retain/release. keep it in the
190190
// unknown retain/release list for now. It might get replaced
191191
// later.
192-
if (NativeRefs.find(ArgVal) == NativeRefs.end()) {
192+
if (!NativeRefs.contains(ArgVal)) {
193193
UnknownObjectReleases[ArgVal].push_back(&CI);
194194
} else {
195195
B.setInsertPoint(&CI);

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
13581358
llvm::raw_svector_ostream OS(Buffer);
13591359
if (swift::ide::printValueDeclUSR(OD, OS))
13601360
return SourceLoc();
1361-
return OverridingRemoveNames.find(OS.str()) == OverridingRemoveNames.end() ?
1362-
SourceLoc() : OverrideLoc;
1361+
return OverridingRemoveNames.contains(OS.str()) ? OverrideLoc : SourceLoc();
13631362
}
13641363

13651364
struct SuperRemoval: public ASTWalker {
@@ -1380,7 +1379,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
13801379
auto *RD = DSC->getFn()->getReferencedDecl().getDecl();
13811380
if (swift::ide::printValueDeclUSR(RD, OS))
13821381
return false;
1383-
return USRs.find(OS.str()) != USRs.end();
1382+
return USRs.contains(OS.str());
13841383
}
13851384
}
13861385
// We should handle try super.foo() too.

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static bool isClangKeyword(Identifier name) {
7171

7272
if (name.empty())
7373
return false;
74-
return keywords.find(name.str()) != keywords.end();
74+
return keywords.contains(name.str());
7575
}
7676

7777

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ findMatchingRetains(SILBasicBlock *BB) {
697697
SA = nullptr;
698698

699699
for (auto X : R.first->getPredecessorBlocks()) {
700-
if (HandledBBs.find(X) != HandledBBs.end())
700+
if (HandledBBs.contains(X))
701701
continue;
702702
// Try to use the predecessor edge-value.
703703
if (SA && SA->getIncomingPhiValue(X)) {

lib/SILOptimizer/Analysis/EpilogueARCAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void EpilogueARCContext::initializeDataflow() {
4040
SILValue CArg = ToProcess.pop_back_val();
4141
if (!CArg)
4242
continue;
43-
if (Processed.find(CArg) != Processed.end())
44-
continue;
43+
if (Processed.contains(CArg))
44+
continue;
4545
Processed.insert(CArg);
4646
if (auto *A = dyn_cast<SILPhiArgument>(CArg)) {
4747
// Find predecessor and break the SILArgument to predecessors.

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static void removeToken(SILValue Op) {
236236
if (!(GAI->use_empty() || GAI->hasOneUse()))
237237
return;
238238
// If it is not a *_token global variable, bail.
239-
if (!Global || Global->getName().find("_token") == StringRef::npos)
239+
if (!Global || !Global->getName().contains("_token"))
240240
return;
241241
GAI->getModule().eraseGlobalVariable(Global);
242242
GAI->replaceAllUsesWithUndef();

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,11 @@ static bool doPrintBefore(SILTransform *T, SILFunction *F) {
163163
return false;
164164

165165
if (!SILPrintOnlyFuns.empty() && F &&
166-
F->getName().find(SILPrintOnlyFuns, 0) == StringRef::npos)
166+
!F->getName().contains(SILPrintOnlyFuns))
167167
return false;
168168

169169
auto MatchFun = [&](const std::string &Str) -> bool {
170-
return T->getTag().find(Str) != StringRef::npos
171-
|| T->getID().find(Str) != StringRef::npos;
170+
return T->getTag().contains(Str) || T->getID().contains(Str);
172171
};
173172

174173
if (SILPrintBefore.end() !=
@@ -188,12 +187,11 @@ static bool doPrintAfter(SILTransform *T, SILFunction *F, bool Default) {
188187
return false;
189188

190189
if (!SILPrintOnlyFuns.empty() && F &&
191-
F->getName().find(SILPrintOnlyFuns, 0) == StringRef::npos)
190+
!F->getName().contains(SILPrintOnlyFuns))
192191
return false;
193192

194193
auto MatchFun = [&](const std::string &Str) -> bool {
195-
return T->getTag().find(Str) != StringRef::npos
196-
|| T->getID().find(Str) != StringRef::npos;
194+
return T->getTag().contains(Str) || T->getID().contains(Str);
197195
};
198196

199197
if (SILPrintAfter.end() !=
@@ -215,8 +213,7 @@ static bool isDisabled(SILTransform *T, SILFunction *F = nullptr) {
215213
return false;
216214

217215
for (const std::string &NamePattern : SILDisablePass) {
218-
if (T->getTag().find(NamePattern) != StringRef::npos
219-
|| T->getID().find(NamePattern) != StringRef::npos) {
216+
if (T->getTag().contains(NamePattern) || T->getID().contains(NamePattern)) {
220217
return true;
221218
}
222219
}
@@ -233,8 +230,7 @@ static void printModule(SILModule *Mod, bool EmitVerboseSIL) {
233230
std::find(SILPrintOnlyFun.begin(), SILPrintOnlyFun.end(), F.getName()))
234231
F.dump(EmitVerboseSIL);
235232

236-
if (!SILPrintOnlyFuns.empty() &&
237-
F.getName().find(SILPrintOnlyFuns, 0) != StringRef::npos)
233+
if (!SILPrintOnlyFuns.empty() && F.getName().contains(SILPrintOnlyFuns))
238234
F.dump(EmitVerboseSIL);
239235
}
240236
}
@@ -429,8 +425,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
429425
CurrentPassHasInvalidated = false;
430426

431427
auto MatchFun = [&](const std::string &Str) -> bool {
432-
return SFT->getTag().find(Str) != StringRef::npos ||
433-
SFT->getID().find(Str) != StringRef::npos;
428+
return SFT->getTag().contains(Str) || SFT->getID().contains(Str);
434429
};
435430
if ((SILVerifyBeforePass.end() != std::find_if(SILVerifyBeforePass.begin(),
436431
SILVerifyBeforePass.end(),
@@ -597,8 +592,7 @@ void SILPassManager::runModulePass(unsigned TransIdx) {
597592
}
598593

599594
auto MatchFun = [&](const std::string &Str) -> bool {
600-
return SMT->getTag().find(Str) != StringRef::npos ||
601-
SMT->getID().find(Str) != StringRef::npos;
595+
return SMT->getTag().contains(Str) || SMT->getID().contains(Str);
602596
};
603597
if ((SILVerifyBeforePass.end() != std::find_if(SILVerifyBeforePass.begin(),
604598
SILVerifyBeforePass.end(),

lib/SILOptimizer/Transforms/Outliner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ class Outliner : public SILFunctionTransform {
12901290

12911291
// Dump function if requested.
12921292
if (DumpFuncsBeforeOutliner.size() &&
1293-
Fun->getName().find(DumpFuncsBeforeOutliner, 0) != StringRef::npos) {
1293+
Fun->getName().contains(DumpFuncsBeforeOutliner)) {
12941294
Fun->dump();
12951295
}
12961296

lib/SILOptimizer/UtilityPasses/CFGPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SILCFGPrinter : public SILFunctionTransform {
5353
if (!SILViewCFGOnlyFun.empty() && F && F->getName() != SILViewCFGOnlyFun)
5454
return;
5555
if (!SILViewCFGOnlyFuns.empty() && F &&
56-
F->getName().find(SILViewCFGOnlyFuns, 0) == StringRef::npos)
56+
!F->getName().contains(SILViewCFGOnlyFuns))
5757
return;
5858

5959
F->viewCFG();

lib/SILOptimizer/UtilityPasses/LoopRegionPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class LoopRegionViewText : public SILModuleTransform {
5050
if (!SILViewCFGOnlyFun.empty() && fn.getName() != SILViewCFGOnlyFun)
5151
continue;
5252
if (!SILViewCFGOnlyFuns.empty() &&
53-
fn.getName().find(SILViewCFGOnlyFuns, 0) == StringRef::npos)
53+
!fn.getName().contains(SILViewCFGOnlyFuns))
5454
continue;
5555

5656
// Ok, we are going to analyze this function. Invalidate all state
@@ -74,7 +74,7 @@ class LoopRegionViewCFG : public SILModuleTransform {
7474
if (!SILViewCFGOnlyFun.empty() && fn.getName() != SILViewCFGOnlyFun)
7575
continue;
7676
if (!SILViewCFGOnlyFuns.empty() &&
77-
fn.getName().find(SILViewCFGOnlyFuns, 0) == StringRef::npos)
77+
!fn.getName().contains(SILViewCFGOnlyFuns))
7878
continue;
7979

8080
// Ok, we are going to analyze this function. Invalidate all state

lib/SILOptimizer/Utils/PerformanceInlinerUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,8 @@ SILFunction *swift::getEligibleFunction(FullApplySite AI,
790790
return nullptr;
791791
}
792792

793-
if (!SILInlineNeverFuns.empty()
794-
&& Callee->getName().find(SILInlineNeverFuns, 0) != StringRef::npos)
793+
if (!SILInlineNeverFuns.empty() &&
794+
Callee->getName().contains(SILInlineNeverFuns))
795795
return nullptr;
796796

797797
if (!SILInlineNeverFun.empty() &&

tools/SourceKit/lib/Support/UIDRegistry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void UIdent::print(llvm::raw_ostream &OS) const {
8585

8686
void *UIDRegistryImpl::get(StringRef Str) {
8787
assert(!Str.empty());
88-
assert(Str.find(' ') == StringRef::npos);
88+
assert(!Str.contains(' '));
8989
EntryTy *Ptr = 0;
9090
Queue.dispatchSync([&]{
9191
HashTableTy::iterator It = HashTable.find(Str);

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ sourcekitd_object_t YAMLRequestParser::createObjFromNode(
721721
if (!Raw.getAsInteger(10, val))
722722
return sourcekitd_request_int64_create(val);
723723

724-
if (Raw.find(' ') != StringRef::npos)
724+
if (Raw.contains(' '))
725725
return withError("Found space in non-string value", Value, Error);
726726

727727
return sourcekitd_request_uid_create(

tools/driver/driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ static bool shouldRunAsSubcommand(StringRef ExecName,
104104
// Otherwise, we have a program argument. If it looks like an option or a
105105
// path, then invoke in interactive mode with the arguments as given.
106106
StringRef FirstArg(Args[1]);
107-
if (FirstArg.startswith("-") || FirstArg.find('.') != StringRef::npos ||
108-
FirstArg.find('/') != StringRef::npos)
107+
if (FirstArg.startswith("-") || FirstArg.contains('.') ||
108+
FirstArg.contains('/'))
109109
return false;
110110

111111
// Otherwise, we should have some sort of subcommand. Get the subcommand name

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,8 +2291,7 @@ int swift::ide::api::findDeclUsr(StringRef dumpPath, CheckerOptions Opts) {
22912291
FinderByLocation(StringRef Location): Location(Location) {}
22922292
void visit(SDKNode* Node) override {
22932293
if (auto *D = dyn_cast<SDKNodeDecl>(Node)) {
2294-
if (D->getLocation().find(Location) != StringRef::npos &&
2295-
!D->getUsr().empty()) {
2294+
if (D->getLocation().contains(Location) && !D->getUsr().empty()) {
22962295
llvm::outs() << D->getFullyQualifiedName() << ": " << D->getUsr() << "\n";
22972296
}
22982297
}

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,11 +2509,13 @@ static int generateMigrationScript(StringRef LeftPath, StringRef RightPath,
25092509
TypeAliasDiffFinder(RightModule, LeftModule, RevertAliasMap).search();
25102510
populateAliasChanges(RevertAliasMap, AllItems, /*IsRevert*/true);
25112511

2512-
AllItems.erase(std::remove_if(AllItems.begin(), AllItems.end(),
2513-
[&](CommonDiffItem &Item) {
2514-
return Item.DiffKind == NodeAnnotation::RemovedDecl &&
2515-
IgnoredRemoveUsrs.find(Item.LeftUsr) != IgnoredRemoveUsrs.end();
2516-
}), AllItems.end());
2512+
AllItems.erase(
2513+
std::remove_if(AllItems.begin(), AllItems.end(),
2514+
[&](CommonDiffItem &Item) {
2515+
return Item.DiffKind == NodeAnnotation::RemovedDecl &&
2516+
IgnoredRemoveUsrs.contains(Item.LeftUsr);
2517+
}),
2518+
AllItems.end());
25172519

25182520
NoEscapeFuncParamVector AllNoEscapingFuncs;
25192521
NoEscapingFuncEmitter::collectDiffItems(RightModule, AllNoEscapingFuncs);

0 commit comments

Comments
 (0)