Skip to content

Commit fd30313

Browse files
committed
Cleaned up message and suggestion for lint_search_is_some
1 parent ee1b959 commit fd30313

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,10 +3053,10 @@ fn lint_search_is_some<'tcx>(
30533053
// lint if caller of search is an Iterator
30543054
if match_trait_method(cx, &is_some_args[0], &paths::ITERATOR) {
30553055
let msg = format!(
3056-
"called `is_some()` after searching an `Iterator` with {}. This is more succinctly \
3057-
expressed by calling `any()`.",
3056+
"called `is_some()` after searching an `Iterator` with {}",
30583057
search_method
30593058
);
3059+
let hint = "this is more succinctly expressed by calling `any()`";
30603060
let search_snippet = snippet(cx, search_args[1].span, "..");
30613061
if search_snippet.lines().count() <= 1 {
30623062
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
@@ -3084,15 +3084,15 @@ fn lint_search_is_some<'tcx>(
30843084
SEARCH_IS_SOME,
30853085
method_span.with_hi(expr.span.hi()),
30863086
&msg,
3087-
"try this",
3087+
"use `any()` instead",
30883088
format!(
30893089
"any({})",
30903090
any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
30913091
),
30923092
Applicability::MachineApplicable,
30933093
);
30943094
} else {
3095-
span_lint(cx, SEARCH_IS_SOME, expr.span, &msg);
3095+
span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, hint);
30963096
}
30973097
}
30983098
// lint if `find()` is called by `String` or `&str`
@@ -3109,17 +3109,15 @@ fn lint_search_is_some<'tcx>(
31093109
if is_string_or_str_slice(&search_args[0]);
31103110
if is_string_or_str_slice(&search_args[1]);
31113111
then {
3112-
let msg = "called `is_some()` after calling `find()` \
3113-
on a string. This is more succinctly expressed by calling \
3114-
`contains()`.";
3112+
let msg = "called `is_some()` after calling `find()` on a string";
31153113
let mut applicability = Applicability::MachineApplicable;
31163114
let find_arg = snippet_with_applicability(cx, search_args[1].span, "..", &mut applicability);
31173115
span_lint_and_sugg(
31183116
cx,
31193117
SEARCH_IS_SOME,
31203118
method_span.with_hi(expr.span.hi()),
31213119
msg,
3122-
"try this",
3120+
"use `contains()` instead",
31233121
format!("contains({})", find_arg),
31243122
applicability,
31253123
);

tests/ui/search_is_some.stderr

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
1+
error: called `is_some()` after searching an `Iterator` with find
22
--> $DIR/search_is_some.rs:13:13
33
|
44
LL | let _ = v.iter().find(|&x| {
@@ -9,8 +9,9 @@ LL | | ).is_some();
99
| |______________________________^
1010
|
1111
= note: `-D clippy::search-is-some` implied by `-D warnings`
12+
= help: this is more succinctly expressed by calling `any()`
1213

13-
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
14+
error: called `is_some()` after searching an `Iterator` with position
1415
--> $DIR/search_is_some.rs:19:13
1516
|
1617
LL | let _ = v.iter().position(|&x| {
@@ -19,8 +20,10 @@ LL | | x < 0
1920
LL | | }
2021
LL | | ).is_some();
2122
| |______________________________^
23+
|
24+
= help: this is more succinctly expressed by calling `any()`
2225

23-
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
26+
error: called `is_some()` after searching an `Iterator` with rposition
2427
--> $DIR/search_is_some.rs:25:13
2528
|
2629
LL | let _ = v.iter().rposition(|&x| {
@@ -29,6 +32,8 @@ LL | | x < 0
2932
LL | | }
3033
LL | | ).is_some();
3134
| |______________________________^
35+
|
36+
= help: this is more succinctly expressed by calling `any()`
3237

3338
error: use of a blacklisted/placeholder name `foo`
3439
--> $DIR/search_is_some.rs:31:9
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
1-
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
1+
error: called `is_some()` after searching an `Iterator` with find
22
--> $DIR/search_is_some_fixable.rs:10:22
33
|
44
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| *x < 0)`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| *x < 0)`
66
|
77
= note: `-D clippy::search-is-some` implied by `-D warnings`
88

9-
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
9+
error: called `is_some()` after searching an `Iterator` with find
1010
--> $DIR/search_is_some_fixable.rs:11:20
1111
|
1212
LL | let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| **y == x)`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| **y == x)`
1414

