Skip to content

Commit 4f65bec

Browse files
committed
Auto merge of #5084 - JohnTitor:clean-up-span-lint, r=flip1995
Clean up `span_lint` in `methods/mod.rs` Uses `span_help_and_lint` instead of `span_lint` and `span_lint_and_sugg` instead of `span_lint_and_then`. changelog: none
2 parents fa046d2 + 4b133f2 commit 4f65bec

File tree

8 files changed

+121
-78
lines changed

8 files changed

+121
-78
lines changed

clippy_lints/src/methods/mod.rs

+52-52
Original file line numberDiff line numberDiff line change
@@ -2133,14 +2133,12 @@ fn lint_iter_nth<'a, 'tcx>(
21332133
return; // caller is not a type that we want to lint
21342134
};
21352135

2136-
span_lint(
2136+
span_help_and_lint(
21372137
cx,
21382138
ITER_NTH,
21392139
expr.span,
2140-
&format!(
2141-
"called `.iter{0}().nth()` on a {1}. Calling `.get{0}()` is both faster and more readable",
2142-
mut_str, caller_type
2143-
),
2140+
&format!("called `.iter{0}().nth()` on a {1}", mut_str, caller_type),
2141+
&format!("calling `.get{}()` is both faster and more readable", mut_str),
21442142
);
21452143
}
21462144

@@ -2244,11 +2242,12 @@ fn lint_get_unwrap<'a, 'tcx>(
22442242
fn lint_iter_skip_next(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) {
22452243
// lint if caller of skip is an Iterator
22462244
if match_trait_method(cx, expr, &paths::ITERATOR) {
2247-
span_lint(
2245+
span_help_and_lint(
22482246
cx,
22492247
ITER_SKIP_NEXT,
22502248
expr.span,
2251-
"called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`",
2249+
"called `skip(x).next()` on an iterator",
2250+
"this is more succinctly expressed by calling `nth(x)`",
22522251
);
22532252
}
22542253
}
@@ -2304,15 +2303,15 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
23042303
};
23052304

23062305
if let Some((lint, kind, none_value)) = mess {
2307-
span_lint(
2306+
span_help_and_lint(
23082307
cx,
23092308
lint,
23102309
expr.span,
2310+
&format!("used `unwrap()` on `{}` value", kind,),
23112311
&format!(
2312-
"used `unwrap()` on `{}` value. If you don't want to handle the `{}` case gracefully, consider \
2313-
using `expect()` to provide a better panic \
2314-
message",
2315-
kind, none_value
2312+
"if you don't want to handle the `{}` case gracefully, consider \
2313+
using `expect()` to provide a better panic message",
2314+
none_value,
23162315
),
23172316
);
23182317
}
@@ -2331,14 +2330,12 @@ fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hi
23312330
};
23322331

23332332
if let Some((lint, kind, none_value)) = mess {
2334-
span_lint(
2333+
span_help_and_lint(
23352334
cx,
23362335
lint,
23372336
expr.span,
2338-
&format!(
2339-
"used `expect()` on `{}` value. If this value is an `{}` it will panic",
2340-
kind, none_value
2341-
),
2337+
&format!("used `expect()` on `{}` value", kind,),
2338+
&format!("if this value is an `{}`, it will panic", none_value,),
23422339
);
23432340
}
23442341
}
@@ -2353,11 +2350,12 @@ fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, ok_args: &[hir
23532350
if has_debug_impl(error_type, cx);
23542351

23552352
then {
2356-
span_lint(
2353+
span_help_and_lint(
23572354
cx,
23582355
OK_EXPECT,
23592356
expr.span,
2360-
"called `ok().expect()` on a `Result` value. You can call `expect()` directly on the `Result`",
2357+
"called `ok().expect()` on a `Result` value",
2358+
"you can call `expect()` directly on the `Result`",
23612359
);
23622360
}
23632361
}
@@ -2372,14 +2370,15 @@ fn lint_map_flatten<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<
23722370
let self_snippet = snippet(cx, map_args[0].span, "..");
23732371
let func_snippet = snippet(cx, map_args[1].span, "..");
23742372
let hint = format!("{0}.flat_map({1})", self_snippet, func_snippet);
2375-
span_lint_and_then(cx, MAP_FLATTEN, expr.span, msg, |db| {
2376-
db.span_suggestion(
2377-
expr.span,
2378-
"try using `flat_map` instead",
2379-
hint,
2380-
Applicability::MachineApplicable,
2381-
);
2382-
});
2373+
span_lint_and_sugg(
2374+
cx,
2375+
MAP_FLATTEN,
2376+
expr.span,
2377+
msg,
2378+
"try using `flat_map` instead",
2379+
hint,
2380+
Applicability::MachineApplicable,
2381+
);
23832382
}
23842383
}
23852384

