@@ -86,7 +86,7 @@ declare_clippy_lint! {
8686#[ derive( PartialEq , Eq , Copy , Clone ) ]
8787enum RetReplacement {
8888 Empty ,
89- Unit
89+ Unit ,
9090}
9191
9292declare_lint_pass ! ( Return => [ NEEDLESS_RETURN , LET_AND_RETURN , UNUSED_UNIT ] ) ;
@@ -105,13 +105,24 @@ impl Return {
105105 }
106106
107107 // 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+ ) {
109115 match expr. node {
110116 // simple return is always "bad"
111117 ast:: ExprKind :: Ret ( ref inner) => {
112118 // allow `#[cfg(a)] return a; #[cfg(b)] return b;`
113119 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+ ) ;
115126 }
116127 } ,
117128 // a whole block? check it!
@@ -135,7 +146,13 @@ impl Return {
135146 }
136147 }
137148
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+ ) {
139156 match inner_span {
140157 Some ( inner_span) => {
141158 if in_external_macro ( cx. sess ( ) , inner_span) || in_macro_or_desugar ( inner_span) {
@@ -144,39 +161,32 @@ impl Return {
144161
145162 span_lint_and_then ( cx, NEEDLESS_RETURN , ret_span, "unneeded return statement" , |db| {
146163 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| {
147171 db. span_suggestion (
148172 ret_span,
149- "remove `return` as shown " ,
150- snippet ,
173+ "remove `return`" ,
174+ String :: new ( ) ,
151175 Applicability :: MachineApplicable ,
152176 ) ;
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+ } ,
155189 } ,
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- }
180190 }
181191 }
182192
0 commit comments