Skip to content

Commit 0abb5d2

Browse files
[Sema, StaticAnalyzer] Use StringRef::contains (NFC)
1 parent fe1f0de commit 0abb5d2

File tree

10 files changed

+17
-21
lines changed

10 files changed

+17
-21
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9773,8 +9773,7 @@ static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr,
97739773

97749774
// Emit a warning if the string literal is truncated and does not contain an
97759775
// embedded null character.
9776-
if (TypeSize <= StrRef.size() &&
9777-
StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) {
9776+
if (TypeSize <= StrRef.size() && !StrRef.substr(0, TypeSize).contains('\0')) {
97789777
CheckFormatHandler::EmitFormatDiagnostic(
97799778
S, inFunctionCall, Args[format_idx],
97809779
S.PDiag(diag::warn_printf_format_string_not_null_terminated),

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,13 +3213,13 @@ static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
32133213
bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
32143214
enum FirstParam { Unsupported, Duplicate, Unknown };
32153215
enum SecondParam { None, Architecture, Tune };
3216-
if (AttrStr.find("fpmath=") != StringRef::npos)
3216+
if (AttrStr.contains("fpmath="))
32173217
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
32183218
<< Unsupported << None << "fpmath=";
32193219

32203220
// Diagnose use of tune if target doesn't support it.
32213221
if (!Context.getTargetInfo().supportsTargetAttributeTune() &&
3222-
AttrStr.find("tune=") != StringRef::npos)
3222+
AttrStr.contains("tune="))
32233223
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
32243224
<< Unsupported << None << "tune=";
32253225

@@ -7570,7 +7570,7 @@ static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
75707570
// C++ for OpenCL 2021 inherits rule from OpenCL C v3.0.
75717571
if (const auto *PDecl = dyn_cast<ParmVarDecl>(D)) {
75727572
const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr();
7573-
if (AL.getAttrName()->getName().find("read_write") != StringRef::npos) {
7573+
if (AL.getAttrName()->getName().contains("read_write")) {
75747574
bool ReadWriteImagesUnsupported =
75757575
(S.getLangOpts().getOpenCLCompatibleVersion() < 200) ||
75767576
(S.getLangOpts().getOpenCLCompatibleVersion() == 300 &&

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12434,8 +12434,7 @@ static void diagnoseXorMisusedAsPow(Sema &S, const ExprResult &XorLHS,
1243412434
RHSStrRef.startswith("0x") || RHSStrRef.startswith("0X") ||
1243512435
(LHSStrRef.size() > 1 && LHSStrRef.startswith("0")) ||
1243612436
(RHSStrRef.size() > 1 && RHSStrRef.startswith("0")) ||
12437-
LHSStrRef.find('\'') != StringRef::npos ||
12438-
RHSStrRef.find('\'') != StringRef::npos)
12437+
LHSStrRef.contains('\'') || RHSStrRef.contains('\''))
1243912438
return;
1244012439

1244112440
bool SuggestXor =

clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,8 @@ void WalkAST::checkDeprecatedOrUnsafeBufferHandling(const CallExpr *CE,
785785
// real flow analysis.
786786
auto FormatString =
787787
dyn_cast<StringLiteral>(CE->getArg(ArgIndex)->IgnoreParenImpCasts());
788-
if (FormatString &&
789-
FormatString->getString().find("%s") == StringRef::npos &&
790-
FormatString->getString().find("%[") == StringRef::npos)
788+
if (FormatString && !FormatString->getString().contains("%s") &&
789+
!FormatString->getString().contains("%["))
791790
BoundsProvided = true;
792791
}
793792

clang/lib/StaticAnalyzer/Checkers/DirectIvarAssignment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ static bool DefaultMethodFilter(const ObjCMethodDecl *M) {
4444
M->getMethodFamily() == OMF_dealloc ||
4545
M->getMethodFamily() == OMF_copy ||
4646
M->getMethodFamily() == OMF_mutableCopy ||
47-
M->getSelector().getNameForSlot(0).find("init") != StringRef::npos ||
48-
M->getSelector().getNameForSlot(0).find("Init") != StringRef::npos;
47+
M->getSelector().getNameForSlot(0).contains("init") ||
48+
M->getSelector().getNameForSlot(0).contains("Init");
4949
}
5050

5151
class DirectIvarAssignment :

clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ bool GenericTaintChecker::isStdin(const Expr *E, CheckerContext &C) {
789789
// variable named stdin with the proper type.
790790
if (const auto *D = dyn_cast_or_null<VarDecl>(DeclReg->getDecl())) {
791791
D = D->getCanonicalDecl();
792-
if ((D->getName().find("stdin") != StringRef::npos) && D->isExternC()) {
792+
if (D->getName().contains("stdin") && D->isExternC()) {
793793
const auto *PtrTy = dyn_cast<PointerType>(D->getType().getTypePtr());
794794
if (PtrTy && PtrTy->getPointeeType().getCanonicalType() ==
795795
C.getASTContext().getFILEType().getCanonicalType())
@@ -816,7 +816,7 @@ static bool getPrintfFormatArgumentNum(const CallEvent &Call,
816816
}
817817

818818
// Or if a function is named setproctitle (this is a heuristic).
819-
if (C.getCalleeName(FDecl).find("setproctitle") != StringRef::npos) {
819+
if (C.getCalleeName(FDecl).contains("setproctitle")) {
820820
ArgNum = 0;
821821
return true;
822822
}

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3195,7 +3195,7 @@ bool MallocChecker::mayFreeAnyEscapedMemoryOrIsModeledExplicitly(
31953195
const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts();
31963196
if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE))
31973197
if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl()))
3198-
if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos)
3198+
if (D->getCanonicalDecl()->getName().contains("std"))
31993199
return true;
32003200
}
32013201
}

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ bool AnyFunctionCall::argumentsMayEscape() const {
681681

682682
// - NSXXInsertXX, for example NSMapInsertIfAbsent, since they can
683683
// be deallocated by NSMapRemove.
684-
if (FName.startswith("NS") && (FName.find("Insert") != StringRef::npos))
684+
if (FName.startswith("NS") && FName.contains("Insert"))
685685
return true;
686686

687687
// - Many CF containers allow objects to escape through custom

clang/lib/StaticAnalyzer/Core/CheckerContext.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
5555
if (Name.empty())
5656
return true;
5757
StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
58-
if (BName.find(Name) != StringRef::npos)
58+
if (BName.contains(Name))
5959
return true;
6060
}
6161

@@ -83,11 +83,10 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
8383
if (FName.equals(Name))
8484
return true;
8585

86-
if (FName.startswith("__inline") && (FName.find(Name) != StringRef::npos))
86+
if (FName.startswith("__inline") && FName.contains(Name))
8787
return true;
8888

89-
if (FName.startswith("__") && FName.endswith("_chk") &&
90-
FName.find(Name) != StringRef::npos)
89+
if (FName.startswith("__") && FName.endswith("_chk") && FName.contains(Name))
9190
return true;
9291

9392
return false;

clang/lib/StaticAnalyzer/Core/MemRegion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
976976
if (Ctx.getSourceManager().isInSystemHeader(D->getLocation())) {
977977
// Whitelist the system globals which often DO GET modified, assume the
978978
// rest are immutable.
979-
if (D->getName().find("errno") != StringRef::npos)
979+
if (D->getName().contains("errno"))
980980
sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
981981
else
982982
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);

0 commit comments

Comments
 (0)