Skip to content

Commit 05f8b58

Browse files
author
Eric Loren
committed
get all tests passing pending some questions
1 parent a397d94 commit 05f8b58

File tree

6 files changed

+36
-50
lines changed

6 files changed

+36
-50
lines changed

clippy_lints/src/methods/mod.rs

+22-32
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ use crate::utils::usage::mutated_variables;
3434
use crate::utils::{
3535
contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
3636
in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path,
37-
match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args,
38-
path_to_local_id, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_block,
39-
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
40-
span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth, SpanlessEq,
37+
match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths,
38+
remove_blocks, return_ty, single_segment_path, snippet, snippet_block, snippet_with_applicability,
39+
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs,
40+
sugg, walk_ptrs_ty_depth, SpanlessEq,
4141
};
42+
use clippy_utils::paths::{OPTION_IS_SOME, OPTION_UNWRAP};
4243

4344
declare_clippy_lint! {
4445
/// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
@@ -3213,17 +3214,12 @@ fn is_method<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_path: &[
32133214
}
32143215
}
32153216

3216-
// these two paths can't be checked and thus aren't in `paths`
3217-
const OPTION_IS_SOME: [&str; 4] = ["core", "option", "Option", "is_some"];
3218-
const OPTION_UNWRAP: [&str; 4] = ["core", "option", "Option", "unwrap"];
3219-
32203217
fn is_option_filter_map<'tcx>(
32213218
cx: &LateContext<'tcx>,
32223219
filter_arg: &'tcx hir::Expr<'_>,
32233220
map_arg: &'tcx hir::Expr<'_>,
32243221
) -> bool {
3225-
is_method(cx, map_arg, &OPTION_UNWRAP, sym!(unwrap)) &&
3226-
is_method(cx, filter_arg, &OPTION_IS_SOME, sym!(is_some))
3222+
is_method(cx, map_arg, &OPTION_UNWRAP, sym!(unwrap)) && is_method(cx, filter_arg, &OPTION_IS_SOME, sym!(is_some))
32273223
}
32283224

32293225
/// lint use of `filter().map()` for `Iterators`
@@ -3238,28 +3234,22 @@ fn lint_filter_some_map_unwrap<'tcx>(
32383234
let iterator = match_trait_method(cx, expr, &paths::ITERATOR);
32393235
let iterator_or_option =
32403236
iterator || is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&filter_recv), sym::option_type);
3241-
if iterator_or_option {
3242-
if is_option_filter_map(cx, filter_arg, map_arg) {
3243-
let msg = "`filter` for `Some` followed by `unwrap`";
3244-
let help = "consider using `flatten` instead";
3245-
let sugg = format!(
3246-
"{}.flatten()",
3247-
snippet_block(cx, filter_recv.span, "..", Some(target_span))
3248-
);
3249-
span_lint_and_sugg(
3250-
cx,
3251-
OPTION_FILTER_MAP,
3252-
expr.span.with_hi(expr.span.hi()),
3253-
msg,
3254-
help,
3255-
sugg,
3256-
Applicability::MachineApplicable,
3257-
);
3258-
} else if iterator {
3259-
let msg = "called `filter(..).map(..)` on an `Iterator`";
3260-
let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead";
3261-
span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
3262-
}
3237+
if iterator_or_option && is_option_filter_map(cx, filter_arg, map_arg) {
3238+
let msg = "`filter` for `Some` followed by `unwrap`";
3239+
let help = "consider using `flatten` instead";
3240+
let sugg = format!(
3241+
"{}.flatten()",
3242+
snippet_block(cx, filter_recv.span, "..", Some(target_span))
3243+
);
3244+
span_lint_and_sugg(
3245+
cx,
3246+
OPTION_FILTER_MAP,
3247+
expr.span.with_hi(expr.span.hi()),
3248+
msg,
3249+
help,
3250+
sugg,
3251+
Applicability::MachineApplicable,
3252+
);
32633253
}
32643254
}
32653255

clippy_utils/src/paths.rs

+2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"];
8787
pub const OPS_MODULE: [&str; 2] = ["core", "ops"];
8888
pub const OPTION: [&str; 3] = ["core", "option", "Option"];
8989
pub const OPTION_NONE: [&str; 4] = ["core", "option", "Option", "None"];
90+
pub const OPTION_IS_SOME: [&str; 4] = ["core", "option", "Option", "is_some"];
9091
pub const OPTION_SOME: [&str; 4] = ["core", "option", "Option", "Some"];
92+
pub const OPTION_UNWRAP: [&str; 4] = ["core", "option", "Option", "unwrap"];
9193
pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
9294
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
9395
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];

