|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{Block, Closure, ClosureKind, Expr, ExprKind, TyKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_session::declare_lint_pass; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// |
| 12 | + /// Detects a closure which takes one argument and returns unit. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// |
| 16 | + /// This is a less clear way to write `drop`. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// fn drop_ok<T, E>(result: Result<T, E>) -> Result<(), E> { |
| 21 | + /// result.map_err(|_| ()) |
| 22 | + /// } |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```no_run |
| 26 | + /// fn drop_ok<T, E>(result: Result<T, E>) -> Result<(), E> { |
| 27 | + /// result.map_err(drop) |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + #[clippy::version = "1.83.0"] |
| 31 | + pub TOILET_CLOSURES, |
| 32 | + style, |
| 33 | + "checks for 'toilet closure' usage" |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint_pass!(ToiletClosures => [TOILET_CLOSURES]); |
| 37 | + |
| 38 | +fn is_empty_expr(expr: &Expr<'_>) -> bool { |
| 39 | + matches!( |
| 40 | + expr.kind, |
| 41 | + ExprKind::Tup([]) |
| 42 | + | ExprKind::Block( |
| 43 | + Block { |
| 44 | + stmts: [], |
| 45 | + expr: None, |
| 46 | + .. |
| 47 | + }, |
| 48 | + None, |
| 49 | + ) |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +impl<'tcx> LateLintPass<'tcx> for ToiletClosures { |
| 54 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 55 | + let ExprKind::Closure(Closure { |
| 56 | + fn_decl, |
| 57 | + body, |
| 58 | + kind: ClosureKind::Closure, |
| 59 | + .. |
| 60 | + }) = expr.kind |
| 61 | + else { |
| 62 | + return; |
| 63 | + }; |
| 64 | + |
| 65 | + // Check that the closure takes one argument |
| 66 | + if let [arg_ty] = fn_decl.inputs |
| 67 | + // and returns `()` or `{}` |
| 68 | + && is_empty_expr(cx.tcx.hir().body(*body).value) |
| 69 | + // and the closure is not higher ranked, which `drop` cannot replace |
| 70 | + && let ty::Closure(_, generic_args) = cx.typeck_results().expr_ty(expr).kind() |
| 71 | + && generic_args.as_closure().sig().bound_vars().is_empty() |
| 72 | + { |
| 73 | + let mut applicability = Applicability::MachineApplicable; |
| 74 | + span_lint_and_sugg( |
| 75 | + cx, |
| 76 | + TOILET_CLOSURES, |
| 77 | + expr.span, |
| 78 | + "defined a 'toilet closure'", |
| 79 | + "replace with", |
| 80 | + if matches!(arg_ty.kind, TyKind::Infer) { |
| 81 | + String::from("drop") |
| 82 | + } else { |
| 83 | + let arg_ty_snippet = snippet_with_applicability(cx, arg_ty.span, "...", &mut applicability); |
| 84 | + format!("drop::<{arg_ty_snippet}>",) |
| 85 | + }, |
| 86 | + applicability, |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments