@@ -86,7 +86,7 @@ declare_clippy_lint! {
86
86
#[ derive( PartialEq , Eq , Copy , Clone ) ]
87
87
enum RetReplacement {
88
88
Empty ,
89
- Unit
89
+ Unit ,
90
90
}
91
91
92
92
declare_lint_pass ! ( Return => [ NEEDLESS_RETURN , LET_AND_RETURN , UNUSED_UNIT ] ) ;
@@ -105,13 +105,24 @@ impl Return {
105
105
}
106
106
107
107
// Check a the final expression in a block if it's a return.
108
- fn check_final_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & ast:: Expr , span : Option < Span > , replacement : RetReplacement ) {
108
+ fn check_final_expr (
109
+ & mut self ,
110
+ cx : & EarlyContext < ' _ > ,
111
+ expr : & ast:: Expr ,
112
+ span : Option < Span > ,
113
+ replacement : RetReplacement ,
114
+ ) {
109
115
match expr. node {
110
116
// simple return is always "bad"
111
117
ast:: ExprKind :: Ret ( ref inner) => {
112
118
// allow `#[cfg(a)] return a; #[cfg(b)] return b;`
113
119
if !expr. attrs . iter ( ) . any ( attr_is_cfg) {
114
- self . emit_return_lint ( cx, span. expect ( "`else return` is not possible" ) , inner. as_ref ( ) . map ( |i| i. span ) , replacement) ;
120
+ self . emit_return_lint (
121
+ cx,
122
+ span. expect ( "`else return` is not possible" ) ,
123
+ inner. as_ref ( ) . map ( |i| i. span ) ,
124
+ replacement,
125
+ ) ;
115
126
}
116
127
} ,
117
128
// a whole block? check it!
@@ -135,7 +146,13 @@ impl Return {
135
146
}
136
147
}
137
148
138
- fn emit_return_lint ( & mut self , cx : & EarlyContext < ' _ > , ret_span : Span , inner_span : Option < Span > , replacement : RetReplacement ) {
149
+ fn emit_return_lint (
150
+ & mut self ,
151
+ cx : & EarlyContext < ' _ > ,
152
+ ret_span : Span ,
153
+ inner_span : Option < Span > ,
154
+ replacement : RetReplacement ,
155
+ ) {
139
156
match inner_span {
140
157
Some ( inner_span) => {
141
158
if in_external_macro ( cx. sess ( ) , inner_span) || in_macro_or_desugar ( inner_span) {
@@ -144,39 +161,32 @@ impl Return {
144
161
145
162
span_lint_and_then ( cx, NEEDLESS_RETURN , ret_span, "unneeded return statement" , |db| {
146
163
if let Some ( snippet) = snippet_opt ( cx, inner_span) {
164
+ db. span_suggestion ( ret_span, "remove `return`" , snippet, Applicability :: MachineApplicable ) ;
165
+ }
166
+ } )
167
+ } ,
168
+ None => match replacement {
169
+ RetReplacement :: Empty => {
170
+ span_lint_and_then ( cx, NEEDLESS_RETURN , ret_span, "unneeded return statement" , |db| {
147
171
db. span_suggestion (
148
172
ret_span,
149
- "remove `return` as shown " ,
150
- snippet ,
173
+ "remove `return`" ,
174
+ String :: new ( ) ,
151
175
Applicability :: MachineApplicable ,
152
176
) ;
153
- }
154
- } )
177
+ } ) ;
178
+ } ,
179
+ RetReplacement :: Unit => {
180
+ span_lint_and_then ( cx, NEEDLESS_RETURN , ret_span, "unneeded return statement" , |db| {
181
+ db. span_suggestion (
182
+ ret_span,
183
+ "replace `return` with the unit type" ,
184
+ "()" . to_string ( ) ,
185
+ Applicability :: MachineApplicable ,
186
+ ) ;
187
+ } ) ;
188
+ } ,
155
189
} ,
156
- None => {
157
- match replacement {
158
- RetReplacement :: Empty => {
159
- span_lint_and_then ( cx, NEEDLESS_RETURN , ret_span, "unneeded return statement" , |db| {
160
- db. span_suggestion (
161
- ret_span,
162
- "remove `return`" ,
163
- String :: new ( ) ,
164
- Applicability :: MachineApplicable ,
165
- ) ;
166
- } ) ;
167
- }
168
- RetReplacement :: Unit => {
169
- span_lint_and_then ( cx, NEEDLESS_RETURN , ret_span, "unneeded return statement" , |db| {
170
- db. span_suggestion (
171
- ret_span,
172
- "replace `return` with the unit type `()`" ,
173
- "()" . to_string ( ) ,
174
- Applicability :: MachineApplicable ,
175
- ) ;
176
- } ) ;
177
- }
178
- }
179
- }
180
190
}
181
191
}
182
192
0 commit comments