Skip to content

Commit

Permalink
Auto merge of rust-lang#12441 - CBSpeir:dedup-else-if-without-else, r…
Browse files Browse the repository at this point in the history
…=dswij

[`else_if_without_else`]: Fix duplicate diagnostics

Relates to: rust-lang#12379

changelog:  Fix duplicate lint diagnostic emission from [`else_if_without_else`]
  • Loading branch information
bors committed Mar 16, 2024
2 parents c9f2482 + 8d78cd1 commit 59a5ad4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 20 deletions.
26 changes: 12 additions & 14 deletions clippy_lints/src/else_if_without_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,22 @@ declare_clippy_lint! {
declare_lint_pass!(ElseIfWithoutElse => [ELSE_IF_WITHOUT_ELSE]);

impl EarlyLintPass for ElseIfWithoutElse {
fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
if in_external_macro(cx.sess(), item.span) {
return;
}

while let ExprKind::If(_, _, Some(ref els)) = item.kind {
if let ExprKind::If(_, _, None) = els.kind {
span_lint_and_help(
cx,
ELSE_IF_WITHOUT_ELSE,
els.span,
"`if` expression with an `else if`, but without a final `else`",
None,
"add an `else` block here",
);
}

item = els;
if let ExprKind::If(_, _, Some(ref els)) = item.kind
&& let ExprKind::If(_, _, None) = els.kind
{
span_lint_and_help(
cx,
ELSE_IF_WITHOUT_ELSE,
els.span,
"`if` expression with an `else if`, but without a final `else`",
None,
"add an `else` block here",
);
}
}
}
68 changes: 65 additions & 3 deletions tests/ui/else_if_without_else.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![warn(clippy::all)]
#![warn(clippy::else_if_without_else)]
#![allow(clippy::collapsible_else_if)]

fn bla1() -> bool {
unimplemented!()
Expand All @@ -12,6 +10,12 @@ fn bla2() -> bool {
fn bla3() -> bool {
unimplemented!()
}
fn bla4() -> bool {
unimplemented!()
}
fn bla5() -> bool {
unimplemented!()
}

fn main() {
if bla1() {
Expand Down Expand Up @@ -57,4 +61,62 @@ fn main() {
//~^ ERROR: `if` expression with an `else if`, but without a final `else`
println!("else if 2");
}

if bla1() {
println!("if");
} else if bla2() {
println!("else if 1");
} else if bla3() {
println!("else if 2");
} else if bla4() {
println!("else if 3");
} else if bla5() {
println!("else if 4");
} else {
println!("else");
}

if bla1() {
println!("if");
} else if bla2() {
println!("else if 1");
} else if bla3() {
println!("else if 2");
} else if bla4() {
println!("else if 3");
} else if bla5() {
//~^ ERROR: `if` expression with an `else if`, but without a final `else`
println!("else if 4");
}

if bla1() {
println!("if");
} else if bla2() {
println!("else if 1");
} else {
if bla3() {
println!("else if 2");
} else if bla4() {
println!("else if 3");
} else if bla5() {
println!("else if 4");
} else {
println!("else");
}
}

if bla1() {
println!("if");
} else if bla2() {
println!("else if 1");
} else {
if bla3() {
println!("else if 2");
} else if bla4() {
println!("else if 3");
} else if bla5() {
//~^ ERROR: `if` expression with an `else if`, but without a final `else`
println!("else if 4");
}
}
}
30 changes: 27 additions & 3 deletions tests/ui/else_if_without_else.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `if` expression with an `else if`, but without a final `else`
--> tests/ui/else_if_without_else.rs:47:12
--> tests/ui/else_if_without_else.rs:51:12
|
LL | } else if bla2() {
| ____________^
Expand All @@ -13,7 +13,7 @@ LL | | }
= help: to override `-D warnings` add `#[allow(clippy::else_if_without_else)]`

error: `if` expression with an `else if`, but without a final `else`
--> tests/ui/else_if_without_else.rs:56:12
--> tests/ui/else_if_without_else.rs:60:12
|
LL | } else if bla3() {
| ____________^
Expand All @@ -24,5 +24,29 @@ LL | | }
|
= help: add an `else` block here

error: aborting due to 2 previous errors
error: `if` expression with an `else if`, but without a final `else`
--> tests/ui/else_if_without_else.rs:87:12
|
LL | } else if bla5() {
| ____________^
LL | |
LL | | println!("else if 4");
LL | | }
| |_____^
|
= help: add an `else` block here

error: `if` expression with an `else if`, but without a final `else`
--> tests/ui/else_if_without_else.rs:117:16
|
LL | } else if bla5() {
| ________________^
LL | |
LL | | println!("else if 4");
LL | | }
| |_________^
|
= help: add an `else` block here

error: aborting due to 4 previous errors

0 comments on commit 59a5ad4

Please sign in to comment.