@@ -3,7 +3,7 @@ use clippy_utils::higher;
33use clippy_utils:: source:: snippet_with_applicability;
44use clippy_utils:: sugg:: Sugg ;
55use clippy_utils:: ty:: is_type_diagnostic_item;
6- use clippy_utils:: { eq_expr_value, is_lang_ctor, path_to_local, peel_blocks, peel_blocks_with_stmt} ;
6+ use clippy_utils:: { eq_expr_value, is_lang_ctor, path_to_local, path_to_local_id , peel_blocks, peel_blocks_with_stmt} ;
77use if_chain:: if_chain;
88use rustc_errors:: Applicability ;
99use rustc_hir:: LangItem :: { OptionNone , OptionSome , ResultErr , ResultOk } ;
@@ -101,14 +101,19 @@ impl QuestionMark {
101101 // Only check one of the blocks
102102 let nested_expr = if_else. unwrap_or( if_then) ;
103103 if Self :: check_lang_items( cx, path1, & [ OptionSome , ResultOk , ResultErr ] ) ;
104- if let PatKind :: Binding ( annot, _ , ident, _) = fields[ 0 ] . kind;
104+ if let PatKind :: Binding ( annot, bind_id , ident, _) = fields[ 0 ] . kind;
105105 if Self :: result_check_and_early_return( cx, let_expr, nested_expr, Some ( ident. name) )
106106 || Self :: option_check_and_early_return( cx, let_expr, nested_expr) ;
107107 let by_ref = matches!( annot, BindingAnnotation :: Ref | BindingAnnotation :: RefMut ) ;
108108 then {
109109 let mut applicability = Applicability :: MachineApplicable ;
110110 let receiver_str = snippet_with_applicability( cx, let_expr. span, ".." , & mut applicability) ;
111- let replacement = format!( "{}{}?;" , receiver_str, if by_ref { ".as_ref()" } else { "" } , ) ;
111+ let replacement = format!(
112+ "{}{}?{}" ,
113+ receiver_str,
114+ if by_ref { ".as_ref()" } else { "" } ,
115+ if path_to_local_id( peel_blocks( if_then) , bind_id) { "" } else { ";" }
116+ ) ;
112117
113118 span_lint_and_sugg(
114119 cx,
@@ -178,8 +183,10 @@ impl QuestionMark {
178183 symbol : Option < Symbol > ,
179184 ) -> bool {
180185 match & peel_blocks_with_stmt ( expr) . kind {
181- ExprKind :: Ret ( Some ( ret_expr) ) => Self :: expression_returns_unmodified_err ( cx, ret_expr, cond_expr, symbol) ,
182- ExprKind :: Path ( _) => path_to_local ( expr) . is_some ( ) && path_to_local ( expr) == path_to_local ( cond_expr) ,
186+ ExprKind :: Ret ( Some ( ret_expr) ) =>
187+ Self :: expression_returns_unmodified_err ( cx, ret_expr, cond_expr, symbol) ,
188+ ExprKind :: Path ( _) =>
189+ path_to_local ( expr) . is_some ( ) && path_to_local ( expr) == path_to_local ( cond_expr) ,
183190 ExprKind :: Call ( _, args_expr) => {
184191 if let Some ( arg) = args_expr. first ( ) {
185192 if let Some ( name) = symbol {
0 commit comments