Skip to content

Commit 26b9911

Browse files
committed
Refactor out enum and address nits
1 parent 4f4e20c commit 26b9911

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

clippy_lints/src/map_unit_fn.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use utils::paths;
88
#[derive(Clone)]
99
pub struct Pass;
1010

11-
/// **What it does:** Checks for usage of `Option.map(f)` where f is a function
11+
/// **What it does:** Checks for usage of `option.map(f)` where f is a function
1212
/// or closure that returns the unit type.
1313
///
1414
/// **Why is this bad?** Readability, this can be written more clearly with
15-
/// an if statement
15+
/// an if let statement
1616
///
1717
/// **Known problems:** None.
1818
///
@@ -38,14 +38,14 @@ pub struct Pass;
3838
declare_clippy_lint! {
3939
pub OPTION_MAP_UNIT_FN,
4040
complexity,
41-
"using `Option.map(f)`, where f is a function or closure that returns ()"
41+
"using `option.map(f)`, where f is a function or closure that returns ()"
4242
}
4343

44-
/// **What it does:** Checks for usage of `Result.map(f)` where f is a function
44+
/// **What it does:** Checks for usage of `result.map(f)` where f is a function
4545
/// or closure that returns the unit type.
4646
///
4747
/// **Why is this bad?** Readability, this can be written more clearly with
48-
/// an if statement
48+
/// an if let statement
4949
///
5050
/// **Known problems:** None.
5151
///
@@ -71,7 +71,7 @@ declare_clippy_lint! {
7171
declare_clippy_lint! {
7272
pub RESULT_MAP_UNIT_FN,
7373
complexity,
74-
"using `Result.map(f)`, where f is a function or closure that returns ()"
74+
"using `result.map(f)`, where f is a function or closure that returns ()"
7575
}
7676

7777

@@ -215,33 +215,21 @@ fn lint_map_unit_fn(cx: &LateContext, stmt: &hir::Stmt, expr: &hir::Expr, map_ar
215215
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
216216
let msg = suggestion_msg("closure", map_type);
217217

218-
enum Suggestion {
219-
Full(String),
220-
Approx(String)
221-
}
222-
223-
let suggestion = if let Some(expr_span) = reduce_unit_expression(cx, closure_expr) {
224-
Suggestion::Full(
225-
format!("if let {0}({1}) = {2} {{ {3} }}",
226-
variant,
227-
snippet(cx, binding.pat.span, "_"),
228-
snippet(cx, var_arg.span, "_"),
229-
snippet(cx, expr_span, "_"))
230-
)
231-
} else {
232-
Suggestion::Approx(
233-
format!("if let {0}({1}) = {2} {{ ... }}",
234-
variant,
235-
snippet(cx, binding.pat.span, "_"),
236-
snippet(cx, var_arg.span, "_"))
237-
)
238-
};
239-
240218
span_lint_and_then(cx, lint, expr.span, &msg, |db| {
241-
match suggestion {
242-
Suggestion::Full(sugg) => db.span_suggestion(stmt.span, "try this", sugg),
243-
Suggestion::Approx(sugg) => db.span_approximate_suggestion(stmt.span, "try this", sugg),
244-
};
219+
if let Some(reduced_expr_span) = reduce_unit_expression(cx, closure_expr) {
220+
let suggestion = format!("if let {0}({1}) = {2} {{ {3} }}",
221+
variant,
222+
snippet(cx, binding.pat.span, "_"),
223+
snippet(cx, var_arg.span, "_"),
224+
snippet(cx, reduced_expr_span, "_"));
225+
db.span_suggestion(stmt.span, "try this", suggestion);
226+
} else {
227+
let suggestion = format!("if let {0}({1}) = {2} {{ ... }}",
228+
variant,
229+
snippet(cx, binding.pat.span, "_"),
230+
snippet(cx, var_arg.span, "_"));
231+
db.span_approximate_suggestion(stmt.span, "try this", suggestion);
232+
}
245233
});
246234
}
247235
}

0 commit comments

Comments
 (0)