rjmurillo / EffectiveCSharp.Analyzers

Many of the recommendations in the book Effective C#: 50 Specific Ways to Improve Your C# can be validated by Roslyn-based analyzers and code fixes.
MIT License
2 stars 1 forks source link

False positive: ECS0700 incorrectly detects usage of delegates when no dangerous patterns occur #54

Closed rjmurillo closed 2 months ago

rjmurillo commented 2 months ago

The analyzer should not trigger in the following scenario

public class C
{
  private Func<char, bool> _predicate = null;
  public C()
  {
      _predicate = char.IsLower;
  }

  public bool M(string s)
  {
      return SpanExtensions.All(s.AsSpan(), _predicate);
  }

  public bool N(string s)
  {
      return s.AsSpan().All(_predicate);
  }
}

public static class SpanExtensions
{
  public static bool All(this ReadOnlySpan<char> source, Func<char, bool> predicate)
  {
      for (var i = 0; i < source.Length; i++)
      {
          if (!predicate(source[i]))
          {
              return false;
          }
      }

      return true;
  }
}

The delegate is being used as intended, without being passed around or combined in a manner that would cause issues.