Skip to content

Refactor : Enable CA2249 #40009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ dotnet_diagnostic.CA2245.severity = warning
# CA2246: Assigning symbol and its member in the same statement
dotnet_diagnostic.CA2246.severity = warning

# CA2249: Use string.Contains instead of string.IndexOf to improve readability.
dotnet_diagnostic.CA2249.severity = warning

# IDE0005: Remove unnecessary usings
dotnet_diagnostic.IDE0005.severity = warning

Expand Down Expand Up @@ -297,6 +300,8 @@ dotnet_diagnostic.CA1847.severity = suggestion
dotnet_diagnostic.CA2008.severity = suggestion
# CA2012: Use ValueTask correctly
dotnet_diagnostic.CA2012.severity = suggestion
# CA2249: Use string.Contains instead of string.IndexOf to improve readability.
dotnet_diagnostic.CA2249.severity = suggestion
# IDE0005: Remove unnecessary usings
dotnet_diagnostic.IDE0005.severity = suggestion
# IDE0044: Make field readonly
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Http.Abstractions/src/HostString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HostString(string host, int port)
}

int index;
if (host.IndexOf('[') == -1
if (!host.Contains('[')
&& (index = host.IndexOf(':')) >= 0
&& index < host.Length - 1
&& host.IndexOf(':', index + 1) >= 0)
Expand Down Expand Up @@ -162,7 +162,7 @@ public static HostString FromUriComponent(string uriComponent)
if (!string.IsNullOrEmpty(uriComponent))
{
int index;
if (uriComponent.IndexOf('[') >= 0)
if (uriComponent.Contains('['))
{
// IPv6 in brackets [::1], maybe with port
}
Expand All @@ -172,7 +172,7 @@ public static HostString FromUriComponent(string uriComponent)
{
// IPv6 without brackets ::1 is the only type of host with 2 or more colons
}
else if (uriComponent.IndexOf("xn--", StringComparison.Ordinal) >= 0)
else if (uriComponent.Contains("xn--", StringComparison.Ordinal))
{
// Contains punycode
if (index >= 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Routing/src/Patterns/RoutePatternFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public static RoutePatternLiteralPart LiteralPart(string content)
throw new ArgumentException(Resources.Argument_NullOrEmpty, nameof(content));
}

if (content.IndexOf('?') >= 0)
if (content.Contains('?'))
{
throw new ArgumentException(Resources.FormatTemplateRoute_InvalidLiteral(content));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/Rewrite/src/RewriteRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public virtual void ApplyRule(RewriteContext context)
result = "/";
}

if (result.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) >= 0)
if (result.Contains(Uri.SchemeDelimiter, StringComparison.Ordinal))
{
string scheme;
HostString host;
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/Rewrite/src/UrlActions/RedirectAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void ApplyAction(RewriteContext context, BackReferenceCollection
return;
}

if (pattern.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) == -1 && pattern[0] != '/')
if (!pattern.Contains(Uri.SchemeDelimiter, StringComparison.Ordinal) && pattern[0] != '/')
{
pattern = '/' + pattern;
}
Expand All @@ -52,6 +52,7 @@ public override void ApplyAction(RewriteContext context, BackReferenceCollection
// url can either contain the full url or the path and query
// always add to location header.
// TODO check for false positives

var split = pattern.IndexOf('?');
if (split >= 0 && QueryStringAppend)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/Rewrite/src/UrlActions/RewriteAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override void ApplyAction(RewriteContext context, BackReferenceCollection
}

// TODO PERF, substrings, object creation, etc.
if (pattern.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) >= 0)
if (pattern.Contains(Uri.SchemeDelimiter, StringComparison.Ordinal))
{
string scheme;
HostString host;
Expand Down
4 changes: 2 additions & 2 deletions src/Mvc/Mvc.TagHelpers/src/TagHelperOutputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static void AddClass(

var encodedSpaceChars = SpaceChars.Where(x => !x.Equals('\u0020')).Select(x => htmlEncoder.Encode(x.ToString())).ToArray();

if (SpaceChars.Any(classValue.Contains) || encodedSpaceChars.Any(value => classValue.IndexOf(value, StringComparison.Ordinal) >= 0))
if (SpaceChars.Any(classValue.Contains) || encodedSpaceChars.Any(value => classValue.Contains(value, StringComparison.Ordinal)))
{
throw new ArgumentException(Resources.ArgumentCannotContainHtmlSpace, nameof(classValue));
}
Expand Down Expand Up @@ -233,7 +233,7 @@ public static void RemoveClass(

var encodedSpaceChars = SpaceChars.Where(x => !x.Equals('\u0020')).Select(x => htmlEncoder.Encode(x.ToString())).ToArray();

if (SpaceChars.Any(classValue.Contains) || encodedSpaceChars.Any(value => classValue.IndexOf(value, StringComparison.Ordinal) >= 0))
if (SpaceChars.Any(classValue.Contains) || encodedSpaceChars.Any(value => classValue.Contains(value, StringComparison.Ordinal)))
{
throw new ArgumentException(Resources.ArgumentCannotContainHtmlSpace, nameof(classValue));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/RangeHelper/RangeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static (bool isRangeRequest, RangeItemHeaderValue? range) ParseRange(
}

// Perf: Check for a single entry before parsing it
if (rawRangeHeader.Count > 1 || (rawRangeHeader[0] ?? string.Empty).IndexOf(',') >= 0)
if (rawRangeHeader.Count > 1 || (rawRangeHeader[0] ?? string.Empty).Contains(','))
{
logger.LogDebug("Multiple ranges are not supported.");

Expand Down