@@ -2474,14 +2473,15 @@ fn lint_map_or_none<'a, 'tcx>(
24742473
let map_or_self_snippet = snippet(cx, map_or_args[0].span, "..");
24752474
let map_or_func_snippet = snippet(cx, map_or_args[2].span, "..");
24762475
let hint = format!("{0}.and_then({1})", map_or_self_snippet, map_or_func_snippet);
2477-
span_lint_and_then(cx, OPTION_MAP_OR_NONE, expr.span, msg, |db| {
2478-
db.span_suggestion(
2479-
expr.span,
2480-
"try using `and_then` instead",
2481-
hint,
2482-
Applicability::MachineApplicable, // snippet
2483-
);
2484-
});
2476+
span_lint_and_sugg(
2477+
cx,
2478+
OPTION_MAP_OR_NONE,
2479+
expr.span,
2480+
msg,
2481+
"try using `and_then` instead",
2482+
hint,
2483+
Applicability::MachineApplicable,
2484+
);
24852485
}
24862486
}
24872487
}
@@ -2607,9 +2607,9 @@ fn lint_filter_map<'a, 'tcx>(
26072607
) {
26082608
// lint if caller of `.filter().map()` is an Iterator
26092609
if match_trait_method(cx, expr, &paths::ITERATOR) {
2610-
let msg = "called `filter(p).map(q)` on an `Iterator`. \
2611-
This is more succinctly expressed by calling `.filter_map(..)` instead.";
2612-
span_lint(cx, FILTER_MAP, expr.span, msg);
2610+
let msg = "called `filter(p).map(q)` on an `Iterator`";
2611+
let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead";
2612+
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
26132613
}
26142614
}
26152615

@@ -2647,9 +2647,9 @@ fn lint_find_map<'a, 'tcx>(
26472647
) {
26482648
// lint if caller of `.filter().map()` is an Iterator
26492649
if match_trait_method(cx, &map_args[0], &paths::ITERATOR) {
2650-
let msg = "called `find(p).map(q)` on an `Iterator`. \
2651-
This is more succinctly expressed by calling `.find_map(..)` instead.";
2652-
span_lint(cx, FIND_MAP, expr.span, msg);
2650+
let msg = "called `find(p).map(q)` on an `Iterator`";
2651+
let hint = "this is more succinctly expressed by calling `.find_map(..)` instead";
2652+
span_help_and_lint(cx, FIND_MAP, expr.span, msg, hint);
26532653
}
26542654
}
26552655

@@ -2662,9 +2662,9 @@ fn lint_filter_map_map<'a, 'tcx>(
26622662
) {
26632663
// lint if caller of `.filter().map()` is an Iterator
26642664
if match_trait_method(cx, expr, &paths::ITERATOR) {
2665-
let msg = "called `filter_map(p).map(q)` on an `Iterator`. \
2666-
This is more succinctly expressed by only calling `.filter_map(..)` instead.";
2667-
span_lint(cx, FILTER_MAP, expr.span, msg);
2665+
let msg = "called `filter_map(p).map(q)` on an `Iterator`";
2666+
let hint = "this is more succinctly expressed by only calling `.filter_map(..)` instead";
2667+
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
26682668
}
26692669
}
26702670

