Skip to content

Commit d9ecbb7

Browse files
committed
clean-up
1 parent 973e596 commit d9ecbb7

File tree

4 files changed

+36
-44
lines changed

4 files changed

+36
-44
lines changed

clippy_lints/src/lines_filter_map_ok.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ impl LateLintPass<'_> for LinesFilterMapOk {
8787
cx,
8888
LINES_FILTER_MAP_OK,
8989
fm_span,
90-
format!("`{fm_method_name}()` will run forever if the iterator repeatedly produces an `Err`",),
90+
format!("`{fm_method_name}()` will run forever if the iterator repeatedly produces an `Err`"),
9191
|diag| {
9292
diag.span_note(
9393
fm_receiver.span,
94-
"this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error");
94+
"this expression returning a `std::io::Lines` may produce \
95+
an infinite number of `Err` in case of a read error",
96+
);
9597
diag.span_suggestion(
9698
fm_span,
9799
"replace with",
@@ -108,27 +110,21 @@ fn should_lint(cx: &LateContext<'_>, args: &[Expr<'_>], method_name: Symbol) ->
108110
match args {
109111
[] => method_name == sym::flatten,
110112
[fm_arg] => {
111-
match &fm_arg.kind {
113+
match fm_arg.kind {
112114
// Detect `Result::ok`
113-
ExprKind::Path(qpath) => cx
115+
ExprKind::Path(ref qpath) => cx
114116
.qpath_res(qpath, fm_arg.hir_id)
115-
.opt_def_id()
116-
.is_some_and(|did| cx.tcx.is_diagnostic_item(sym::result_ok_method, did)),
117+
.is_diag_item(cx, sym::result_ok_method),
117118
// Detect `|x| x.ok()`
118-
ExprKind::Closure(Closure { body, .. }) => {
119+
ExprKind::Closure(&Closure { body, .. }) => {
119120
if let Body {
120121
params: [param], value, ..
121-
} = cx.tcx.hir_body(*body)
122+
} = cx.tcx.hir_body(body)
122123
&& let ExprKind::MethodCall(method, receiver, [], _) = value.kind
123124
{
124125
method.ident.name == sym::ok
125126
&& receiver.res_local_id() == Some(param.pat.hir_id)
126-
&& cx
127-
.typeck_results()
128-
.type_dependent_def_id(value.hir_id)
129-
.opt_parent(cx)
130-
.opt_impl_ty(cx)
131-
.is_diag_item(cx, sym::Result)
127+
&& cx.ty_based_def(*value).is_diag_item(cx, sym::result_ok_method)
132128
} else {
133129
false
134130
}

tests/ui/lines_filter_map_ok.fixed

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
#![allow(unused, clippy::map_identity)]
1+
#![allow(clippy::map_identity)]
22
#![warn(clippy::lines_filter_map_ok)]
33

44
use std::io::{self, BufRead, BufReader};
55

66
fn main() -> io::Result<()> {
7+
// Lint:
8+
79
let f = std::fs::File::open("/")?;
8-
// Lint
910
BufReader::new(f).lines().map_while(Result::ok).for_each(|_| ());
1011
//~^ lines_filter_map_ok
11-
// Lint
1212
let f = std::fs::File::open("/")?;
1313
BufReader::new(f).lines().map_while(Result::ok).for_each(|_| ());
1414
//~^ lines_filter_map_ok
15-
// Lint
1615
let f = std::fs::File::open("/")?;
1716
BufReader::new(f).lines().map_while(Result::ok).for_each(|_| ());
1817
//~^ lines_filter_map_ok
1918

20-
let s = "foo\nbar\nbaz\n";
21-
// Lint
2219
io::stdin().lines().map_while(Result::ok).for_each(|_| ());
2320
//~^ lines_filter_map_ok
24-
// Lint
2521
io::stdin().lines().map_while(Result::ok).for_each(|_| ());
2622
//~^ lines_filter_map_ok
27-
// Lint
2823
io::stdin().lines().map_while(Result::ok).for_each(|_| ());
2924
//~^ lines_filter_map_ok
30-
// Do not lint (not a `Lines` iterator)
25+
26+
// Do not lint:
27+
28+
// not a `Lines` iterator
3129
io::stdin()
3230
.lines()
3331
.map(std::convert::identity)
3432
.filter_map(|x| x.ok())
3533
.for_each(|_| ());
36-
// Do not lint (not a `Result::ok()` extractor)
34+
// not a `Result::ok()` extractor
3735
io::stdin().lines().filter_map(|x| x.err()).for_each(|_| ());
3836
Ok(())
3937
}

tests/ui/lines_filter_map_ok.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
#![allow(unused, clippy::map_identity)]
1+
#![allow(clippy::map_identity)]
22
#![warn(clippy::lines_filter_map_ok)]
33

44
use std::io::{self, BufRead, BufReader};
55

66
fn main() -> io::Result<()> {
7+
// Lint:
8+
79
let f = std::fs::File::open("/")?;
8-
// Lint
910
BufReader::new(f).lines().filter_map(Result::ok).for_each(|_| ());
1011
//~^ lines_filter_map_ok
11-
// Lint
1212
let f = std::fs::File::open("/")?;
1313
BufReader::new(f).lines().flat_map(Result::ok).for_each(|_| ());
1414
//~^ lines_filter_map_ok
15-
// Lint
1615
let f = std::fs::File::open("/")?;
1716
BufReader::new(f).lines().flatten().for_each(|_| ());
1817
//~^ lines_filter_map_ok
1918

20-
let s = "foo\nbar\nbaz\n";
21-
// Lint
2219
io::stdin().lines().filter_map(Result::ok).for_each(|_| ());
2320
//~^ lines_filter_map_ok
24-
// Lint
2521
io::stdin().lines().filter_map(|x| x.ok()).for_each(|_| ());
2622
//~^ lines_filter_map_ok
27-
// Lint
2823
io::stdin().lines().flatten().for_each(|_| ());
2924
//~^ lines_filter_map_ok
30-
// Do not lint (not a `Lines` iterator)
25+
26+
// Do not lint:
27+
28+
// not a `Lines` iterator
3129
io::stdin()
3230
.lines()
3331
.map(std::convert::identity)
3432
.filter_map(|x| x.ok())
3533
.for_each(|_| ());
36-
// Do not lint (not a `Result::ok()` extractor)
34+
// not a `Result::ok()` extractor
3735
io::stdin().lines().filter_map(|x| x.err()).for_each(|_| ());
3836
Ok(())
3937
}

tests/ui/lines_filter_map_ok.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: `filter_map()` will run forever if the iterator repeatedly produces an `Err`
2-
--> tests/ui/lines_filter_map_ok.rs:9:31
2+
--> tests/ui/lines_filter_map_ok.rs:10:31
33
|
44
LL | BufReader::new(f).lines().filter_map(Result::ok).for_each(|_| ());
55
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `map_while(Result::ok)`
66
|
77
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
8-
--> tests/ui/lines_filter_map_ok.rs:9:5
8+
--> tests/ui/lines_filter_map_ok.rs:10:5
99
|
1010
LL | BufReader::new(f).lines().filter_map(Result::ok).for_each(|_| ());
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -25,49 +25,49 @@ LL | BufReader::new(f).lines().flat_map(Result::ok).for_each(|_| ());
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: `flatten()` will run forever if the iterator repeatedly produces an `Err`
28-
--> tests/ui/lines_filter_map_ok.rs:17:31
28+
--> tests/ui/lines_filter_map_ok.rs:16:31
2929
|
3030
LL | BufReader::new(f).lines().flatten().for_each(|_| ());
3131
| ^^^^^^^^^ help: replace with: `map_while(Result::ok)`
3232
|
3333
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
34-
--> tests/ui/lines_filter_map_ok.rs:17:5
34+
--> tests/ui/lines_filter_map_ok.rs:16:5
3535
|
3636
LL | BufReader::new(f).lines().flatten().for_each(|_| ());
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: `filter_map()` will run forever if the iterator repeatedly produces an `Err`
40-
--> tests/ui/lines_filter_map_ok.rs:22:25
40+
--> tests/ui/lines_filter_map_ok.rs:19:25
4141
|
4242
LL | io::stdin().lines().filter_map(Result::ok).for_each(|_| ());
4343
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `map_while(Result::ok)`
4444
|
4545
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
46-
--> tests/ui/lines_filter_map_ok.rs:22:5
46+
--> tests/ui/lines_filter_map_ok.rs:19:5
4747
|
4848
LL | io::stdin().lines().filter_map(Result::ok).for_each(|_| ());
4949
| ^^^^^^^^^^^^^^^^^^^
5050

5151
error: `filter_map()` will run forever if the iterator repeatedly produces an `Err`
52-
--> tests/ui/lines_filter_map_ok.rs:25:25
52+
--> tests/ui/lines_filter_map_ok.rs:21:25
5353
|
5454
LL | io::stdin().lines().filter_map(|x| x.ok()).for_each(|_| ());
5555
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `map_while(Result::ok)`
5656
|
5757
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
58-
--> tests/ui/lines_filter_map_ok.rs:25:5
58+
--> tests/ui/lines_filter_map_ok.rs:21:5
5959
|
6060
LL | io::stdin().lines().filter_map(|x| x.ok()).for_each(|_| ());
6161
| ^^^^^^^^^^^^^^^^^^^
6262

6363
error: `flatten()` will run forever if the iterator repeatedly produces an `Err`
64-
--> tests/ui/lines_filter_map_ok.rs:28:25
64+
--> tests/ui/lines_filter_map_ok.rs:23:25
6565
|
6666
LL | io::stdin().lines().flatten().for_each(|_| ());
6767
| ^^^^^^^^^ help: replace with: `map_while(Result::ok)`
6868
|
6969
note: this expression returning a `std::io::Lines` may produce an infinite number of `Err` in case of a read error
70-
--> tests/ui/lines_filter_map_ok.rs:28:5
70+
--> tests/ui/lines_filter_map_ok.rs:23:5
7171
|
7272
LL | io::stdin().lines().flatten().for_each(|_| ());
7373
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)