A collection is passed to a set or bulk operation of its own, which is a no-op or has a fixed result

tx0/csharp:self-bulk-operationrecommendedwarningreliability/logicdepth fastbug

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.

Example from the rule's tests
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>();}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

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.