File tree Expand file tree Collapse file tree 3 files changed +104
-20
lines changed Expand file tree Collapse file tree 3 files changed +104
-20
lines changed Original file line number Diff line number Diff line change @@ -49,24 +49,22 @@ declare_clippy_lint! {
4949declare_lint_pass ! ( ElseIfWithoutElse => [ ELSE_IF_WITHOUT_ELSE ] ) ;
5050
5151impl EarlyLintPass for ElseIfWithoutElse {
52- fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , mut item : & Expr ) {
52+ fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , item : & Expr ) {
5353 if in_external_macro ( cx. sess ( ) , item. span ) {
5454 return ;
5555 }
5656
57- while let ExprKind :: If ( _, _, Some ( ref els) ) = item. kind {
58- if let ExprKind :: If ( _, _, None ) = els. kind {
59- span_lint_and_help (
60- cx,
61- ELSE_IF_WITHOUT_ELSE ,
62- els. span ,
63- "`if` expression with an `else if`, but without a final `else`" ,
64- None ,
65- "add an `else` block here" ,
66- ) ;
67- }
68-
69- item = els;
57+ if let ExprKind :: If ( _, _, Some ( ref els) ) = item. kind
58+ && let ExprKind :: If ( _, _, None ) = els. kind
59+ {
60+ span_lint_and_help (
61+ cx,
62+ ELSE_IF_WITHOUT_ELSE ,
63+ els. span ,
64+ "`if` expression with an `else if`, but without a final `else`" ,
65+ None ,
66+ "add an `else` block here" ,
67+ ) ;
7068 }
7169 }
7270}
Original file line number Diff line number Diff line change 1- //@compile-flags: -Zdeduplicate-diagnostics=yes
2-
3- #![ warn( clippy:: all) ]
41#![ warn( clippy:: else_if_without_else) ]
2+ #![ allow( clippy:: collapsible_else_if) ]
53
64fn bla1 ( ) -> bool {
75 unimplemented ! ( )
@@ -12,6 +10,12 @@ fn bla2() -> bool {
1210fn bla3 ( ) -> bool {
1311 unimplemented ! ( )
1412}
13+ fn bla4 ( ) -> bool {
14+ unimplemented ! ( )
15+ }
16+ fn bla5 ( ) -> bool {
17+ unimplemented ! ( )
18+ }
1519
1620fn main ( ) {
1721 if bla1 ( ) {
@@ -57,4 +61,62 @@ fn main() {
5761 //~^ ERROR: `if` expression with an `else if`, but without a final `else`
5862 println ! ( "else if 2" ) ;
5963 }
64+
65+ if bla1 ( ) {
66+ println ! ( "if" ) ;
67+ } else if bla2 ( ) {
68+ println ! ( "else if 1" ) ;
69+ } else if bla3 ( ) {
70+ println ! ( "else if 2" ) ;
71+ } else if bla4 ( ) {
72+ println ! ( "else if 3" ) ;
73+ } else if bla5 ( ) {
74+ println ! ( "else if 4" ) ;
75+ } else {
76+ println ! ( "else" ) ;
77+ }
78+
79+ if bla1 ( ) {
80+ println ! ( "if" ) ;
81+ } else if bla2 ( ) {
82+ println ! ( "else if 1" ) ;
83+ } else if bla3 ( ) {
84+ println ! ( "else if 2" ) ;
85+ } else if bla4 ( ) {
86+ println ! ( "else if 3" ) ;
87+ } else if bla5 ( ) {
88+ //~^ ERROR: `if` expression with an `else if`, but without a final `else`
89+ println ! ( "else if 4" ) ;
90+ }
91+
92+ if bla1 ( ) {
93+ println ! ( "if" ) ;
94+ } else if bla2 ( ) {
95+ println ! ( "else if 1" ) ;
96+ } else {
97+ if bla3 ( ) {
98+ println ! ( "else if 2" ) ;
99+ } else if bla4 ( ) {
100+ println ! ( "else if 3" ) ;
101+ } else if bla5 ( ) {
102+ println ! ( "else if 4" ) ;
103+ } else {
104+ println ! ( "else" ) ;
105+ }
106+ }
107+
108+ if bla1 ( ) {
109+ println ! ( "if" ) ;
110+ } else if bla2 ( ) {
111+ println ! ( "else if 1" ) ;
112+ } else {
113+ if bla3 ( ) {
114+ println ! ( "else if 2" ) ;
115+ } else if bla4 ( ) {
116+ println ! ( "else if 3" ) ;
117+ } else if bla5 ( ) {
118+ //~^ ERROR: `if` expression with an `else if`, but without a final `else`
119+ println ! ( "else if 4" ) ;
120+ }
121+ }
60122}
Original file line number Diff line number Diff line change 11error: `if` expression with an `else if`, but without a final `else`
2- --> tests/ui/else_if_without_else.rs:47 :12
2+ --> tests/ui/else_if_without_else.rs:51 :12
33 |
44LL | } else if bla2() {
55 | ____________^
@@ -13,7 +13,7 @@ LL | | }
1313 = help: to override `-D warnings` add `#[allow(clippy::else_if_without_else)]`
1414
1515error: `if` expression with an `else if`, but without a final `else`
16- --> tests/ui/else_if_without_else.rs:56 :12
16+ --> tests/ui/else_if_without_else.rs:60 :12
1717 |
1818LL | } else if bla3() {
1919 | ____________^
@@ -24,5 +24,29 @@ LL | | }
2424 |
2525 = help: add an `else` block here
2626
27- error: aborting due to 2 previous errors
27+ error: `if` expression with an `else if`, but without a final `else`
28+ --> tests/ui/else_if_without_else.rs:87:12
29+ |
30+ LL | } else if bla5() {
31+ | ____________^
32+ LL | |
33+ LL | | println!("else if 4");
34+ LL | | }
35+ | |_____^
36+ |
37+ = help: add an `else` block here
38+
39+ error: `if` expression with an `else if`, but without a final `else`
40+ --> tests/ui/else_if_without_else.rs:117:16
41+ |
42+ LL | } else if bla5() {
43+ | ________________^
44+ LL | |
45+ LL | | println!("else if 4");
46+ LL | | }
47+ | |_________^
48+ |
49+ = help: add an `else` block here
50+
51+ error: aborting due to 4 previous errors
2852
You can’t perform that action at this time.
0 commit comments