Skip to content

Commit 21ddc50

Browse files
committed
chore(unnecessary_map_on_constructor): clean-up
- reduce indentation - print constructor/method name in backticks
1 parent dddaf07 commit 21ddc50

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

clippy_lints/src/unnecessary_map_on_constructor.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ declare_lint_pass!(UnnecessaryMapOnConstructor => [UNNECESSARY_MAP_ON_CONSTRUCTO
3535

3636
impl<'tcx> LateLintPass<'tcx> for UnnecessaryMapOnConstructor {
3737
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
38-
if expr.span.from_expansion() {
39-
return;
40-
}
41-
if let hir::ExprKind::MethodCall(path, recv, [map_arg], ..) = expr.kind
38+
if !expr.span.from_expansion()
39+
&& let hir::ExprKind::MethodCall(path, recv, [map_arg], ..) = expr.kind
40+
&& !map_arg.span.from_expansion()
41+
&& let hir::ExprKind::Path(fun) = map_arg.kind
4242
&& let Some(sym::Option | sym::Result) = cx.typeck_results().expr_ty(recv).opt_diag_name(cx)
4343
{
4444
let (constructor_path, constructor_item) = if let hir::ExprKind::Call(constructor, [arg, ..]) = recv.kind
4545
&& let hir::ExprKind::Path(constructor_path) = constructor.kind
46+
&& !constructor.span.from_expansion()
47+
&& !arg.span.from_expansion()
4648
{
47-
if constructor.span.from_expansion() || arg.span.from_expansion() {
48-
return;
49-
}
5049
(constructor_path, arg)
5150
} else {
5251
return;
@@ -67,29 +66,22 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryMapOnConstructor {
6766
_ => return,
6867
}
6968

70-
if let hir::ExprKind::Path(fun) = map_arg.kind {
71-
if map_arg.span.from_expansion() {
72-
return;
73-
}
74-
let mut applicability = Applicability::MachineApplicable;
75-
let fun_snippet = snippet_with_applicability(cx, fun.span(), "_", &mut applicability);
76-
let constructor_snippet =
77-
snippet_with_applicability(cx, constructor_path.span(), "_", &mut applicability);
78-
let constructor_arg_snippet =
79-
snippet_with_applicability(cx, constructor_item.span, "_", &mut applicability);
80-
span_lint_and_sugg(
81-
cx,
82-
UNNECESSARY_MAP_ON_CONSTRUCTOR,
83-
expr.span,
84-
format!(
85-
"unnecessary {} on constructor {constructor_snippet}(_)",
86-
path.ident.name
87-
),
88-
"try",
89-
format!("{constructor_snippet}({fun_snippet}({constructor_arg_snippet}))"),
90-
applicability,
91-
);
92-
}
69+
let mut app = Applicability::MachineApplicable;
70+
let fun_snippet = snippet_with_applicability(cx, fun.span(), "_", &mut app);
71+
let constructor_snippet = snippet_with_applicability(cx, constructor_path.span(), "_", &mut app);
72+
let constructor_arg_snippet = snippet_with_applicability(cx, constructor_item.span, "_", &mut app);
73+
span_lint_and_sugg(
74+
cx,
75+
UNNECESSARY_MAP_ON_CONSTRUCTOR,
76+
expr.span,
77+
format!(
78+
"unnecessary `{}` on constructor `{constructor_snippet}(_)`",
79+
path.ident.name
80+
),
81+
"try",
82+
format!("{constructor_snippet}({fun_snippet}({constructor_arg_snippet}))"),
83+
app,
84+
);
9385
}
9486
}
9587
}

tests/ui/unnecessary_map_on_constructor.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: unnecessary map on constructor Some(_)
1+
error: unnecessary `map` on constructor `Some(_)`
22
--> tests/ui/unnecessary_map_on_constructor.rs:32:13
33
|
44
LL | let a = Some(x).map(fun);
@@ -7,43 +7,43 @@ LL | let a = Some(x).map(fun);
77
= note: `-D clippy::unnecessary-map-on-constructor` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_map_on_constructor)]`
99

10-
error: unnecessary map on constructor Ok(_)
10+
error: unnecessary `map` on constructor `Ok(_)`
1111
--> tests/ui/unnecessary_map_on_constructor.rs:34:27
1212
|
1313
LL | let b: SimpleResult = Ok(x).map(fun);
1414
| ^^^^^^^^^^^^^^ help: try: `Ok(fun(x))`
1515

16-
error: unnecessary map_err on constructor Err(_)
16+
error: unnecessary `map_err` on constructor `Err(_)`
1717
--> tests/ui/unnecessary_map_on_constructor.rs:36:27
1818
|
1919
LL | let c: SimpleResult = Err(err).map_err(notfun);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Err(notfun(err))`
2121

22-
error: unnecessary map on constructor Option::Some(_)
22+
error: unnecessary `map` on constructor `Option::Some(_)`
2323
--> tests/ui/unnecessary_map_on_constructor.rs:39:13
2424
|
2525
LL | let a = Option::Some(x).map(fun);
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Option::Some(fun(x))`
2727

28-
error: unnecessary map on constructor SimpleResult::Ok(_)
28+
error: unnecessary `map` on constructor `SimpleResult::Ok(_)`
2929
--> tests/ui/unnecessary_map_on_constructor.rs:41:27
3030
|
3131
LL | let b: SimpleResult = SimpleResult::Ok(x).map(fun);
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `SimpleResult::Ok(fun(x))`
3333

34-
error: unnecessary map_err on constructor SimpleResult::Err(_)
34+
error: unnecessary `map_err` on constructor `SimpleResult::Err(_)`
3535
--> tests/ui/unnecessary_map_on_constructor.rs:43:27
3636
|
3737
LL | let c: SimpleResult = SimpleResult::Err(err).map_err(notfun);
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `SimpleResult::Err(notfun(err))`
3939

40-
error: unnecessary map on constructor Ok(_)
40+
error: unnecessary `map` on constructor `Ok(_)`
4141
--> tests/ui/unnecessary_map_on_constructor.rs:45:52
4242
|
4343
LL | let b: std::result::Result<i32, SimpleError> = Ok(x).map(fun);
4444
| ^^^^^^^^^^^^^^ help: try: `Ok(fun(x))`
4545

46-
error: unnecessary map_err on constructor Err(_)
46+
error: unnecessary `map_err` on constructor `Err(_)`
4747
--> tests/ui/unnecessary_map_on_constructor.rs:47:52
4848
|
4949
LL | let c: std::result::Result<i32, SimpleError> = Err(err).map_err(notfun);

0 commit comments

Comments
 (0)