tests/ui/filter_methods.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
error: called `filter(..).map(..)` on an `Iterator`
2-
--> $DIR/filter_methods.rs:6:21
3-
|
4-
LL | let _: Vec<_> = vec![5; 6].into_iter().filter(|&x| x == 0).map(|x| x * 2).collect();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: `-D clippy::filter-map` implied by `-D warnings`
8-
= help: this is more succinctly expressed by calling `.filter_map(..)` instead
9-
101
error: called `filter(..).flat_map(..)` on an `Iterator`
112
--> $DIR/filter_methods.rs:8:21
123
|
@@ -17,6 +8,7 @@ LL | | .filter(|&x| x == 0)
178
LL | | .flat_map(|x| x.checked_mul(2))
189
| |_______________________________________^
1910
|
11+
= note: `-D clippy::filter-map` implied by `-D warnings`
2012
= help: this is more succinctly expressed by calling `.flat_map(..)` and filtering by returning `iter::empty()`
2113

2214
error: called `filter_map(..).flat_map(..)` on an `Iterator`
@@ -43,5 +35,5 @@ LL | | .map(|x| x.checked_mul(2))
4335
|
4436
= help: this is more succinctly expressed by only calling `.filter_map(..)` instead
4537

46-
error: aborting due to 4 previous errors
38+
error: aborting due to 3 previous errors
4739

tests/ui/option_filter_map.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![warn(clippy::option_filter_map)]
12
// run-rustfix
23
fn odds_out(x: i32) -> Option<i32> {
34
if x % 2 == 0 {

tests/ui/option_filter_map.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![warn(clippy::option_filter_map)]
12
// run-rustfix
23
fn odds_out(x: i32) -> Option<i32> {
34
if x % 2 == 0 {

tests/ui/option_filter_map.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
error: `filter` for `Some` followed by `unwrap`
2-
--> $DIR/option_filter_map.rs:11:13
2+
--> $DIR/option_filter_map.rs:12:13
33
|
44
LL | let _ = Some(Some(1)).filter(Option::is_some).map(Option::unwrap);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `Some(Some(1)).flatten()`
66
|
77
= note: `-D clippy::option-filter-map` implied by `-D warnings`
88

99
error: `filter` for `Some` followed by `unwrap`
10-
--> $DIR/option_filter_map.rs:12:13
10+
--> $DIR/option_filter_map.rs:13:13
1111
|
1212
LL | let _ = Some(Some(1)).filter(|o| o.is_some()).map(|o| o.unwrap());
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `Some(Some(1)).flatten()`
1414

1515
error: `filter` for `Some` followed by `unwrap`
16-
--> $DIR/option_filter_map.rs:13:13
16+
--> $DIR/option_filter_map.rs:14:13
1717
|
1818
LL | let _ = Some(1).map(odds_out).filter(Option::is_some).map(Option::unwrap);
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `Some(1).map(odds_out).flatten()`
2020

2121
error: `filter` for `Some` followed by `unwrap`
22-
--> $DIR/option_filter_map.rs:14:13
22+
--> $DIR/option_filter_map.rs:15:13
2323
|
2424
LL | let _ = Some(1).map(odds_out).filter(|o| o.is_some()).map(|o| o.unwrap());
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `Some(1).map(odds_out).flatten()`
2626

2727
error: `filter` for `Some` followed by `unwrap`
28-
--> $DIR/option_filter_map.rs:16:13
28+
--> $DIR/option_filter_map.rs:17:13
2929
|
3030
LL | let _ = vec![Some(1)].into_iter().filter(Option::is_some).map(Option::unwrap);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `vec![Some(1)].into_iter().flatten()`
3232

3333
error: `filter` for `Some` followed by `unwrap`
34-
--> $DIR/option_filter_map.rs:17:13
34+
--> $DIR/option_filter_map.rs:18:13
3535
|
3636
LL | let _ = vec![Some(1)].into_iter().filter(|o| o.is_some()).map(|o| o.unwrap());
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `vec![Some(1)].into_iter().flatten()`
3838

3939
error: `filter` for `Some` followed by `unwrap`
40-
--> $DIR/option_filter_map.rs:18:13
40+
--> $DIR/option_filter_map.rs:19:13
4141
|
4242
LL | let _ = vec![1]
4343
| _____________^
@@ -55,7 +55,7 @@ LL | .map(odds_out).flatten();
5555
|
5656

5757
error: `filter` for `Some` followed by `unwrap`
58-
--> $DIR/option_filter_map.rs:23:13
58+
--> $DIR/option_filter_map.rs:24:13
5959
|
6060
LL | let _ = vec![1]
6161
| _____________^

0 commit comments

Comments
 (0)