Skip to content

Commit

Permalink
MA0074 excludes string.Contains(string) as this method use Ordinal co…
Browse files Browse the repository at this point in the history
…mparison (#451)
  • Loading branch information
meziantou authored Feb 21, 2023
1 parent f5418d3 commit 72aa43b
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/Meziantou.Analyzer/Rules/UseStringComparisonAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,34 @@ private static bool IsNonCultureSensitiveMethod(IInvocationOperation operation)
if (method == null)
return false;

// string.Equals(string)
if (method.ContainingType.IsString() && method.Name == nameof(string.Equals) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsString())
return true;
if (method.ContainingType.IsString())
{
// string.Equals(string)
if (method.Name == nameof(string.Equals) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsString())
return true;

// string.Equals(string, string)
if (method.ContainingType.IsString() && method.Name == nameof(string.Equals) && method.IsStatic && method.Parameters.Length == 2 && method.Parameters[0].Type.IsString() && method.Parameters[1].Type.IsString())
return true;
// string.Equals(string, string)
if (method.Name == nameof(string.Equals) && method.IsStatic && method.Parameters.Length == 2 && method.Parameters[0].Type.IsString() && method.Parameters[1].Type.IsString())
return true;

// string.IndexOf(char)
if (method.ContainingType.IsString() && method.Name == nameof(string.IndexOf) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;
// string.IndexOf(char)
if (method.Name == nameof(string.IndexOf) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;

// string.EndsWith(char)
if (method.ContainingType.IsString() && method.Name == nameof(string.EndsWith) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;
// string.EndsWith(char)
if (method.Name == nameof(string.EndsWith) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;

// string.StartsWith(char)
if (method.ContainingType.IsString() && method.Name == nameof(string.StartsWith) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;
// string.StartsWith(char)
if (method.Name == nameof(string.StartsWith) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;

// string.Contains(char)
if (method.ContainingType.IsString() && method.Name == nameof(string.Contains) && method.Parameters.Length == 1 && method.Parameters[0].Type.IsChar())
return true;
// string.Contains(char) | string.Contains(string)
if (method.Name == nameof(string.Contains) && method.Parameters.Length == 1 && method.Parameters[0].Type.SpecialType is SpecialType.System_Char or SpecialType.System_String)
return true;

return false;
}

// JObject.Property / TryGetValue / GetValue
var jobjectType = operation.SemanticModel!.Compilation.GetBestTypeByMetadataName("Newtonsoft.Json.Linq.JObject");
Expand Down

0 comments on commit 72aa43b

Please sign in to comment.