15-
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
15+
error: called `is_some()` after searching an `Iterator` with find
1616
--> $DIR/search_is_some_fixable.rs:12:20
1717
|
1818
LL | let _ = (0..1).find(|x| *x == 0).is_some();
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| x == 0)`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| x == 0)`
2020

21-
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
21+
error: called `is_some()` after searching an `Iterator` with find
2222
--> $DIR/search_is_some_fixable.rs:13:22
2323
|
2424
LL | let _ = v.iter().find(|x| **x == 0).is_some();
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| *x == 0)`
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| *x == 0)`
2626

27-
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
27+
error: called `is_some()` after searching an `Iterator` with position
2828
--> $DIR/search_is_some_fixable.rs:16:22
2929
|
3030
LL | let _ = v.iter().position(|&x| x < 0).is_some();
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|&x| x < 0)`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|&x| x < 0)`
3232

33-
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
33+
error: called `is_some()` after searching an `Iterator` with rposition
3434
--> $DIR/search_is_some_fixable.rs:19:22
3535
|
3636
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|&x| x < 0)`
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|&x| x < 0)`
3838

39-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
39+
error: called `is_some()` after calling `find()` on a string
4040
--> $DIR/search_is_some_fixable.rs:24:27
4141
|
4242
LL | let _ = "hello world".find("world").is_some();
43-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains("world")`
43+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
4444

45-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
45+
error: called `is_some()` after calling `find()` on a string
4646
--> $DIR/search_is_some_fixable.rs:25:27
4747
|
4848
LL | let _ = "hello world".find(&s2).is_some();
49-
| ^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2)`
49+
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
5050

51-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
51+
error: called `is_some()` after calling `find()` on a string
5252
--> $DIR/search_is_some_fixable.rs:26:27
5353
|
5454
LL | let _ = "hello world".find(&s2[2..]).is_some();
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2[2..])`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
5656

57-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
57+
error: called `is_some()` after calling `find()` on a string
5858
--> $DIR/search_is_some_fixable.rs:28:16
5959
|
6060
LL | let _ = s1.find("world").is_some();
61-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains("world")`
61+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
6262

63-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
63+
error: called `is_some()` after calling `find()` on a string
6464
--> $DIR/search_is_some_fixable.rs:29:16
6565
|
6666
LL | let _ = s1.find(&s2).is_some();
67-
| ^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2)`
67+
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
6868

69-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
69+
error: called `is_some()` after calling `find()` on a string
7070
--> $DIR/search_is_some_fixable.rs:30:16
7171
|
7272
LL | let _ = s1.find(&s2[2..]).is_some();
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2[2..])`
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
7474

75-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
75+
error: called `is_some()` after calling `find()` on a string
7676
--> $DIR/search_is_some_fixable.rs:32:21
7777
|
7878
LL | let _ = s1[2..].find("world").is_some();
79-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains("world")`
79+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains("world")`
8080

81-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
81+
error: called `is_some()` after calling `find()` on a string
8282
--> $DIR/search_is_some_fixable.rs:33:21
8383
|
8484
LL | let _ = s1[2..].find(&s2).is_some();
85-
| ^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2)`
85+
| ^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2)`
8686

87-
error: called `is_some()` after calling `find()` on a string. This is more succinctly expressed by calling `contains()`.
87+
error: called `is_some()` after calling `find()` on a string
8888
--> $DIR/search_is_some_fixable.rs:34:21
8989
|
9090
LL | let _ = s1[2..].find(&s2[2..]).is_some();
91-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `contains(&s2[2..])`
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
9292

9393
error: aborting due to 15 previous errors
9494

0 commit comments

Comments
 (0)