From 9ea718f1719e54e81ae8c704f9b1bd2357ad90bc Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Sun, 10 Jan 2021 13:15:02 +0000
Subject: [PATCH] Enable CA2249: Consider using String.Contains instead of
IndexOf (#14395)
https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2249
---
.globalconfig | 2 +-
.../commands/utility/ImplicitRemotingCommands.cs | 6 +++---
.../cimSupport/cmdletization/ScriptWriter.cs | 6 +++---
.../engine/LanguagePrimitives.cs | 2 +-
.../engine/Modules/RemoteDiscoveryHelper.cs | 7 ++++---
.../engine/remoting/common/RunspaceConnectionInfo.cs | 8 +++-----
.../help/CommandHelpProvider.cs | 2 +-
src/System.Management.Automation/help/MUIFileSearcher.cs | 2 +-
.../security/SecuritySupport.cs | 2 +-
.../security/wldpNativeMethods.cs | 2 +-
10 files changed, 19 insertions(+), 20 deletions(-)
diff --git a/.globalconfig b/.globalconfig
index f7c9313877..e5cb35160f 100644
--- a/.globalconfig
+++ b/.globalconfig
@@ -468,7 +468,7 @@ dotnet_diagnostic.CA2247.severity = warning
dotnet_diagnostic.CA2248.severity = suggestion
# CA2249: Consider using 'string.Contains' instead of 'string.IndexOf'
-dotnet_diagnostic.CA2249.severity = suggestion
+dotnet_diagnostic.CA2249.severity = warning
# CA2300: Do not use insecure deserializer BinaryFormatter
dotnet_diagnostic.CA2300.severity = none
diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs
index 67d2512855..d772258227 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs
@@ -1989,9 +1989,9 @@ namespace Microsoft.PowerShell.Commands
StringBuilder result = new(name.Length);
foreach (char c in name)
{
- if (("\"'`$".IndexOf(c) == (-1)) &&
- (!char.IsControl(c)) &&
- (!char.IsWhiteSpace(c)))
+ if (!"\"'`$".Contains(c)
+ && !char.IsControl(c)
+ && !char.IsWhiteSpace(c))
{
result.Append(c);
}
diff --git a/src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs b/src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs
index 3197361fae..a663160645 100644
--- a/src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs
+++ b/src/System.Management.Automation/cimSupport/cmdletization/ScriptWriter.cs
@@ -832,9 +832,9 @@ function __cmdletization_BindCommonParameters
StringBuilder result = new(name.Length);
foreach (char c in name)
{
- if (("\"'`$#".IndexOf(c) == (-1)) &&
- (!char.IsControl(c)) &&
- (!char.IsWhiteSpace(c)))
+ if (!"\"'`$#".Contains(c)
+ && !char.IsControl(c)
+ && !char.IsWhiteSpace(c))
{
result.Append(c);
}
diff --git a/src/System.Management.Automation/engine/LanguagePrimitives.cs b/src/System.Management.Automation/engine/LanguagePrimitives.cs
index aeb5d5a2bc..293dbb6dce 100644
--- a/src/System.Management.Automation/engine/LanguagePrimitives.cs
+++ b/src/System.Management.Automation/engine/LanguagePrimitives.cs
@@ -1502,7 +1502,7 @@ namespace System.Management.Automation
private static object NewConverterInstance(string assemblyQualifiedTypeName)
{
- if (assemblyQualifiedTypeName.IndexOf(',') == -1)
+ if (!assemblyQualifiedTypeName.Contains(','))
{
typeConversion.WriteLine("Type name \"{0}\" should be assembly qualified.", assemblyQualifiedTypeName);
return null;
diff --git a/src/System.Management.Automation/engine/Modules/RemoteDiscoveryHelper.cs b/src/System.Management.Automation/engine/Modules/RemoteDiscoveryHelper.cs
index a0039cee17..c52a752c64 100644
--- a/src/System.Management.Automation/engine/Modules/RemoteDiscoveryHelper.cs
+++ b/src/System.Management.Automation/engine/Modules/RemoteDiscoveryHelper.cs
@@ -815,9 +815,10 @@ namespace System.Management.Automation
ErrorRecord errorRecord = GetErrorRecordForRemoteDiscoveryProvider(exception);
if (!cmdlet.MyInvocation.ExpectingInput)
{
- if ((errorRecord.FullyQualifiedErrorId.IndexOf(DiscoveryProviderNotFoundErrorId, StringComparison.OrdinalIgnoreCase) != (-1)) ||
- (cancellationToken.IsCancellationRequested || (exception is OperationCanceledException)) ||
- (!cimSession.TestConnection()))
+ if (errorRecord.FullyQualifiedErrorId.Contains(DiscoveryProviderNotFoundErrorId, StringComparison.OrdinalIgnoreCase)
+ || cancellationToken.IsCancellationRequested
+ || exception is OperationCanceledException
+ || !cimSession.TestConnection())
{
cmdlet.ThrowTerminatingError(errorRecord);
}
diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs
index 548620b8aa..ea8a9c1a9d 100644
--- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs
+++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs
@@ -1154,10 +1154,9 @@ namespace System.Management.Automation.Runspaces
resolvedShellUri = DefaultShellUri;
}
- if (resolvedShellUri.IndexOf(
- System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix, StringComparison.OrdinalIgnoreCase) == -1)
+ if (!resolvedShellUri.Contains(WSManNativeApi.ResourceURIPrefix, StringComparison.OrdinalIgnoreCase))
{
- resolvedShellUri = System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix + resolvedShellUri;
+ resolvedShellUri = WSManNativeApi.ResourceURIPrefix + resolvedShellUri;
}
return resolvedShellUri;
@@ -1464,8 +1463,7 @@ namespace System.Management.Automation.Runspaces
///
/// Default value for shell.
///
- private const string DefaultShellUri =
- System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix + RemotingConstants.DefaultShellName;
+ private const string DefaultShellUri = WSManNativeApi.ResourceURIPrefix + RemotingConstants.DefaultShellName;
///
/// Default credentials - null indicates credentials of
diff --git a/src/System.Management.Automation/help/CommandHelpProvider.cs b/src/System.Management.Automation/help/CommandHelpProvider.cs
index 05a4e48af7..d5863f3181 100644
--- a/src/System.Management.Automation/help/CommandHelpProvider.cs
+++ b/src/System.Management.Automation/help/CommandHelpProvider.cs
@@ -1022,7 +1022,7 @@ namespace System.Management.Automation
{
if (decoratedSearch)
{
- if (target.IndexOf(StringLiterals.CommandVerbNounSeparator) >= 0)
+ if (target.Contains(StringLiterals.CommandVerbNounSeparator))
{
patternList.Add(target + "*");
}
diff --git a/src/System.Management.Automation/help/MUIFileSearcher.cs b/src/System.Management.Automation/help/MUIFileSearcher.cs
index bf6244fe3e..d6d3313b68 100644
--- a/src/System.Management.Automation/help/MUIFileSearcher.cs
+++ b/src/System.Management.Automation/help/MUIFileSearcher.cs
@@ -127,7 +127,7 @@ namespace System.Management.Automation
foreach (string filePath in files)
{
- if (filePath.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
+ if (filePath.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
result.Add(filePath);
break;
diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs
index dba2441413..4afb15be55 100644
--- a/src/System.Management.Automation/security/SecuritySupport.cs
+++ b/src/System.Management.Automation/security/SecuritySupport.cs
@@ -1406,7 +1406,7 @@ namespace System.Management.Automation
const string EICAR_STRING = "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
if (InternalTestHooks.UseDebugAmsiImplementation)
{
- if (content.IndexOf(EICAR_STRING, StringComparison.Ordinal) >= 0)
+ if (content.Contains(EICAR_STRING, StringComparison.Ordinal))
{
return AmsiNativeMethods.AMSI_RESULT.AMSI_RESULT_DETECTED;
}
diff --git a/src/System.Management.Automation/security/wldpNativeMethods.cs b/src/System.Management.Automation/security/wldpNativeMethods.cs
index 04e73079ca..3e0edc58fd 100644
--- a/src/System.Management.Automation/security/wldpNativeMethods.cs
+++ b/src/System.Management.Automation/security/wldpNativeMethods.cs
@@ -361,7 +361,7 @@ namespace System.Management.Automation.Security
{
// Assume everything under SYSTEM32 is trusted, with a purposefully sloppy
// check so that we can actually put it in the filename during testing.
- if (path.IndexOf("System32", StringComparison.OrdinalIgnoreCase) >= 0)
+ if (path.Contains("System32", StringComparison.OrdinalIgnoreCase))
{
return SystemEnforcementMode.None;
}