@@ -8,20 +8,20 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
8
8
use rustc_hir:: * ;
9
9
10
10
declare_clippy_lint ! {
11
- /// **What it does:** Checks for instances `iterator. map(f)` where `f` is the identity function.
11
+ /// **What it does:** Checks for instances of ` map(f)` where `f` is the identity function.
12
12
///
13
- /// **Why is this bad?** It can be written more concisely as `iterator`, without the call to `map`.
13
+ /// **Why is this bad?** It can be written more concisely without the call to `map`.
14
14
///
15
15
/// **Known problems:** None.
16
16
///
17
17
/// **Example:**
18
18
///
19
19
/// ```rust
20
- /// // example code where clippy issues a warning
20
+ /// let x = [1, 2, 3].map(|x| x).
21
21
/// ```
22
22
/// Use instead:
23
23
/// ```rust
24
- /// // example code which does not raise clippy warning
24
+ /// let x =
25
25
/// ```
26
26
pub MAP_IDENTITY ,
27
27
style,
@@ -32,22 +32,21 @@ declare_lint_pass!(MapIdentity => [MAP_IDENTITY]);
32
32
33
33
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for MapIdentity {
34
34
fn check_expr ( & mut self , cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > ) {
35
- // println!("start");
36
35
if expr. span . from_expansion ( ) {
37
36
return ;
38
37
}
39
38
40
39
if_chain ! {
41
- if let Some ( func) = get_map_argument( cx, expr) ;
40
+ if let Some ( [ caller , func] ) = get_map_argument( cx, expr) ;
42
41
if let Some ( body) = get_body( cx, func) ;
43
42
if is_identity_function( cx, body) ;
44
43
then {
45
44
span_lint_and_sugg(
46
45
cx,
47
46
MAP_IDENTITY ,
48
- expr. span,
49
- "Unnecessary map of the identity function" ,
50
- "Remove the `map` call " ,
47
+ expr. span. trim_start ( caller . span ) . unwrap ( ) ,
48
+ "unnecessary map of the identity function" ,
49
+ "remove the call to `map`" ,
51
50
String :: new( ) ,
52
51
Applicability :: MachineApplicable
53
52
)
@@ -57,9 +56,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapIdentity {
57
56
}
58
57
59
58
60
- /// Returns the function passed into iterator. map() if the expression is a method call to
61
- /// iterator. map(). Otherwise, returns None.
62
- fn get_map_argument < ' a > ( cx : & LateContext < ' _ , ' _ > , expr : & ' a Expr < ' a > ) -> Option < & ' a Expr < ' a > > {
59
+ /// Returns the arguments passed into map() if the expression is a method call to
60
+ /// map(). Otherwise, returns None.
61
+ fn get_map_argument < ' a > ( cx : & LateContext < ' _ , ' _ > , expr : & ' a Expr < ' a > ) -> Option < & ' a [ Expr < ' a > ] > {
63
62
if_chain ! {
64
63
if let ExprKind :: MethodCall ( ref method, _, ref args) = expr. kind;
65
64
if args. len( ) == 2 && method. ident. as_str( ) == "map" ;
@@ -68,7 +67,7 @@ fn get_map_argument<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr<'a>) -> Option<
68
67
|| is_type_diagnostic_item( cx, caller_ty, sym!( result_type) )
69
68
|| is_type_diagnostic_item( cx, caller_ty, sym!( option_type) ) ;
70
69
then {
71
- Some ( & args[ 1 ] )
70
+ Some ( args)
72
71
} else {
73
72
None
74
73
}
0 commit comments