A condition of an if-else chain repeats an earlier one
What it finds
if a {} else if a {}: a condition that repeats an earlier one of the same chain is false whenever it is reached, so its branch never runs. Usually one of the two was meant to test something else.
Between two conditions of a chain only conditions and init statements run. The chain is followed only as long as neither can change what a later condition reads: it ends at an else if with an init statement and at a condition that receives or calls anything but len and cap.
From the rule's source, duplicate_condition.rs.
package p func f(n int, s []int, ok bool, m map[string]int, k string) int { if n < 0 { return -1 } else if n == 0 { return 0 } else if n < 0 {tx0 reports: n < 0 is tested earlier in this chain return 1 } if ok { } else if !ok { } else if (ok) {tx0 reports: ok is tested earlier } if s[0] > 2 { } else if s[1] == 1 { } else if s[0] > 2 {tx0 reports: s[0] > 2 is tested earlier } if m[k] > 0 { } else if m[k] > 0 {tx0 reports: m[k] > 0 is tested earlier } if len(s) > 2 { } else if len(s) > 2 {tx0 reports: len(s) > 2 is tested earlier } if size(s) > 2 { } else if size(s) > 2 { } if n > 1 { } else if next() { } else if n > 1 { } if n > 1 { } else if n = g(); n > 1 { } if v := g(); v > 1 { } else if v > 1 {tx0 reports: v > 1 is tested earlier } if n > 1 { if n > 1 { } } else { if n > 1 { } } if n > 1 { } if n > 1 { } return n} func next() bool { return false } func size(s []int) int { return len(s) } func g() int { return 0 } Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/go:duplicate-condition" = "off"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/go:duplicate-condition] reason="..."tx0 explain tx0/go:duplicate-condition shows the same in the terminal. Found a false positive? Report it.