Enable CA2249: Consider using String.Contains instead of IndexOf (#14395)

https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2249
This commit is contained in:
xtqqczze
2021-01-10 18:15:02 +05:00
committed by GitHub
parent 8315515494
commit 9ea718f171
10 changed files with 19 additions and 20 deletions
+1 -1
View File
@@ -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
@@ -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);
}
@@ -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);
}
@@ -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;
@@ -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);
}
@@ -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
/// <summary>
/// Default value for shell.
/// </summary>
private const string DefaultShellUri =
System.Management.Automation.Remoting.Client.WSManNativeApi.ResourceURIPrefix + RemotingConstants.DefaultShellName;
private const string DefaultShellUri = WSManNativeApi.ResourceURIPrefix + RemotingConstants.DefaultShellName;
/// <summary>
/// Default credentials - null indicates credentials of
@@ -1022,7 +1022,7 @@ namespace System.Management.Automation
{
if (decoratedSearch)
{
if (target.IndexOf(StringLiterals.CommandVerbNounSeparator) >= 0)
if (target.Contains(StringLiterals.CommandVerbNounSeparator))
{
patternList.Add(target + "*");
}
@@ -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;
@@ -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;
}
@@ -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;
}