@@ -2677,10 +2677,10 @@ fn lint_filter_flat_map<'a, 'tcx>(
26772677
) {
26782678
// lint if caller of `.filter().flat_map()` is an Iterator
26792679
if match_trait_method(cx, expr, &paths::ITERATOR) {
2680-
let msg = "called `filter(p).flat_map(q)` on an `Iterator`. \
2681-
This is more succinctly expressed by calling `.flat_map(..)` \
2682-
and filtering by returning an empty Iterator.";
2683-
span_lint(cx, FILTER_MAP, expr.span, msg);
2680+
let msg = "called `filter(p).flat_map(q)` on an `Iterator`";
2681+
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2682+
and filtering by returning `iter::empty()`";
2683+
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
26842684
}
26852685
}
26862686

@@ -2693,10 +2693,10 @@ fn lint_filter_map_flat_map<'a, 'tcx>(
26932693
) {
26942694
// lint if caller of `.filter_map().flat_map()` is an Iterator
26952695
if match_trait_method(cx, expr, &paths::ITERATOR) {
2696-
let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`. \
2697-
This is more succinctly expressed by calling `.flat_map(..)` \
2698-
and filtering by returning an empty Iterator.";
2699-
span_lint(cx, FILTER_MAP, expr.span, msg);
2696+
let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`";
2697+
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2698+
and filtering by returning `iter::empty()`";
2699+
span_help_and_lint(cx, FILTER_MAP, expr.span, msg, hint);
27002700
}
27012701
}
27022702

tests/ui/expect.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
error: used `expect()` on `an Option` value. If this value is an `None` it will panic
1+
error: used `expect()` on `an Option` value
22
--> $DIR/expect.rs:5:13
33
|
44
LL | let _ = opt.expect("");
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::option-expect-used` implied by `-D warnings`
8+
= help: if this value is an `None`, it will panic
89

9-
error: used `expect()` on `a Result` value. If this value is an `Err` it will panic
10+
error: used `expect()` on `a Result` value
1011
--> $DIR/expect.rs:10:13
1112
|
1213
LL | let _ = res.expect("");
1314
| ^^^^^^^^^^^^^^
1415
|
1516
= note: `-D clippy::result-expect-used` implied by `-D warnings`
17+
= help: if this value is an `Err`, it will panic
1618

1719
error: aborting due to 2 previous errors
1820

tests/ui/filter_methods.stderr

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
error: called `filter(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.filter_map(..)` instead.
1+
error: called `filter(p).map(q)` on an `Iterator`
22
--> $DIR/filter_methods.rs:5:21
33
|
44
LL | let _: Vec<_> = vec![5; 6].into_iter().filter(|&x| x == 0).map(|x| x * 2).collect();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::filter-map` implied by `-D warnings`
8+
= help: this is more succinctly expressed by calling `.filter_map(..)` instead
89

9-
error: called `filter(p).flat_map(q)` on an `Iterator`. This is more succinctly expressed by calling `.flat_map(..)` and filtering by returning an empty Iterator.
10+
error: called `filter(p).flat_map(q)` on an `Iterator`
1011
--> $DIR/filter_methods.rs:7:21
1112
|
1213
LL | let _: Vec<_> = vec![5_i8; 6]
@@ -15,8 +16,10 @@ LL | | .into_iter()
1516
LL | | .filter(|&x| x == 0)
1617
LL | | .flat_map(|x| x.checked_mul(2))
1718
| |_______________________________________^
19+
|
20+
= help: this is more succinctly expressed by calling `.flat_map(..)` and filtering by returning `iter::empty()`
1821

19-
error: called `filter_map(p).flat_map(q)` on an `Iterator`. This is more succinctly expressed by calling `.flat_map(..)` and filtering by returning an empty Iterator.
22+
error: called `filter_map(p).flat_map(q)` on an `Iterator`
2023
--> $DIR/filter_methods.rs:13:21
2124
|
2225
LL | let _: Vec<_> = vec![5_i8; 6]
@@ -25,8 +28,10 @@ LL | | .into_iter()
2528
LL | | .filter_map(|x| x.checked_mul(2))
2629
LL | | .flat_map(|x| x.checked_mul(2))
2730
| |_______________________________________^
31+
|
32+
= help: this is more succinctly expressed by calling `.flat_map(..)` and filtering by returning `iter::empty()`
2833

29-
error: called `filter_map(p).map(q)` on an `Iterator`. This is more succinctly expressed by only calling `.filter_map(..)` instead.
34+
error: called `filter_map(p).map(q)` on an `Iterator`
3035
--> $DIR/filter_methods.rs:19:21
3136
|
3237
LL | let _: Vec<_> = vec![5_i8; 6]
@@ -35,6 +40,8 @@ LL | | .into_iter()
3540
LL | | .filter_map(|x| x.checked_mul(2))
3641
LL | | .map(|x| x.checked_mul(2))
3742
| |__________________________________^
43+
|
44+
= help: this is more succinctly expressed by only calling `.filter_map(..)` instead
3845

3946
error: aborting due to 4 previous errors
4047

tests/ui/find_map.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
error: called `find(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead.
1+
error: called `find(p).map(q)` on an `Iterator`
22
--> $DIR/find_map.rs:20:26
33
|
44
LL | let _: Option<i32> = a.iter().find(|s| s.parse::<i32>().is_ok()).map(|s| s.parse().unwrap());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::find-map` implied by `-D warnings`
8+
= help: this is more succinctly expressed by calling `.find_map(..)` instead
89

9-
error: called `find(p).map(q)` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead.
10+
error: called `find(p).map(q)` on an `Iterator`
1011
--> $DIR/find_map.rs:22:29
1112
|
1213
LL | let _: Option<Flavor> = desserts_of_the_week
@@ -18,6 +19,8 @@ LL | | Dessert::Cake(_) => true,
1819
LL | | _ => unreachable!(),
1920
LL | | });
2021
| |__________^
22+
|
23+
= help: this is more succinctly expressed by calling `.find_map(..)` instead
2124

