@@ -4,7 +4,7 @@ use if_chain::if_chain;
4
4
use rustc_hir:: { Expr , ExprKind , QPath } ;
5
5
use rustc_lint:: { LateContext , LateLintPass } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
- use rustc_span:: { sym, Span } ;
7
+ use rustc_span:: sym;
8
8
9
9
declare_clippy_lint ! {
10
10
/// ### What it does
@@ -47,25 +47,22 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
47
47
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
48
48
// look for x.or().unwrap()
49
49
if_chain ! {
50
- if let ExprKind :: MethodCall ( path, args , unwrap_span) = expr. kind;
50
+ if let ExprKind :: MethodCall ( path, [ unwrap_self ] , unwrap_span) = expr. kind;
51
51
if path. ident. name == sym:: unwrap;
52
- if let Some ( caller) = args. first( ) ;
53
- if let ExprKind :: MethodCall ( caller_path, caller_args, or_span) = caller. kind;
52
+ if let ExprKind :: MethodCall ( caller_path, [ or_self, or_arg] , or_span) = unwrap_self. kind;
54
53
if caller_path. ident. name == sym:: or;
55
54
then {
56
- let ty = cx. typeck_results( ) . expr_ty( & caller_args [ 0 ] ) ; // get type of x (we later check if it's Option or Result)
55
+ let ty = cx. typeck_results( ) . expr_ty( & or_self ) ; // get type of x (we later check if it's Option or Result)
57
56
let title;
58
- let arg = & caller_args[ 1 ] ; // the argument or(xyz) is called with
59
57
60
58
if is_type_diagnostic_item( cx, ty, sym:: Option ) {
61
59
title = ".or(Some(…)).unwrap() found" ;
62
- if !is( arg , "Some" ) {
60
+ if !is( or_arg , "Some" ) {
63
61
return ;
64
62
}
65
-
66
63
} else if is_type_diagnostic_item( cx, ty, sym:: Result ) {
67
64
title = ".or(Ok(…)).unwrap() found" ;
68
- if !is( arg , "Ok" ) {
65
+ if !is( or_arg , "Ok" ) {
69
66
return ;
70
67
}
71
68
} else {
@@ -74,13 +71,10 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
74
71
return ;
75
72
}
76
73
77
- // span = or_span + unwrap_span
78
- let span = Span :: new( or_span. lo( ) , unwrap_span. hi( ) , or_span. ctxt( ) , or_span. parent( ) ) ;
79
-
80
74
span_lint_and_help(
81
75
cx,
82
76
USE_UNWRAP_OR ,
83
- span ,
77
+ or_span . to ( unwrap_span ) ,
84
78
title,
85
79
None ,
86
80
"use `unwrap_or()` instead"
0 commit comments