A collection is passed to a set or bulk operation of its own, which is a no-op or has a fixed result
What it finds
Passing a collection to a set operation of its own is a mistake for another collection: a.UnionWith(a) and a.IntersectWith(a) change nothing, a.ExceptWith(a) empties the set, a.IsSubsetOf(a) and a.SequenceEqual(a) are always true, a.Except(a) is always empty, and a.AddRange(a) doubles the list. The receiver and the argument must be the same expression without calls or other effects. Tests, which call these methods on their own receiver on purpose to check the edge case, are left alone.
From the rule's source, self_bulk_operation.rs.
using System.Collections.Generic;using System.Linq; class Sets{ void Merge(HashSet<int> seen, HashSet<int> other, List<int> list) { seen.UnionWith(seen);tx0 reports: seen is passed to its own UnionWith, which changes nothing seen.ExceptWith(seen);tx0 reports: seen is passed to its own ExceptWith, which removes every element bool same = list.SequenceEqual(list);tx0 reports: list is passed to its own SequenceEqual, which is always true list.AddRange(list);tx0 reports: list is passed to its own AddRange, which doubles the collection seen.UnionWith(other); bool both = list.SequenceEqual(other); Next().UnionWith(Next()); } HashSet<int> Next() => new HashSet<int>();} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:self-bulk-operation" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:self-bulk-operation] reason="..."tx0 explain tx0/csharp:self-bulk-operation shows the same in the terminal. Found a false positive? Report it.