Skip to content

Commit 9ea718f

Browse files
authored
Enable CA2249: Consider using String.Contains instead of IndexOf (#14395)
https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2249
1 parent 8315515 commit 9ea718f

File tree

10 files changed

+19
-20
lines changed

10 files changed

+19
-20
lines changed

.globalconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ dotnet_diagnostic.CA2247.severity = warning
468468
dotnet_diagnostic.CA2248.severity = suggestion
469469

470470
# CA2249: Consider using 'string.Contains' instead of 'string.IndexOf'
471-
dotnet_diagnostic.CA2249.severity = suggestion
471+
dotnet_diagnostic.CA2249.severity = warning
472472

473473
# CA2300: Do not use insecure deserializer BinaryFormatter
474474
dotnet_diagnostic.CA2300.severity = none

src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,9 +1989,9 @@ private static string EscapeFunctionNameForRemoteHelp(string name)
19891989
StringBuilder result = new(name.Length);
19901990
foreach (char c in name)
19911991
{
1992-
if (("\"'`$".IndexOf(c) == (-1)) &&
1993-
(!char.IsControl(c)) &&
1994-
(!char.IsWhiteSpace(c)))
1992+
if (!"\"'`$".Contains(c)
1993+
&& !char.IsControl(c)
1994+
&& !char.IsWhiteSpace(c))
19951995
{
19961996
result.Append(c);
19971997
}

src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,9 @@ private static string EscapeModuleNameForHelpComment(string name)
832832
StringBuilder result = new(name.Length);
833833
foreach (char c in name)
834834
{
835-
if (("\"'`$#".IndexOf(c) == (-1)) &&
836-
(!char.IsControl(c)) &&
837-
(!char.IsWhiteSpace(c)))
835+
if (!"\"'`$#".Contains(c)
836+
&& !char.IsControl(c)
837+
&& !char.IsWhiteSpace(c))
838838
{
839839
result.Append(c);
840840
}

src/System.Management.Automation/engine/LanguagePrimitives.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ internal static object GetConverter(Type type, TypeTable backupTypeTable)
15021502

15031503
private static object NewConverterInstance(string assemblyQualifiedTypeName)
15041504
{
1505-
if (assemblyQualifiedTypeName.IndexOf(',') == -1)
1505+
if (!assemblyQualifiedTypeName.Contains(','))
15061506
{
15071507
typeConversion.WriteLine("Type name \"{0}\" should be assembly qualified.", assemblyQualifiedTypeName);
15081508
return null;

src/System.Management.Automation/engine/Modules/RemoteDiscoveryHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,10 @@ private static IEnumerable<CimModule> GetCimModules(
815815
ErrorRecord errorRecord = GetErrorRecordForRemoteDiscoveryProvider(exception);
816816
if (!cmdlet.MyInvocation.ExpectingInput)
817817
{
818-
if ((errorRecord.FullyQualifiedErrorId.IndexOf(DiscoveryProviderNotFoundErrorId, StringComparison.OrdinalIgnoreCase) != (-1)) ||
819-
(cancellationToken.IsCancellationRequested || (exception is OperationCanceledException)) ||
820-
(!cimSession.TestConnection()))
818+
if (errorRecord.FullyQualifiedErrorId.Contains(DiscoveryProviderNotFoundErrorId, StringComparison.OrdinalIgnoreCase)
819+
|| cancellationToken.IsCancellationRequested
820+
|| exception is OperationCanceledException
821+
|| !cimSession.TestConnection())
821822
{
822823
cmdlet.ThrowTerminatingError(errorRecord);
823824
}

src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,10 +1154,9 @@ private static string ResolveShellUri(string shell)
11541154
resolvedShellUri = DefaultShellUri;
11551155
}
11561156

1157-
if (resolvedShellUri.IndexOf(
1158-
System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix, StringComparison.OrdinalIgnoreCase) == -1)
1157+
if (!resolvedShellUri.Contains(WSManNativeApi.ResourceURIPrefix, StringComparison.OrdinalIgnoreCase))
11591158
{
1160-
resolvedShellUri = System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix + resolvedShellUri;
1159+
resolvedShellUri = WSManNativeApi.ResourceURIPrefix + resolvedShellUri;
11611160
}
11621161

11631162
return resolvedShellUri;
@@ -1464,8 +1463,7 @@ internal bool UseDefaultWSManPort
14641463
/// <summary>
14651464
/// Default value for shell.
14661465
/// </summary>
1467-
private const string DefaultShellUri =
1468-
System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix + RemotingConstants.DefaultShellName;
1466+
private const string DefaultShellUri = WSManNativeApi.ResourceURIPrefix + RemotingConstants.DefaultShellName;
14691467

14701468
/// <summary>
14711469
/// Default credentials - null indicates credentials of

src/System.Management.Automation/help/CommandHelpProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ internal override IEnumerable<HelpInfo> SearchHelp(HelpRequest helpRequest, bool
10221022
{
10231023
if (decoratedSearch)
10241024
{
1025-
if (target.IndexOf(StringLiterals.CommandVerbNounSeparator) >= 0)
1025+
if (target.Contains(StringLiterals.CommandVerbNounSeparator))
10261026
{
10271027
patternList.Add(target + "*");
10281028
}

src/System.Management.Automation/help/MUIFileSearcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static string[] GetFiles(string path, string pattern)
127127

128128
foreach (string filePath in files)
129129
{
130-
if (filePath.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
130+
if (filePath.Contains(pattern, StringComparison.OrdinalIgnoreCase))
131131
{
132132
result.Add(filePath);
133133
break;

src/System.Management.Automation/security/SecuritySupport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ internal static AmsiNativeMethods.AMSI_RESULT WinScanContent(string content, str
14061406
const string EICAR_STRING = "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
14071407
if (InternalTestHooks.UseDebugAmsiImplementation)
14081408
{
1409-
if (content.IndexOf(EICAR_STRING, StringComparison.Ordinal) >= 0)
1409+
if (content.Contains(EICAR_STRING, StringComparison.Ordinal))
14101410
{
14111411
return AmsiNativeMethods.AMSI_RESULT.AMSI_RESULT_DETECTED;
14121412
}

src/System.Management.Automation/security/wldpNativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ private static SystemEnforcementMode GetDebugLockdownPolicy(string path)
361361
{
362362
// Assume everything under SYSTEM32 is trusted, with a purposefully sloppy
363363
// check so that we can actually put it in the filename during testing.
364-
if (path.IndexOf("System32", StringComparison.OrdinalIgnoreCase) >= 0)
364+
if (path.Contains("System32", StringComparison.OrdinalIgnoreCase))
365365
{
366366
return SystemEnforcementMode.None;
367367
}

0 commit comments

Comments
 (0)