Add MA0002.report_only_non_ordinal option - #1283
Merged
Merged
Conversation
MA0002 reports whenever a string-keyed collection or LINQ operation has an IEqualityComparer<string>/IComparer<string> overload but none is supplied, even when the default comparison is already ordinal (and therefore safe). Add an opt-in `MA0002.report_only_non_ordinal` option (default false, so behavior is unchanged). When enabled, diagnostics are suppressed for a curated, exhaustive list of known-ordinal (equality-default) APIs while culture-sensitive ordering APIs (SortedDictionary, OrderBy, ...) keep being reported. Known-ordinal types: HashSet, Dictionary, ConcurrentDictionary, OrderedDictionary, ImmutableDictionary, ImmutableHashSet, FrozenDictionary, FrozenSet. Known-ordinal methods: LINQ equality methods (Distinct, Contains, GroupBy, ToDictionary, Union, Except, Intersect, Join/LeftJoin/RightJoin, SequenceEqual, CountBy, AggregateBy, *By variants) and immutable/frozen factories (Create, CreateBuilder, CreateRange, ToImmutableDictionary, ToFrozenSet, ...), scoped to their BCL container types so Sorted* factories sharing the same names keep reporting. Meziantou.Framework.Assertions.Assert is folded into the option: previously it was unconditionally excluded; it is now reported by default and suppressed only when the option is enabled.
This was referenced Sep 10, 2026
Open
Closed
Closed
Closed
Closed
Closed
Open
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.235
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#570
Closed
Closed
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new opt-in EditorConfig option
MA0002.report_only_non_ordinal(defaultfalse, so existing behavior is unchanged).MA0002 currently reports whenever a string-keyed collection or LINQ operation has an
IEqualityComparer<string>/IComparer<string>overload but none is supplied — even when the default comparison is already ordinal (and therefore safe), e.g.new HashSet<string>(),new Dictionary<string,int>(),ToDictionary,ToImmutableDictionary.When
MA0002.report_only_non_ordinal = true, the analyzer suppresses diagnostics for a curated, exhaustive list of known-ordinal (equality-default) APIs while still reporting the genuinely risky culture-sensitive ordering APIs (SortedDictionary,SortedSet,SortedList,OrderBy,ThenBy,Order, ...), which default toComparer<string>.Default(=StringComparer.CurrentCulture).Known-ordinal set (suppressed when the option is on)
HashSet,Dictionary,ConcurrentDictionary,OrderedDictionary,ImmutableDictionary,ImmutableHashSet,FrozenDictionary,FrozenSet.Distinct,DistinctBy,Contains,Except,ExceptBy,Intersect,IntersectBy,Union,UnionBy,GroupBy,GroupJoin,Join,LeftJoin,RightJoin,SequenceEqual,ToDictionary,ToHashSet,ToLookup,CountBy,AggregateBy) and immutable/frozen factories (Create,CreateBuilder,CreateRange,CreateRangeWithOverwrite,ToImmutableDictionary,ToImmutableHashSet,ToFrozenDictionary,ToFrozenSet).The method names are scoped to their BCL container types (
Enumerable,Queryable,ImmutableDictionary,ImmutableHashSet,FrozenDictionary,FrozenSet), so theSorted*factories that share names likeCreate/CreateRangekeep reporting. The list was verified by reflecting over the actualnet10.0assemblies rather than by guessing.Because this is an allow-list (only suppress what is known-ordinal), the only failure mode is under-suppression — a culture-sensitive API can never be silently hidden.
Behavior change to note
Meziantou.Framework.Assertions.Assertwas previously unconditionally excluded from MA0002. It is now folded into the option: reported by default and suppressed only whenreport_only_non_ordinal = true. This means Assert methods with a comparer overload start being reported by default.Tests / docs
SortedDictionary,OrderBy,Order,ImmutableSortedDictionary.Create).docs/Rules/MA0002.mdand regenerateddocs/README.md.roslyn4.2/roslyn4.14.