1
1
use crate :: utils:: span_lint_and_sugg;
2
2
use rustc_errors:: Applicability ;
3
- use rustc_hir:: { Expr , ExprKind , CaptureBy , PatKind , QPath } ;
3
+ use rustc_hir:: { CaptureBy , Expr , ExprKind , PatKind , QPath } ;
4
4
use rustc_lint:: { LateContext , LateLintPass } ;
5
5
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
6
6
@@ -18,7 +18,7 @@ declare_clippy_lint! {
18
18
/// Ignore
19
19
///}
20
20
///fn main() -> Result<(), Errors> {
21
- ///
21
+ ///
22
22
/// let x = u32::try_from(-123_i32);
23
23
///
24
24
/// println!("{:?}", x.map_err(|_| Errors::Ignore));
@@ -32,7 +32,7 @@ declare_clippy_lint! {
32
32
/// WithContext(TryFromIntError)
33
33
///}
34
34
///fn main() -> Result<(), Errors> {
35
- ///
35
+ ///
36
36
/// let x = u32::try_from(-123_i32);
37
37
///
38
38
/// println!("{:?}", x.map_err(|e| Errors::WithContext(e)));
@@ -48,34 +48,42 @@ declare_clippy_lint! {
48
48
declare_lint_pass ! ( MapErrIgnore => [ MAP_ERR_IGNORE ] ) ;
49
49
50
50
impl < ' tcx > LateLintPass < ' tcx > for MapErrIgnore {
51
- // do not try to lint if this is from a macro or desugaring
51
+ // do not try to lint if this is from a macro or desugaring
52
52
fn check_expr ( & mut self , cx : & LateContext < ' _ > , e : & Expr < ' _ > ) {
53
53
if e. span . from_expansion ( ) {
54
54
return ;
55
55
}
56
56
57
57
// check if this is a method call (e.g. x.foo())
58
58
if let ExprKind :: MethodCall ( ref method, _t_span, ref args, _) = e. kind {
59
- // only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1] Enum::Variant[2]))
59
+ // only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1]
60
+ // Enum::Variant[2]))
60
61
if method. ident . as_str ( ) == "map_err" && args. len ( ) == 2 {
61
62
// make sure the first argument is a closure, and grab the CaptureRef, body_id, and body_span fields
62
- if let ExprKind :: Closure ( capture, _, body_id, body_span, _) = args[ 1 ] . kind {
63
+ if let ExprKind :: Closure ( capture, _, body_id, body_span, _) = args[ 1 ] . kind {
63
64
// check if this is by Reference (meaning there's no move statement)
64
- if capture == CaptureBy :: Ref {
65
- // Get the closure body to check the parameters and values
65
+ if capture == CaptureBy :: Ref {
66
+ // Get the closure body to check the parameters and values
66
67
let closure_body = cx. tcx . hir ( ) . body ( body_id) ;
67
68
// make sure there's only one parameter (`|_|`)
68
- if closure_body. params . len ( ) == 1 {
69
- // make sure that parameter is the wild token (`_`)
69
+ if closure_body. params . len ( ) == 1 {
70
+ // make sure that parameter is the wild token (`_`)
70
71
if let PatKind :: Wild = closure_body. params [ 0 ] . pat . kind {
71
- // Check the value of the closure to see if we can build the enum we are throwing away the error for
72
- // make sure this is a Path
72
+ // Check the value of the closure to see if we can build the enum we are throwing away
73
+ // the error for make sure this is a Path
73
74
if let ExprKind :: Path ( q_path) = & closure_body. value . kind {
74
75
// this should be a resolved path, only keep the path field
75
76
if let QPath :: Resolved ( _, path) = q_path {
76
- // finally get the idents for each path segment collect them as a string and join them with the path separator ("::"")
77
- let closure_fold: String = path. segments . iter ( ) . map ( |x| x. ident . as_str ( ) . to_string ( ) ) . collect :: < Vec < String > > ( ) . join ( "::" ) ;
78
- //Span the body of the closure (the |...| bit) and suggest the fix by taking the error and encapsulating it in the enum
77
+ // finally get the idents for each path segment collect them as a string and
78
+ // join them with the path separator ("::"")
79
+ let closure_fold: String = path
80
+ . segments
81
+ . iter ( )
82
+ . map ( |x| x. ident . as_str ( ) . to_string ( ) )
83
+ . collect :: < Vec < String > > ( )
84
+ . join ( "::" ) ;
85
+ //Span the body of the closure (the |...| bit) and suggest the fix by taking
86
+ // the error and encapsulating it in the enum
79
87
span_lint_and_sugg (
80
88
cx,
81
89
MAP_ERR_IGNORE ,
@@ -84,10 +92,11 @@ impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
84
92
"Allow the error enum to encapsulate the original error" ,
85
93
format ! ( "|e| {}(e)" , closure_fold) ,
86
94
Applicability :: HasPlaceholders ,
87
- ) ;
95
+ ) ;
88
96
}
89
97
} else {
90
- //If we cannot build the enum in a human readable way just suggest not throwing way the error
98
+ //If we cannot build the enum in a human readable way just suggest not throwing way
99
+ // the error
91
100
span_lint_and_sugg (
92
101
cx,
93
102
MAP_ERR_IGNORE ,
@@ -96,13 +105,13 @@ impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
96
105
"Allow the error enum to encapsulate the original error" ,
97
106
"|e|" . to_string ( ) ,
98
107
Applicability :: HasPlaceholders ,
99
- ) ;
108
+ ) ;
100
109
}
101
110
}
102
111
}
103
- }
112
+ }
104
113
}
105
114
}
106
115
}
107
116
}
108
- }
117
+ }
0 commit comments