@@ -3,7 +3,7 @@ use clippy_utils::higher;
3
3
use clippy_utils:: source:: snippet_with_applicability;
4
4
use clippy_utils:: sugg:: Sugg ;
5
5
use 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} ;
7
7
use if_chain:: if_chain;
8
8
use rustc_errors:: Applicability ;
9
9
use rustc_hir:: LangItem :: { OptionNone , OptionSome , ResultErr , ResultOk } ;
@@ -101,14 +101,19 @@ impl QuestionMark {
101
101
// Only check one of the blocks
102
102
let nested_expr = if_else. unwrap_or( if_then) ;
103
103
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;
105
105
if Self :: result_check_and_early_return( cx, let_expr, nested_expr, Some ( ident. name) )
106
106
|| Self :: option_check_and_early_return( cx, let_expr, nested_expr) ;
107
107
let by_ref = matches!( annot, BindingAnnotation :: Ref | BindingAnnotation :: RefMut ) ;
108
108
then {
109
109
let mut applicability = Applicability :: MachineApplicable ;
110
110
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
+ ) ;
112
117
113
118
span_lint_and_sugg(
114
119
cx,
@@ -178,8 +183,10 @@ impl QuestionMark {
178
183
symbol : Option < Symbol > ,
179
184
) -> bool {
180
185
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) ,
183
190
ExprKind :: Call ( _, args_expr) => {
184
191
if let Some ( arg) = args_expr. first ( ) {
185
192
if let Some ( name) = symbol {
0 commit comments