@@ -8,6 +8,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Path, QPath, UnO
8
8
use rustc_lint:: { LateContext , LateLintPass } ;
9
9
use rustc_middle:: hir:: map:: Map ;
10
10
use rustc_middle:: lint:: in_external_macro;
11
+ use rustc_middle:: ty:: Ty ;
11
12
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
12
13
use rustc_span:: source_map:: Span ;
13
14
@@ -90,6 +91,14 @@ fn collect_unwrap_info<'a, 'tcx>(
90
91
branch : & ' tcx Expr < ' _ > ,
91
92
invert : bool ,
92
93
) -> Vec < UnwrapInfo < ' tcx > > {
94
+ fn is_relevant_option_call ( cx : & LateContext < ' _ , ' _ > , ty : Ty < ' _ > , method_name : & str ) -> bool {
95
+ is_type_diagnostic_item ( cx, ty, sym ! ( option_type) ) && [ "is_some" , "is_none" ] . contains ( & method_name)
96
+ }
97
+
98
+ fn is_relevant_result_call ( cx : & LateContext < ' _ , ' _ > , ty : Ty < ' _ > , method_name : & str ) -> bool {
99
+ is_type_diagnostic_item ( cx, ty, sym ! ( result_type) ) && [ "is_ok" , "is_err" ] . contains ( & method_name)
100
+ }
101
+
93
102
if let ExprKind :: Binary ( op, left, right) = & expr. kind {
94
103
match ( invert, op. node ) {
95
104
( false , BinOpKind :: And ) | ( false , BinOpKind :: BitAnd ) | ( true , BinOpKind :: Or ) | ( true , BinOpKind :: BitOr ) => {
@@ -106,9 +115,8 @@ fn collect_unwrap_info<'a, 'tcx>(
106
115
if let ExprKind :: MethodCall ( method_name, _, args) = & expr. kind;
107
116
if let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = & args[ 0 ] . kind;
108
117
let ty = cx. tables. expr_ty( & args[ 0 ] ) ;
109
- if is_type_diagnostic_item( cx, ty, sym!( option_type) ) || is_type_diagnostic_item( cx, ty, sym!( result_type) ) ;
110
118
let name = method_name. ident. as_str( ) ;
111
- if [ "is_some" , "is_none" , "is_ok" , "is_err" ] . contains ( && * name) ;
119
+ if is_relevant_option_call ( cx , ty , & name ) || is_relevant_result_call ( cx , ty , & name) ;
112
120
then {
113
121
assert!( args. len( ) == 1 ) ;
114
122
let unwrappable = match name. as_ref( ) {
0 commit comments