Skip to content

Commit ab4c8b4

Browse files
committed
Fixed the suggestion, added stderr and fixed files
1 parent c1b04e6 commit ab4c8b4

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

clippy_lints/src/map_identity.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_hir::*;
99

1010
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.
1212
///
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`.
1414
///
1515
/// **Known problems:** None.
1616
///
1717
/// **Example:**
1818
///
1919
/// ```rust
20-
/// // example code where clippy issues a warning
20+
/// let x = [1, 2, 3].map(|x| x).
2121
/// ```
2222
/// Use instead:
2323
/// ```rust
24-
/// // example code which does not raise clippy warning
24+
/// let x =
2525
/// ```
2626
pub MAP_IDENTITY,
2727
style,
@@ -32,22 +32,21 @@ declare_lint_pass!(MapIdentity => [MAP_IDENTITY]);
3232

3333
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapIdentity {
3434
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
35-
// println!("start");
3635
if expr.span.from_expansion() {
3736
return;
3837
}
3938

4039
if_chain! {
41-
if let Some(func) = get_map_argument(cx, expr);
40+
if let Some([caller, func]) = get_map_argument(cx, expr);
4241
if let Some(body) = get_body(cx, func);
4342
if is_identity_function(cx, body);
4443
then {
4544
span_lint_and_sugg(
4645
cx,
4746
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`",
5150
String::new(),
5251
Applicability::MachineApplicable
5352
)
@@ -57,9 +56,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapIdentity {
5756
}
5857

5958

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>]> {
6362
if_chain! {
6463
if let ExprKind::MethodCall(ref method, _, ref args) = expr.kind;
6564
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<
6867
|| is_type_diagnostic_item(cx, caller_ty, sym!(result_type))
6968
|| is_type_diagnostic_item(cx, caller_ty, sym!(option_type));
7069
then {
71-
Some(&args[1])
70+
Some(args)
7271
} else {
7372
None
7473
}

tests/ui/map_identity.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-rustfix
2+
#![warn(clippy::map_identity)]
3+
#![allow(clippy::needless_return)]
4+
5+
fn main() {
6+
let x: [u16; 3] = [1, 2, 3];
7+
// should lint
8+
let _: Vec<_> = x.iter().collect();
9+
let _: Vec<_> = x.iter().collect();
10+
let _: Option<u8> = Some(3);
11+
// should not lint
12+
let _: Vec<_> = x.iter().map(|x| 2*x).collect();
13+
let _: Vec<_> = x.iter().map(not_identity).collect();
14+
}
15+
16+
#[allow(dead_code)]
17+
fn identity(x: &u16) -> &u16 {
18+
return x;
19+
}
20+
21+
fn not_identity(x: &u16) -> u16 {
22+
*x
23+
}

tests/ui/map_identity.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
#![warn(clippy::map_identity)]
23
#![allow(clippy::needless_return)]
34

@@ -12,6 +13,7 @@ fn main() {
1213
let _: Vec<_> = x.iter().map(not_identity).collect();
1314
}
1415

16+
#[allow(dead_code)]
1517
fn identity(x: &u16) -> &u16 {
1618
return x;
1719
}

tests/ui/map_identity.stderr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: unnecessary map of the identity function
2+
--> $DIR/map_identity.rs:8:29
3+
|
4+
LL | let _: Vec<_> = x.iter().map(|x| { return x }).collect();
5+
| ^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
6+
|
7+
= note: `-D clippy::map-identity` implied by `-D warnings`
8+
9+
error: unnecessary map of the identity function
10+
--> $DIR/map_identity.rs:9:29
11+
|
12+
LL | let _: Vec<_> = x.iter().map(identity).collect();
13+
| ^^^^^^^^^^^^^^ help: remove the call to `map`
14+
15+
error: unnecessary map of the identity function
16+
--> $DIR/map_identity.rs:10:32
17+
|
18+
LL | let _: Option<u8> = Some(3).map(|x| x);
19+
| ^^^^^^^^^^^ help: remove the call to `map`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)