Skip to content

[MA0002] Raised on immutable set #786

@Pretasoc

Description

@Pretasoc

Version of the Meziantou.Analyzer NuGet package

2.0.187

Rule Identifier

MA0002

Target Framework

net9.0

C# Language version

preview

Description

The rule MA002 seems to take linq provided Contains(...) overloads into account, when it checks, if an equality comparer can be used. In the case of immutable sets, it also suggest manually passing a equality comparer. But that would case the set to be enumerated, so effectively a worse performing overload is suggested.

Reproduction Steps

Minimal code:

#r "nuget: System.Collections.Immutable, 9.0.2"
#r "nuget: Meziantou.Analyzer, 2.0.187"

using System.Collections.Immutable;

IImmutableSet<string> set = ImmutableHashSet.Create<string>(StringComparer.Ordinal);

bool found = [|set.Contains("foo")|];

Other information

Mutable sets do not raise this issue.
Immutable dictionary kind of work, since the contains method has an other signature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions