11use crate :: map_unit_fn:: OPTION_MAP_UNIT_FN ;
22use crate :: matches:: MATCH_AS_REF ;
3- use clippy_utils:: res:: { MaybeDef , MaybeQPath , MaybeResPath } ;
3+ use clippy_utils:: res:: { MaybeDef , MaybeResPath } ;
44use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
55use clippy_utils:: sugg:: Sugg ;
66use clippy_utils:: ty:: { is_copy, is_unsafe_fn, peel_and_count_ty_refs} ;
77use clippy_utils:: {
8- CaptureKind , can_move_expr_to_closure, expr_requires_coercion, is_else_clause, is_lint_allowed, peel_blocks ,
9- peel_hir_expr_refs, peel_hir_expr_while,
8+ CaptureKind , as_some_pattern , can_move_expr_to_closure, expr_requires_coercion, is_else_clause, is_lint_allowed,
9+ is_none_expr , is_none_pattern , peel_blocks , peel_hir_expr_refs, peel_hir_expr_while,
1010} ;
1111use rustc_ast:: util:: parser:: ExprPrecedence ;
1212use rustc_errors:: Applicability ;
13- use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
1413use rustc_hir:: def:: Res ;
15- use rustc_hir:: { BindingMode , Expr , ExprKind , HirId , Mutability , Pat , PatExpr , PatExprKind , PatKind , Path , QPath } ;
14+ use rustc_hir:: { BindingMode , Expr , ExprKind , HirId , Mutability , Pat , PatKind , Path , QPath } ;
1615use rustc_lint:: LateContext ;
1716use rustc_span:: { SyntaxContext , sym} ;
1817
@@ -44,16 +43,16 @@ where
4443 try_parse_pattern ( cx, then_pat, expr_ctxt) ,
4544 else_pat. map_or ( Some ( OptionPat :: Wild ) , |p| try_parse_pattern ( cx, p, expr_ctxt) ) ,
4645 ) {
47- ( Some ( OptionPat :: Wild ) , Some ( OptionPat :: Some { pattern, ref_count } ) ) if is_none_expr ( cx, then_body) => {
46+ ( Some ( OptionPat :: Wild ) , Some ( OptionPat :: Some { pattern, ref_count } ) ) if is_none_arm_body ( cx, then_body) => {
4847 ( else_body, pattern, ref_count, true )
4948 } ,
50- ( Some ( OptionPat :: None ) , Some ( OptionPat :: Some { pattern, ref_count } ) ) if is_none_expr ( cx, then_body) => {
49+ ( Some ( OptionPat :: None ) , Some ( OptionPat :: Some { pattern, ref_count } ) ) if is_none_arm_body ( cx, then_body) => {
5150 ( else_body, pattern, ref_count, false )
5251 } ,
53- ( Some ( OptionPat :: Some { pattern, ref_count } ) , Some ( OptionPat :: Wild ) ) if is_none_expr ( cx, else_body) => {
52+ ( Some ( OptionPat :: Some { pattern, ref_count } ) , Some ( OptionPat :: Wild ) ) if is_none_arm_body ( cx, else_body) => {
5453 ( then_body, pattern, ref_count, true )
5554 } ,
56- ( Some ( OptionPat :: Some { pattern, ref_count } ) , Some ( OptionPat :: None ) ) if is_none_expr ( cx, else_body) => {
55+ ( Some ( OptionPat :: Some { pattern, ref_count } ) , Some ( OptionPat :: None ) ) if is_none_arm_body ( cx, else_body) => {
5756 ( then_body, pattern, ref_count, false )
5857 } ,
5958 _ => return None ,
@@ -255,23 +254,9 @@ pub(super) fn try_parse_pattern<'tcx>(
255254 match pat. kind {
256255 PatKind :: Wild => Some ( OptionPat :: Wild ) ,
257256 PatKind :: Ref ( pat, _) => f ( cx, pat, ref_count + 1 , ctxt) ,
258- PatKind :: Expr ( PatExpr {
259- kind : PatExprKind :: Path ( qpath) ,
260- hir_id,
261- ..
262- } ) if cx
263- . qpath_res ( qpath, * hir_id)
264- . ctor_parent ( cx)
265- . is_lang_item ( cx, OptionNone ) =>
266- {
267- Some ( OptionPat :: None )
268- } ,
269- PatKind :: TupleStruct ( ref qpath, [ pattern] , _)
270- if cx
271- . qpath_res ( qpath, pat. hir_id )
272- . ctor_parent ( cx)
273- . is_lang_item ( cx, OptionSome )
274- && pat. span . ctxt ( ) == ctxt =>
257+ _ if is_none_pattern ( cx, pat) => Some ( OptionPat :: None ) ,
258+ _ if let Some ( [ pattern] ) = as_some_pattern ( cx, pat)
259+ && pat. span . ctxt ( ) == ctxt =>
275260 {
276261 Some ( OptionPat :: Some { pattern, ref_count } )
277262 } ,
@@ -281,7 +266,7 @@ pub(super) fn try_parse_pattern<'tcx>(
281266 f ( cx, pat, 0 , ctxt)
282267}
283268
284- // Checks for the `None` value.
285- fn is_none_expr ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
286- peel_blocks ( expr ) . res ( cx ) . ctor_parent ( cx ) . is_lang_item ( cx , OptionNone )
269+ /// Checks for the `None` value, possibly in a block .
270+ fn is_none_arm_body ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
271+ is_none_expr ( cx , peel_blocks ( expr ) )
287272}
0 commit comments