Skip to content

Upgrade map_flatten to complexity #8054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
LintId::of(methods::MANUAL_SPLIT_ONCE),
LintId::of(methods::MANUAL_STR_REPEAT),
LintId::of(methods::MAP_COLLECT_RESULT_UNIT),
LintId::of(methods::MAP_FLATTEN),
LintId::of(methods::MAP_IDENTITY),
LintId::of(methods::NEEDLESS_SPLITN),
LintId::of(methods::NEW_RET_NO_SELF),
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
LintId::of(methods::MANUAL_FILTER_MAP),
LintId::of(methods::MANUAL_FIND_MAP),
LintId::of(methods::MANUAL_SPLIT_ONCE),
LintId::of(methods::MAP_FLATTEN),
LintId::of(methods::MAP_IDENTITY),
LintId::of(methods::NEEDLESS_SPLITN),
LintId::of(methods::OPTION_AS_REF_DEREF),
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/lib.register_pedantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
LintId::of(methods::FROM_ITER_INSTEAD_OF_COLLECT),
LintId::of(methods::IMPLICIT_CLONE),
LintId::of(methods::INEFFICIENT_TO_STRING),
LintId::of(methods::MAP_FLATTEN),
LintId::of(methods::MAP_UNWRAP_OR),
LintId::of(misc::FLOAT_CMP),
LintId::of(misc::USED_UNDERSCORE_BINDING),
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.31.0"]
pub MAP_FLATTEN,
pedantic,
complexity,
"using combinations of `flatten` and `map` which can usually be written as a single method call"
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/option_env_unwrap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// aux-build:macro_rules.rs
#![warn(clippy::option_env_unwrap)]
#![allow(clippy::map_flatten)]

#[macro_use]
extern crate macro_rules;
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/option_env_unwrap.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this will panic at run-time if the environment variable doesn't exist at compile-time
--> $DIR/option_env_unwrap.rs:17:13
--> $DIR/option_env_unwrap.rs:18:13
|
LL | let _ = option_env!("PATH").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -8,15 +8,15 @@ LL | let _ = option_env!("PATH").unwrap();
= help: consider using the `env!` macro instead

error: this will panic at run-time if the environment variable doesn't exist at compile-time
--> $DIR/option_env_unwrap.rs:18:13
--> $DIR/option_env_unwrap.rs:19:13
|
LL | let _ = option_env!("PATH").expect("environment variable PATH isn't set");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using the `env!` macro instead

error: this will panic at run-time if the environment variable doesn't exist at compile-time
--> $DIR/option_env_unwrap.rs:9:9
--> $DIR/option_env_unwrap.rs:10:9
|
LL | option_env!($env).unwrap()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -28,7 +28,7 @@ LL | let _ = option_env_unwrap!("PATH");
= note: this error originates in the macro `option_env_unwrap` (in Nightly builds, run with -Z macro-backtrace for more info)

error: this will panic at run-time if the environment variable doesn't exist at compile-time
--> $DIR/option_env_unwrap.rs:12:9
--> $DIR/option_env_unwrap.rs:13:9
|
LL | option_env!($env).expect($message)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -40,7 +40,7 @@ LL | let _ = option_env_unwrap!("PATH", "environment variable PATH isn't set
= note: this error originates in the macro `option_env_unwrap` (in Nightly builds, run with -Z macro-backtrace for more info)

error: this will panic at run-time if the environment variable doesn't exist at compile-time
--> $DIR/option_env_unwrap.rs:21:13
--> $DIR/option_env_unwrap.rs:22:13
|
LL | let _ = option_env_unwrap_external!("PATH");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -49,7 +49,7 @@ LL | let _ = option_env_unwrap_external!("PATH");
= note: this error originates in the macro `option_env_unwrap_external` (in Nightly builds, run with -Z macro-backtrace for more info)

error: this will panic at run-time if the environment variable doesn't exist at compile-time
--> $DIR/option_env_unwrap.rs:22:13
--> $DIR/option_env_unwrap.rs:23:13
|
LL | let _ = option_env_unwrap_external!("PATH", "environment variable PATH isn't set");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/option_filter_map.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![warn(clippy::option_filter_map)]
// run-rustfix
fn odds_out(x: i32) -> Option<i32> {
if x % 2 == 0 { Some(x) } else { None }
}
#![warn(clippy::option_filter_map)]
#![allow(clippy::map_flatten)]

fn main() {
let _ = Some(Some(1)).flatten();
Expand All @@ -21,3 +19,7 @@ fn main() {
.map(odds_out)
.flatten();
}

fn odds_out(x: i32) -> Option<i32> {
if x % 2 == 0 { Some(x) } else { None }
}
10 changes: 6 additions & 4 deletions tests/ui/option_filter_map.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![warn(clippy::option_filter_map)]
// run-rustfix
fn odds_out(x: i32) -> Option<i32> {
if x % 2 == 0 { Some(x) } else { None }
}
#![warn(clippy::option_filter_map)]
#![allow(clippy::map_flatten)]

fn main() {
let _ = Some(Some(1)).filter(Option::is_some).map(Option::unwrap);
Expand All @@ -23,3 +21,7 @@ fn main() {
.filter(|o| o.is_some())
.map(|o| o.unwrap());
}

fn odds_out(x: i32) -> Option<i32> {
if x % 2 == 0 { Some(x) } else { None }
}
16 changes: 8 additions & 8 deletions tests/ui/option_filter_map.stderr
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:8:27
--> $DIR/option_filter_map.rs:6:27
|
LL | let _ = Some(Some(1)).filter(Option::is_some).map(Option::unwrap);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()`
|
= note: `-D clippy::option-filter-map` implied by `-D warnings`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:9:27
--> $DIR/option_filter_map.rs:7:27
|
LL | let _ = Some(Some(1)).filter(|o| o.is_some()).map(|o| o.unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:10:35
--> $DIR/option_filter_map.rs:8:35
|
LL | let _ = Some(1).map(odds_out).filter(Option::is_some).map(Option::unwrap);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:11:35
--> $DIR/option_filter_map.rs:9:35
|
LL | let _ = Some(1).map(odds_out).filter(|o| o.is_some()).map(|o| o.unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:13:39
--> $DIR/option_filter_map.rs:11:39
|
LL | let _ = vec![Some(1)].into_iter().filter(Option::is_some).map(Option::unwrap);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:14:39
--> $DIR/option_filter_map.rs:12:39
|
LL | let _ = vec![Some(1)].into_iter().filter(|o| o.is_some()).map(|o| o.unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:18:10
--> $DIR/option_filter_map.rs:16:10
|
LL | .filter(Option::is_some)
| __________^
LL | | .map(Option::unwrap);
| |____________________________^ help: consider using `flatten` instead: `flatten()`

error: `filter` for `Some` followed by `unwrap`
--> $DIR/option_filter_map.rs:23:10
--> $DIR/option_filter_map.rs:21:10
|
LL | .filter(|o| o.is_some())
| __________^
Expand Down