Skip to content

Commit d51136d

Browse files
committed
Use 'span_lint_and_sugg'
1 parent 3a65e4e commit d51136d

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,17 @@ fn lint_flat_map_identity<'a, 'tcx>(
21762176
let arg_node = &flat_map_args[1].node;
21772177

21782178
let apply_lint = |message: &str| {
2179-
span_lint(cx, FLAT_MAP_IDENTITY, expr.span, message);
2179+
if let hir::ExprKind::MethodCall(_, span, _) = &expr.node {
2180+
span_lint_and_sugg(
2181+
cx,
2182+
FLAT_MAP_IDENTITY,
2183+
*span,
2184+
message,
2185+
"try",
2186+
"flatten()".to_string(),
2187+
Applicability::MachineApplicable,
2188+
);
2189+
}
21802190
};
21812191

21822192
if_chain! {
@@ -2190,8 +2200,7 @@ fn lint_flat_map_identity<'a, 'tcx>(
21902200
if path.segments[0].ident.as_str() == binding_ident.as_str();
21912201

21922202
then {
2193-
apply_lint("called `flat_map(|x| x)` on an `Iterator`. \
2194-
This can be simplified by calling `flatten().`");
2203+
apply_lint("called `flat_map(|x| x)` on an `Iterator`");
21952204
}
21962205
}
21972206

@@ -2201,8 +2210,7 @@ fn lint_flat_map_identity<'a, 'tcx>(
22012210
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
22022211

22032212
then {
2204-
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`. \
2205-
This can be simplified by calling `flatten().`");
2213+
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
22062214
}
22072215
}
22082216
}

tests/ui/unnecessary_flat_map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#![warn(clippy::flat_map_identity)]
24

35
use std::convert;

tests/ui/unnecessary_flat_map.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: called `flat_map(|x| x)` on an `Iterator`. This can be simplified by calling `flatten().`
2-
--> $DIR/unnecessary_flat_map.rs:7:5
1+
error: called `flat_map(|x| x)` on an `Iterator`
2+
--> $DIR/unnecessary_flat_map.rs:9:14
33
|
44
LL | iterator.flat_map(|x| x);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^ help: try: `flatten()`
66
|
77
= note: `-D clippy::flat-map-identity` implied by `-D warnings`
88

9-
error: called `flat_map(std::convert::identity)` on an `Iterator`. This can be simplified by calling `flatten().`
10-
--> $DIR/unnecessary_flat_map.rs:10:23
9+
error: called `flat_map(std::convert::identity)` on an `Iterator`
10+
--> $DIR/unnecessary_flat_map.rs:12:14
1111
|
1212
LL | iterator.flat_map(convert::identity);
13-
| ^^^^^^^^^^^^^^^^^
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
1414

1515
error: aborting due to 2 previous errors
1616

0 commit comments

Comments
 (0)