Skip to content
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

Improper use of Count Linq method should be detected when Length property should be used. #539

Open
manfred-brands opened this issue May 1, 2023 · 0 comments

Comments

@manfred-brands
Copy link
Member

The below occurred when changing a variable from a List to an ImmutableArray.
The same error occurs on a normal array (string[])
The Count previously was a property on the List.

[Test]
public void ShouldNotAllowCountLinqMethodOnArray()
{
    ImmutableArray<string> names = GetNames();

    Assert.That(names.Count, Is.EqualTo(1));

    static ImmutableArray<string> GetNames() => ImmutableArray.Create("NUnit");
}

ImmutableArray doesn't have a Count property but a Length one. The above code actually calls the Count() Linq method.

When using Assert.That(names.Count(), Is.EqualTo(1)); roslyn raises CA1829 to use the Length property.
Roslyn doesn't know that the above example will evaluate the method ActualValueDelegate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant