File tree 3 files changed +104
-20
lines changed
3 files changed +104
-20
lines changed Original file line number Diff line number Diff line change @@ -49,24 +49,22 @@ declare_clippy_lint! {
49
49
declare_lint_pass ! ( ElseIfWithoutElse => [ ELSE_IF_WITHOUT_ELSE ] ) ;
50
50
51
51
impl EarlyLintPass for ElseIfWithoutElse {
52
- fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , mut item : & Expr ) {
52
+ fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , item : & Expr ) {
53
53
if in_external_macro ( cx. sess ( ) , item. span ) {
54
54
return ;
55
55
}
56
56
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
+ ) ;
70
68
}
71
69
}
72
70
}
Original file line number Diff line number Diff line change 1
- //@compile-flags: -Zdeduplicate-diagnostics=yes
2
-
3
- #![ warn( clippy:: all) ]
4
1
#![ warn( clippy:: else_if_without_else) ]
2
+ #![ allow( clippy:: collapsible_else_if) ]
5
3
6
4
fn bla1 ( ) -> bool {
7
5
unimplemented ! ( )
@@ -12,6 +10,12 @@ fn bla2() -> bool {
12
10
fn bla3 ( ) -> bool {
13
11
unimplemented ! ( )
14
12
}
13
+ fn bla4 ( ) -> bool {
14
+ unimplemented ! ( )
15
+ }
16
+ fn bla5 ( ) -> bool {
17
+ unimplemented ! ( )
18
+ }
15
19
16
20
fn main ( ) {
17
21
if bla1 ( ) {
@@ -57,4 +61,62 @@ fn main() {
57
61
//~^ ERROR: `if` expression with an `else if`, but without a final `else`
58
62
println ! ( "else if 2" ) ;
59
63
}
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
+ }
60
122
}
Original file line number Diff line number Diff line change 1
1
error: `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
3
3
|
4
4
LL | } else if bla2() {
5
5
| ____________^
@@ -13,7 +13,7 @@ LL | | }
13
13
= help: to override `-D warnings` add `#[allow(clippy::else_if_without_else)]`
14
14
15
15
error: `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
17
17
|
18
18
LL | } else if bla3() {
19
19
| ____________^
@@ -24,5 +24,29 @@ LL | | }
24
24
|
25
25
= help: add an `else` block here
26
26
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
28
52
You can’t perform that action at this time.
0 commit comments