rust-lang/rust-clippy

if_same_then_else Should not warn for `else if` blocks.

Open

#3.770 geöffnet am 16. Feb. 2019

Auf GitHub ansehen
 (30 Kommentare) (5 Reaktionen) (0 zugewiesene Personen)Rust (1.391 Forks)batch import
I-false-positiveS-needs-discussiongood first issue

Repository-Metriken

Stars
 (10.406 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 19T 22h) (113 gemergte PRs in 30 T)

Beschreibung

View all comments

if condition1() {
  1
} else if condition2() {
  1
} else if condition3() {
  2
} else {
  3
}

There are many cases where code like this shouldn't be warned on. While you could do condition1() || condition2() often times it is more clear of the intent if they are separate. Especially if the conditions are non-trivial compared to the value being returned.

I think the right time for this warning to warn is if the last if has the same condition as the else. For example the following two cases would warn.

if condition1() {
  1
} else {
  1
}
if condition1() {
  1
} else if condition2() {
  2
} else {
  2
}

Contributor Guide