Skip to content

Commit

Permalink
Rename lint map_unwrap to map_unwrap_or and register lints as ren…
Browse files Browse the repository at this point in the history
…amed
  • Loading branch information
ThibsG committed May 4, 2020
1 parent ba76700 commit d75c258
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ Released 2018-09-13
[`map_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
[`map_entry`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
[`map_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten
[`map_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap
[`map_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
[`match_as_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_as_ref
[`match_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_bool
[`match_on_vec_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_on_vec_items
Expand Down
15 changes: 13 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&methods::ITER_SKIP_NEXT,
&methods::MANUAL_SATURATING_ARITHMETIC,
&methods::MAP_FLATTEN,
&methods::MAP_UNWRAP,
&methods::MAP_UNWRAP_OR,
&methods::NEW_RET_NO_SELF,
&methods::OK_EXPECT,
&methods::OPTION_AND_THEN_SOME,
Expand Down Expand Up @@ -1138,7 +1138,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&methods::FIND_MAP),
LintId::of(&methods::INEFFICIENT_TO_STRING),
LintId::of(&methods::MAP_FLATTEN),
LintId::of(&methods::MAP_UNWRAP),
LintId::of(&methods::MAP_UNWRAP_OR),
LintId::of(&misc::USED_UNDERSCORE_BINDING),
LintId::of(&misc_early::UNSEPARATED_LITERAL_SUFFIX),
LintId::of(&mut_mut::MUT_MUT),
Expand Down Expand Up @@ -1770,6 +1770,17 @@ pub fn register_renamed(ls: &mut rustc_lint::LintStore) {
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
ls.register_renamed("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes");
ls.register_renamed("clippy::block_in_if_condition_expr", "clippy::block_in_if_condition");
ls.register_renamed("clippy::block_in_if_condition_stmt", "clippy::block_in_if_condition");
ls.register_renamed("clippy::option_map_unwrap_or", "clippy::map_unwrap_or");
ls.register_renamed("clippy::option_map_unwrap_or_else", "clippy::map_unwrap_or");
ls.register_renamed("clippy::result_map_unwrap_or_else", "clippy::map_unwrap_or");
ls.register_renamed("clippy::option_unwrap_used", "clippy::unwrap_used");
ls.register_renamed("clippy::result_unwrap_used", "clippy::unwrap_used");
ls.register_renamed("clippy::option_expect_used", "clippy::expect_used");
ls.register_renamed("clippy::result_expect_used", "clippy::expect_used");
ls.register_renamed("clippy::for_loop_over_option", "clippy::for_loop_over_fallible");
ls.register_renamed("clippy::for_loop_over_result", "clippy::for_loop_over_fallible");
}

// only exists to let the dogfood integration test works.
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ declare_clippy_lint! {
/// // Good
/// x.map_or_else(some_function, |a| a + 1);
/// ```
pub MAP_UNWRAP,
pub MAP_UNWRAP_OR,
pedantic,
"using `.map(f).unwrap_or(a)` or `.map(f).unwrap_or_else(func)`, which are more succinctly expressed as `map_or(a, f)` or `map_or_else(a, f)`"
}
Expand Down Expand Up @@ -1240,7 +1240,7 @@ declare_lint_pass!(Methods => [
WRONG_SELF_CONVENTION,
WRONG_PUB_SELF_CONVENTION,
OK_EXPECT,
MAP_UNWRAP,
MAP_UNWRAP_OR,
RESULT_MAP_OR_INTO_OPTION,
OPTION_MAP_OR_NONE,
OPTION_AND_THEN_SOME,
Expand Down Expand Up @@ -2512,7 +2512,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
if same_span && !multiline {
span_lint_and_note(
cx,
MAP_UNWRAP,
MAP_UNWRAP_OR,
expr.span,
msg,
None,
Expand All @@ -2522,7 +2522,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
),
);
} else if same_span && multiline {
span_lint(cx, MAP_UNWRAP, expr.span, msg);
span_lint(cx, MAP_UNWRAP_OR, expr.span, msg);
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/option_map_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_middle::hir::map::Map;
use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol;

use super::MAP_UNWRAP;
use super::MAP_UNWRAP_OR;

/// lint use of `map().unwrap_or()` for `Option`s
pub(super) fn lint<'a, 'tcx>(
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(super) fn lint<'a, 'tcx>(
arg, suggest
);

span_lint_and_then(cx, MAP_UNWRAP, expr.span, msg, |diag| {
span_lint_and_then(cx, MAP_UNWRAP_OR, expr.span, msg, |diag| {
let map_arg_span = map_args[1].span;

let mut suggestion = vec![
Expand Down
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
module: "methods",
},
Lint {
name: "map_unwrap",
name: "map_unwrap_or",
group: "pedantic",
desc: "using `.map(f).unwrap_or(a)` or `.map(f).unwrap_or_else(func)`, which are more succinctly expressed as `map_or(a, f)` or `map_or_else(a, f)`",
deprecation: None,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/map_unwrap.rs → tests/ui/map_unwrap_or.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// FIXME: Add "run-rustfix" once it's supported for multipart suggestions
// aux-build:option_helpers.rs

#![warn(clippy::map_unwrap)]
#![warn(clippy::map_unwrap_or)]

#[macro_use]
extern crate option_helpers;
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/map_unwrap.stderr → tests/ui/map_unwrap_or.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/map_unwrap.rs:17:13
--> $DIR/map_unwrap_or.rs:17:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
LL | | // Should lint even though this call is on a separate line.
LL | | .unwrap_or(0);
| |_____________________^
|
= note: `-D clippy::map-unwrap` implied by `-D warnings`
= note: `-D clippy::map-unwrap-or` implied by `-D warnings`
help: use `map_or(a, f)` instead
|
LL | let _ = opt.map_or(0, |x| x + 1);
| ^^^^^^ ^^ --

error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/map_unwrap.rs:21:13
--> $DIR/map_unwrap_or.rs:21:13
|
LL | let _ = opt.map(|x| {
| _____________^
Expand All @@ -32,7 +32,7 @@ LL | );
|

error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/map_unwrap.rs:25:13
--> $DIR/map_unwrap_or.rs:25:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
Expand All @@ -49,7 +49,7 @@ LL | }, |x| x + 1);
|

error: called `map(f).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(f)` instead
--> $DIR/map_unwrap.rs:30:13
--> $DIR/map_unwrap_or.rs:30:13
|
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -60,7 +60,7 @@ LL | let _ = opt.and_then(|x| Some(x + 1));
| ^^^^^^^^ --

error: called `map(f).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(f)` instead
--> $DIR/map_unwrap.rs:32:13
--> $DIR/map_unwrap_or.rs:32:13
|
LL | let _ = opt.map(|x| {
| _____________^
Expand All @@ -78,7 +78,7 @@ LL | );
|

error: called `map(f).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(f)` instead
--> $DIR/map_unwrap.rs:36:13
--> $DIR/map_unwrap_or.rs:36:13
|
LL | let _ = opt
| _____________^
Expand All @@ -92,7 +92,7 @@ LL | .and_then(|x| Some(x + 1));
| ^^^^^^^^ --

error: called `map(f).unwrap_or(a)` on an `Option` value. This can be done more directly by calling `map_or(a, f)` instead
--> $DIR/map_unwrap.rs:47:13
--> $DIR/map_unwrap_or.rs:47:13
|
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -103,7 +103,7 @@ LL | let _ = Some("prefix").map_or(id, |p| format!("{}.", p));
| ^^^^^^ ^^^ --

error: called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling `map_or_else(g, f)` instead
--> $DIR/map_unwrap.rs:51:13
--> $DIR/map_unwrap_or.rs:51:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
Expand All @@ -114,7 +114,7 @@ LL | | .unwrap_or_else(|| 0);
= note: replace `map(|x| x + 1).unwrap_or_else(|| 0)` with `map_or_else(|| 0, |x| x + 1)`

error: called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling `map_or_else(g, f)` instead
--> $DIR/map_unwrap.rs:55:13
--> $DIR/map_unwrap_or.rs:55:13
|
LL | let _ = opt.map(|x| {
| _____________^
Expand All @@ -124,7 +124,7 @@ LL | | ).unwrap_or_else(|| 0);
| |__________________________^

error: called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling `map_or_else(g, f)` instead
--> $DIR/map_unwrap.rs:59:13
--> $DIR/map_unwrap_or.rs:59:13
|
LL | let _ = opt.map(|x| x + 1)
| _____________^
Expand All @@ -134,23 +134,23 @@ LL | | );
| |_________^

error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/map_unwrap.rs:88:13
--> $DIR/map_unwrap_or.rs:88:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`

error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/map_unwrap.rs:90:13
--> $DIR/map_unwrap_or.rs:90:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `map(|x| x + 1).unwrap_or_else(|e| 0)` with `map_or_else(|e| 0, |x| x + 1)`

error: called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling `.map_or_else(g, f)` instead
--> $DIR/map_unwrap.rs:91:13
--> $DIR/map_unwrap_or.rs:91:13
|
LL | let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit d75c258

Please sign in to comment.