@@ -3,7 +3,7 @@ use clippy_utils::ty::is_type_diagnostic_item;
33use clippy_utils:: visitors:: contains_unsafe_block;
44use clippy_utils:: { is_res_lang_ctor, path_res, path_to_local_id} ;
55
6- use rustc_hir:: LangItem :: OptionSome ;
6+ use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
77use rustc_hir:: { Arm , Expr , ExprKind , HirId , Pat , PatKind } ;
88use rustc_lint:: LateContext ;
99use rustc_span:: { sym, SyntaxContext } ;
@@ -25,15 +25,13 @@ fn get_cond_expr<'tcx>(
2525 if let Some ( block_expr) = peels_blocks_incl_unsafe_opt( expr) ;
2626 if let ExprKind :: If ( cond, then_expr, Some ( else_expr) ) = block_expr. kind;
2727 if let PatKind :: Binding ( _, target, ..) = pat. kind;
28- if let ( then_visitor, else_visitor)
29- = ( is_some_expr( cx, target, ctxt, then_expr) ,
30- is_some_expr( cx, target, ctxt, else_expr) ) ;
31- if then_visitor != else_visitor; // check that one expr resolves to `Some(x)`, the other to `None`
28+ if is_some_expr( cx, target, ctxt, then_expr) && is_none_expr( cx, else_expr)
29+ || is_none_expr( cx, then_expr) && is_some_expr( cx, target, ctxt, else_expr) ; // check that one expr resolves to `Some(x)`, the other to `None`
3230 then {
3331 return Some ( SomeExpr {
3432 expr: peels_blocks_incl_unsafe( cond. peel_drop_temps( ) ) ,
3533 needs_unsafe_block: contains_unsafe_block( cx, expr) ,
36- needs_negated: !then_visitor // if the `then_expr` resolves to `None`, need to negate the cond
34+ needs_negated: is_none_expr ( cx , then_expr ) // if the `then_expr` resolves to `None`, need to negate the cond
3735 } )
3836 }
3937 } ;
@@ -74,6 +72,13 @@ fn is_some_expr(cx: &LateContext<'_>, target: HirId, ctxt: SyntaxContext, expr:
7472 false
7573}
7674
75+ fn is_none_expr ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
76+ if let Some ( inner_expr) = peels_blocks_incl_unsafe_opt ( expr) {
77+ return is_res_lang_ctor ( cx, path_res ( cx, inner_expr) , OptionNone ) ;
78+ } ;
79+ false
80+ }
81+
7782// given the closure: `|<pattern>| <expr>`
7883// returns `|&<pattern>| <expr>`
7984fn add_ampersand_if_copy ( body_str : String , has_copy_trait : bool ) -> String {
0 commit comments