1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 use crate::rustc::hir;
12 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use if_chain::if_chain;
16 use crate::rustc_errors::Applicability;
17 use crate::syntax::source_map::Span;
18 use crate::utils::{in_macro, iter_input_pats, match_type, method_chain_args, snippet, span_lint_and_then};
19 use crate::utils::paths;
24 /// **What it does:** Checks for usage of `option.map(f)` where f is a function
25 /// or closure that returns the unit type.
27 /// **Why is this bad?** Readability, this can be written more clearly with
28 /// an if let statement
30 /// **Known problems:** None.
35 /// let x: Option<&str> = do_stuff();
36 /// x.map(log_err_msg);
37 /// x.map(|msg| log_err_msg(format_msg(msg)))
40 /// The correct use would be:
43 /// let x: Option<&str> = do_stuff();
44 /// if let Some(msg) = x {
47 /// if let Some(msg) = x {
48 /// log_err_msg(format_msg(msg))
51 declare_clippy_lint! {
52 pub OPTION_MAP_UNIT_FN,
54 "using `option.map(f)`, where f is a function or closure that returns ()"
57 /// **What it does:** Checks for usage of `result.map(f)` where f is a function
58 /// or closure that returns the unit type.
60 /// **Why is this bad?** Readability, this can be written more clearly with
61 /// an if let statement
63 /// **Known problems:** None.
68 /// let x: Result<&str, &str> = do_stuff();
69 /// x.map(log_err_msg);
70 /// x.map(|msg| log_err_msg(format_msg(msg)))
73 /// The correct use would be:
76 /// let x: Result<&str, &str> = do_stuff();
77 /// if let Ok(msg) = x {
80 /// if let Ok(msg) = x {
81 /// log_err_msg(format_msg(msg))
84 declare_clippy_lint! {
85 pub RESULT_MAP_UNIT_FN,
87 "using `result.map(f)`, where f is a function or closure that returns ()"
91 impl LintPass for Pass {
92 fn get_lints(&self) -> LintArray {
93 lint_array!(OPTION_MAP_UNIT_FN, RESULT_MAP_UNIT_FN)
97 fn is_unit_type(ty: ty::Ty<'_>) -> bool {
99 ty::Tuple(slice) => slice.is_empty(),
105 fn is_unit_function(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> bool {
106 let ty = cx.tables.expr_ty(expr);
108 if let ty::FnDef(id, _) = ty.sty {
109 if let Some(fn_type) = cx.tcx.fn_sig(id).no_late_bound_regions() {
110 return is_unit_type(fn_type.output());
116 fn is_unit_expression(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> bool {
117 is_unit_type(cx.tables.expr_ty(expr))
120 /// The expression inside a closure may or may not have surrounding braces and
121 /// semicolons, which causes problems when generating a suggestion. Given an
122 /// expression that evaluates to '()' or '!', recursively remove useless braces
123 /// and semi-colons until is suitable for including in the suggestion template
124 fn reduce_unit_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a hir::Expr) -> Option<Span> {
125 if !is_unit_expression(cx, expr) {
130 hir::ExprKind::Call(_, _) |
131 hir::ExprKind::MethodCall(_, _, _) => {
132 // Calls can't be reduced any more
135 hir::ExprKind::Block(ref block, _) => {
136 match (&block.stmts[..], block.expr.as_ref()) {
137 (&[], Some(inner_expr)) => {
138 // If block only contains an expression,
139 // reduce `{ X }` to `X`
140 reduce_unit_expression(cx, inner_expr)
142 (&[ref inner_stmt], None) => {
143 // If block only contains statements,
144 // reduce `{ X; }` to `X` or `X;`
145 match inner_stmt.node {
146 hir::StmtKind::Decl(ref d, _) => Some(d.span),
147 hir::StmtKind::Expr(ref e, _) => Some(e.span),
148 hir::StmtKind::Semi(_, _) => Some(inner_stmt.span),
152 // For closures that contain multiple statements
153 // it's difficult to get a correct suggestion span
154 // for all cases (multi-line closures specifically)
156 // We do not attempt to build a suggestion for those right now.
165 fn unit_closure<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'a hir::Expr) -> Option<(&'tcx hir::Arg, &'a hir::Expr)> {
166 if let hir::ExprKind::Closure(_, ref decl, inner_expr_id, _, _) = expr.node {
167 let body = cx.tcx.hir.body(inner_expr_id);
168 let body_expr = &body.value;
171 if decl.inputs.len() == 1;
172 if is_unit_expression(cx, body_expr);
173 if let Some(binding) = iter_input_pats(&decl, body).next();
175 return Some((binding, body_expr));
182 /// Builds a name for the let binding variable (var_arg)
184 /// `x.field` => `x_field`
187 /// Anything else will return `_`.
188 fn let_binding_name(cx: &LateContext<'_, '_>, var_arg: &hir::Expr) -> String {
189 match &var_arg.node {
190 hir::ExprKind::Field(_, _) => snippet(cx, var_arg.span, "_").replace(".", "_"),
191 hir::ExprKind::Path(_) => format!("_{}", snippet(cx, var_arg.span, "")),
196 fn suggestion_msg(function_type: &str, map_type: &str) -> String {
198 "called `map(f)` on an {0} value where `f` is a unit {1}",
204 fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt, expr: &hir::Expr, map_args: &[hir::Expr]) {
205 let var_arg = &map_args[0];
206 let fn_arg = &map_args[1];
208 let (map_type, variant, lint) =
209 if match_type(cx, cx.tables.expr_ty(var_arg), &paths::OPTION) {
210 ("Option", "Some", OPTION_MAP_UNIT_FN)
211 } else if match_type(cx, cx.tables.expr_ty(var_arg), &paths::RESULT) {
212 ("Result", "Ok", RESULT_MAP_UNIT_FN)
217 if is_unit_function(cx, fn_arg) {
218 let msg = suggestion_msg("function", map_type);
219 let suggestion = format!("if let {0}({1}) = {2} {{ {3}(...) }}",
221 let_binding_name(cx, var_arg),
222 snippet(cx, var_arg.span, "_"),
223 snippet(cx, fn_arg.span, "_"));
225 span_lint_and_then(cx, lint, expr.span, &msg, |db| {
226 db.span_suggestion_with_applicability(stmt.span,
229 Applicability::Unspecified);
231 } else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
232 let msg = suggestion_msg("closure", map_type);
234 span_lint_and_then(cx, lint, expr.span, &msg, |db| {
235 if let Some(reduced_expr_span) = reduce_unit_expression(cx, closure_expr) {
236 let suggestion = format!("if let {0}({1}) = {2} {{ {3} }}",
238 snippet(cx, binding.pat.span, "_"),
239 snippet(cx, var_arg.span, "_"),
240 snippet(cx, reduced_expr_span, "_"));
241 db.span_suggestion_with_applicability(
245 Applicability::MachineApplicable, // snippet
248 let suggestion = format!("if let {0}({1}) = {2} {{ ... }}",
250 snippet(cx, binding.pat.span, "_"),
251 snippet(cx, var_arg.span, "_"));
252 db.span_suggestion_with_applicability(
256 Applicability::Unspecified,
263 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
264 fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &hir::Stmt) {
265 if in_macro(stmt.span) {
269 if let hir::StmtKind::Semi(ref expr, _) = stmt.node {
270 if let Some(arglists) = method_chain_args(expr, &["map"]) {
271 lint_map_unit_fn(cx, stmt, expr, arglists[0]);