1
- use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
1
use clippy_utils:: source:: snippet_with_applicability;
3
2
use clippy_utils:: ty:: is_type_diagnostic_item;
4
- use if_chain :: if_chain ;
3
+ use clippy_utils :: { diagnostics :: span_lint_and_sugg , is_lang_ctor } ;
5
4
use rustc_errors:: Applicability ;
6
- use rustc_hir:: { Expr , ExprKind , QPath } ;
5
+ use rustc_hir:: { lang_items :: LangItem , Expr , ExprKind } ;
7
6
use rustc_lint:: LateContext ;
8
7
use rustc_span:: { sym, Span } ;
9
8
@@ -22,14 +21,14 @@ pub(super) fn check<'tcx>(
22
21
23
22
if is_type_diagnostic_item ( cx, ty, sym:: Option ) {
24
23
title = "found `.or(Some(…)).unwrap()`" ;
25
- if let Some ( content) = get_content_if_is ( or_arg, "Some" ) {
24
+ if let Some ( content) = get_content_if_ctor_matches ( cx , or_arg, LangItem :: OptionSome ) {
26
25
or_arg_content = content;
27
26
} else {
28
27
return ;
29
28
}
30
29
} else if is_type_diagnostic_item ( cx, ty, sym:: Result ) {
31
30
title = "found `.or(Ok(…)).unwrap()`" ;
32
- if let Some ( content) = get_content_if_is ( or_arg, "Ok" ) {
31
+ if let Some ( content) = get_content_if_ctor_matches ( cx , or_arg, LangItem :: ResultOk ) {
33
32
or_arg_content = content;
34
33
} else {
35
34
return ;
@@ -64,19 +63,13 @@ pub(super) fn check<'tcx>(
64
63
) ;
65
64
}
66
65
67
- /// is expr a Call to name? if so, return what it's wrapping
68
- /// name might be "Some", "Ok", "Err", etc.
69
- fn get_content_if_is < ' a > ( expr : & Expr < ' a > , name : & str ) -> Option < Span > {
70
- if_chain ! {
71
- if let ExprKind :: Call ( some_expr, [ arg] ) = expr. kind;
72
- if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = & some_expr. kind;
73
- if let Some ( path_segment) = path. segments. first( ) ;
74
- if path_segment. ident. name. as_str( ) == name;
75
- then {
76
- Some ( arg. span)
77
- }
78
- else {
79
- None
80
- }
66
+ fn get_content_if_ctor_matches ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , item : LangItem ) -> Option < Span > {
67
+ if let ExprKind :: Call ( some_expr, [ arg] ) = expr. kind
68
+ && let ExprKind :: Path ( qpath) = & some_expr. kind
69
+ && is_lang_ctor ( cx, qpath, item)
70
+ {
71
+ Some ( arg. span )
72
+ } else {
73
+ None
81
74
}
82
75
}
0 commit comments