@@ -3,12 +3,13 @@ use clippy_utils::higher;
3
3
use clippy_utils:: source:: snippet_with_applicability;
4
4
use clippy_utils:: ty:: is_type_diagnostic_item;
5
5
use clippy_utils:: {
6
- eq_expr_value, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
6
+ eq_expr_value, get_parent_node, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks,
7
+ peel_blocks_with_stmt,
7
8
} ;
8
9
use if_chain:: if_chain;
9
10
use rustc_errors:: Applicability ;
10
11
use rustc_hir:: LangItem :: { OptionNone , OptionSome , ResultErr , ResultOk } ;
11
- use rustc_hir:: { BindingAnnotation , Expr , ExprKind , PatKind , PathSegment , QPath } ;
12
+ use rustc_hir:: { BindingAnnotation , Expr , ExprKind , Node , PatKind , PathSegment , QPath } ;
12
13
use rustc_lint:: { LateContext , LateLintPass } ;
13
14
use rustc_middle:: ty:: Ty ;
14
15
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -122,11 +123,12 @@ fn check_if_let_some_or_err_and_early_return(cx: &LateContext<'_>, expr: &Expr<'
122
123
let mut applicability = Applicability :: MachineApplicable ;
123
124
let receiver_str = snippet_with_applicability( cx, let_expr. span, ".." , & mut applicability) ;
124
125
let by_ref = matches!( annot, BindingAnnotation :: Ref | BindingAnnotation :: RefMut ) ;
126
+ let requires_semi = matches!( get_parent_node( cx. tcx, expr. hir_id) , Some ( Node :: Stmt ( _) ) ) ;
125
127
let replacement = format!(
126
128
"{}{}?{}" ,
127
129
receiver_str,
128
130
if by_ref { ".as_ref()" } else { "" } ,
129
- if requires_semi( if_then , if_else ) { ";" } else { "" }
131
+ if requires_semi { ";" } else { "" }
130
132
) ;
131
133
offer_suggestion( cx, expr, replacement, applicability) ;
132
134
}
@@ -202,18 +204,6 @@ fn expr_return_none_or_err(
202
204
}
203
205
}
204
206
205
- /// Check whether the fixed code needs semicolon after `?`
206
- ///
207
- /// It will need a semicolon if all block(s) has one.
208
- fn requires_semi ( if_then : & Expr < ' _ > , if_else : Option < & Expr < ' _ > > ) -> bool {
209
- let if_then_kind = & peel_blocks_with_stmt ( if_then) . kind ;
210
- let if_then_is_ret_stmt = matches ! ( if_then_kind, ExprKind :: Ret ( ..) ) ;
211
- if if_else. is_none ( ) || matches ! ( peel_blocks_with_stmt( if_else. unwrap( ) ) . kind, ExprKind :: Ret ( _) ) {
212
- return if_then_is_ret_stmt;
213
- }
214
- false
215
- }
216
-
217
207
fn offer_suggestion ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , suggestion : String , applicability : Applicability ) {
218
208
if !suggestion. is_empty ( ) {
219
209
span_lint_and_sugg (
0 commit comments