Skip to content

Commit

Permalink
Auto merge of #5292 - jpospychala:map-placeholder, r=flip1995
Browse files Browse the repository at this point in the history
Improve placeholder in map_unit_fn

Instead of using `_` as a default placeholder use `a`.

fixes #5180
changelog: Improve default placeholder in map_unit_fn
  • Loading branch information
bors committed Mar 9, 2020
2 parents d8f64b6 + 697e3c8 commit 64b7624
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/map_unit_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ fn unit_closure<'a, 'tcx>(
/// `x.field` => `x_field`
/// `y` => `_y`
///
/// Anything else will return `_`.
/// Anything else will return `a`.
fn let_binding_name(cx: &LateContext<'_, '_>, var_arg: &hir::Expr<'_>) -> String {
match &var_arg.kind {
hir::ExprKind::Field(_, _) => snippet(cx, var_arg.span, "_").replace(".", "_"),
hir::ExprKind::Path(_) => format!("_{}", snippet(cx, var_arg.span, "")),
_ => "_".to_string(),
_ => "a".to_string(),
}
}

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/option_map_unit_fn_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ fn plus_one(value: usize) -> usize {
value + 1
}

fn option() -> Option<usize> {
Some(10)
}

struct HasOption {
field: Option<usize>,
}
Expand Down Expand Up @@ -73,6 +77,8 @@ fn option_map_unit_fn() {
if let Some(value) = x.field { plus_one(value + captured); }


if let Some(ref value) = x.field { do_nothing(value + captured) }}
if let Some(ref value) = x.field { do_nothing(value + captured) }

if let Some(a) = option() { do_nothing(a) }}

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/option_map_unit_fn_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ fn plus_one(value: usize) -> usize {
value + 1
}

fn option() -> Option<usize> {
Some(10)
}

struct HasOption {
field: Option<usize>,
}
Expand Down Expand Up @@ -73,6 +77,8 @@ fn option_map_unit_fn() {
x.field.map(|value| { { plus_one(value + captured); } });


x.field.map(|ref value| { do_nothing(value + captured) });}
x.field.map(|ref value| { do_nothing(value + captured) });

option().map(do_nothing);}

fn main() {}
46 changes: 27 additions & 19 deletions tests/ui/option_map_unit_fn_fixable.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:34:5
--> $DIR/option_map_unit_fn_fixable.rs:38:5
|
LL | x.field.map(do_nothing);
| ^^^^^^^^^^^^^^^^^^^^^^^-
Expand All @@ -9,132 +9,140 @@ LL | x.field.map(do_nothing);
= note: `-D clippy::option-map-unit-fn` implied by `-D warnings`

error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:36:5
--> $DIR/option_map_unit_fn_fixable.rs:40:5
|
LL | x.field.map(do_nothing);
| ^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(x_field) = x.field { do_nothing(x_field) }`

error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:38:5
--> $DIR/option_map_unit_fn_fixable.rs:42:5
|
LL | x.field.map(diverge);
| ^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(x_field) = x.field { diverge(x_field) }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:44:5
--> $DIR/option_map_unit_fn_fixable.rs:48:5
|
LL | x.field.map(|value| x.do_option_nothing(value + captured));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { x.do_option_nothing(value + captured) }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:46:5
--> $DIR/option_map_unit_fn_fixable.rs:50:5
|
LL | x.field.map(|value| { x.do_option_plus_one(value + captured); });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { x.do_option_plus_one(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:49:5
--> $DIR/option_map_unit_fn_fixable.rs:53:5
|
LL | x.field.map(|value| do_nothing(value + captured));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured) }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:51:5
--> $DIR/option_map_unit_fn_fixable.rs:55:5
|
LL | x.field.map(|value| { do_nothing(value + captured) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured) }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:53:5
--> $DIR/option_map_unit_fn_fixable.rs:57:5
|
LL | x.field.map(|value| { do_nothing(value + captured); });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:55:5
--> $DIR/option_map_unit_fn_fixable.rs:59:5
|
LL | x.field.map(|value| { { do_nothing(value + captured); } });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:58:5
--> $DIR/option_map_unit_fn_fixable.rs:62:5
|
LL | x.field.map(|value| diverge(value + captured));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { diverge(value + captured) }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:60:5
--> $DIR/option_map_unit_fn_fixable.rs:64:5
|
LL | x.field.map(|value| { diverge(value + captured) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { diverge(value + captured) }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:62:5
--> $DIR/option_map_unit_fn_fixable.rs:66:5
|
LL | x.field.map(|value| { diverge(value + captured); });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { diverge(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:64:5
--> $DIR/option_map_unit_fn_fixable.rs:68:5
|
LL | x.field.map(|value| { { diverge(value + captured); } });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { diverge(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:69:5
--> $DIR/option_map_unit_fn_fixable.rs:73:5
|
LL | x.field.map(|value| { let y = plus_one(value + captured); });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { let y = plus_one(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:71:5
--> $DIR/option_map_unit_fn_fixable.rs:75:5
|
LL | x.field.map(|value| { plus_one(value + captured); });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { plus_one(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:73:5
--> $DIR/option_map_unit_fn_fixable.rs:77:5
|
LL | x.field.map(|value| { { plus_one(value + captured); } });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(value) = x.field { plus_one(value + captured); }`

error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:76:5
--> $DIR/option_map_unit_fn_fixable.rs:80:5
|
LL | x.field.map(|ref value| { do_nothing(value + captured) });}
LL | x.field.map(|ref value| { do_nothing(value + captured) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(ref value) = x.field { do_nothing(value + captured) }`

error: aborting due to 17 previous errors
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type
--> $DIR/option_map_unit_fn_fixable.rs:82:5
|
LL | option().map(do_nothing);}
| ^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try this: `if let Some(a) = option() { do_nothing(a) }`

error: aborting due to 18 previous errors

0 comments on commit 64b7624

Please sign in to comment.