A set or dictionary is searched with Contains or ContainsKey and then added to, where one call would do both
What it finds
if (!seen.Contains(x)) seen.Add(x); searches the set twice; seen.Add(x) already returns whether it added. For dictionaries, TryAdd adds only when the key is missing.
From the rule's source, check_then_add.rs.
using System.Collections.Generic; class Seen{ void Mark(HashSet<string> seen, string name) { if (!seen.Contains(name))tx0 reports: the collection is searched twice; the result of Add does both { seen.Add(name); } }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:check-then-add" = "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:check-then-add] reason="..."tx0 explain tx0/csharp:check-then-add shows the same in the terminal. Found a false positive? Report it.