2225
error: aborting due to 2 previous errors
2326

tests/ui/iter_nth.stderr

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
1-
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
1+
error: called `.iter().nth()` on a Vec
22
--> $DIR/iter_nth.rs:33:23
33
|
44
LL | let bad_vec = some_vec.iter().nth(3);
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::iter-nth` implied by `-D warnings`
8+
= help: calling `.get()` is both faster and more readable
89

9-
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
10+
error: called `.iter().nth()` on a slice
1011
--> $DIR/iter_nth.rs:34:26
1112
|
1213
LL | let bad_slice = &some_vec[..].iter().nth(3);
1314
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: calling `.get()` is both faster and more readable
1417

15-
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
18+
error: called `.iter().nth()` on a slice
1619
--> $DIR/iter_nth.rs:35:31
1720
|
1821
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
1922
| ^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: calling `.get()` is both faster and more readable
2025

21-
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
26+
error: called `.iter().nth()` on a VecDeque
2227
--> $DIR/iter_nth.rs:36:29
2328
|
2429
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
2530
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= help: calling `.get()` is both faster and more readable
2633

27-
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
34+
error: called `.iter_mut().nth()` on a Vec
2835
--> $DIR/iter_nth.rs:41:23
2936
|
3037
LL | let bad_vec = some_vec.iter_mut().nth(3);
3138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= help: calling `.get_mut()` is both faster and more readable
3241

33-
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
42+
error: called `.iter_mut().nth()` on a slice
3443
--> $DIR/iter_nth.rs:44:26
3544
|
3645
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
3746
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= help: calling `.get_mut()` is both faster and more readable
3849

39-
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
50+
error: called `.iter_mut().nth()` on a VecDeque
4051
--> $DIR/iter_nth.rs:47:29
4152
|
4253
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
4354
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
|
56+
= help: calling `.get_mut()` is both faster and more readable
4457

4558
error: aborting due to 7 previous errors
4659

0 commit comments

Comments
 (0)