|
| 1 | +use crate::lints::MappingToUnit; |
| 2 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 3 | + |
| 4 | +use rustc_hir::{Expr, ExprKind, HirId, Stmt, StmtKind}; |
| 5 | +use rustc_middle::{ |
| 6 | + query::Key, |
| 7 | + ty::{self, Ty}, |
| 8 | +}; |
| 9 | + |
| 10 | +declare_lint! { |
| 11 | + /// The `map_unit_fn` lint checks for `Iterator::map` receive |
| 12 | + /// a callable that returns `()`. |
| 13 | + /// |
| 14 | + /// ### Example |
| 15 | + /// |
| 16 | + /// ```rust |
| 17 | + /// fn foo(items: &mut Vec<u8>) { |
| 18 | + /// items.sort(); |
| 19 | + /// } |
| 20 | + /// |
| 21 | + /// fn main() { |
| 22 | + /// let mut x: Vec<Vec<u8>> = vec![ |
| 23 | + /// vec![0, 2, 1], |
| 24 | + /// vec![5, 4, 3], |
| 25 | + /// ]; |
| 26 | + /// x.iter_mut().map(foo); |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// {{produces}} |
| 31 | + /// |
| 32 | + /// ### Explanation |
| 33 | + /// |
| 34 | + /// Mapping to `()` is almost always a mistake. |
| 35 | + pub MAP_UNIT_FN, |
| 36 | + Warn, |
| 37 | + "`Iterator::map` call that discard the iterator's values" |
| 38 | +} |
| 39 | + |
| 40 | +declare_lint_pass!(MapUnitFn => [MAP_UNIT_FN]); |
| 41 | + |
| 42 | +impl<'tcx> LateLintPass<'tcx> for MapUnitFn { |
| 43 | + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &Stmt<'_>) { |
| 44 | + if stmt.span.from_expansion() { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if let StmtKind::Semi(expr) = stmt.kind { |
| 49 | + if let ExprKind::MethodCall(path, receiver, args, span) = expr.kind { |
| 50 | + if path.ident.name.as_str() == "map" { |
| 51 | + if receiver.span.from_expansion() |
| 52 | + || args.iter().any(|e| e.span.from_expansion()) |
| 53 | + || !is_impl_slice(cx, receiver) |
| 54 | + || !is_diagnostic_name(cx, expr.hir_id, "IteratorMap") |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + let arg_ty = cx.typeck_results().expr_ty(&args[0]); |
| 59 | + if let ty::FnDef(id, _) = arg_ty.kind() { |
| 60 | + let fn_ty = cx.tcx.fn_sig(id).skip_binder(); |
| 61 | + let ret_ty = fn_ty.output().skip_binder(); |
| 62 | + if is_unit_type(ret_ty) { |
| 63 | + cx.emit_spanned_lint( |
| 64 | + MAP_UNIT_FN, |
| 65 | + span, |
| 66 | + MappingToUnit { |
| 67 | + function_label: cx.tcx.span_of_impl(*id).unwrap(), |
| 68 | + argument_label: args[0].span, |
| 69 | + map_label: arg_ty.default_span(cx.tcx), |
| 70 | + suggestion: path.ident.span, |
| 71 | + replace: "for_each".to_string(), |
| 72 | + }, |
| 73 | + ) |
| 74 | + } |
| 75 | + } else if let ty::Closure(id, subs) = arg_ty.kind() { |
| 76 | + let cl_ty = subs.as_closure().sig(); |
| 77 | + let ret_ty = cl_ty.output().skip_binder(); |
| 78 | + if is_unit_type(ret_ty) { |
| 79 | + cx.emit_spanned_lint( |
| 80 | + MAP_UNIT_FN, |
| 81 | + span, |
| 82 | + MappingToUnit { |
| 83 | + function_label: cx.tcx.span_of_impl(*id).unwrap(), |
| 84 | + argument_label: args[0].span, |
| 85 | + map_label: arg_ty.default_span(cx.tcx), |
| 86 | + suggestion: path.ident.span, |
| 87 | + replace: "for_each".to_string(), |
| 88 | + }, |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn is_impl_slice(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 99 | + if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { |
| 100 | + if let Some(impl_id) = cx.tcx.impl_of_method(method_id) { |
| 101 | + return cx.tcx.type_of(impl_id).skip_binder().is_slice(); |
| 102 | + } |
| 103 | + } |
| 104 | + false |
| 105 | +} |
| 106 | + |
| 107 | +fn is_unit_type(ty: Ty<'_>) -> bool { |
| 108 | + ty.is_unit() || ty.is_never() |
| 109 | +} |
| 110 | + |
| 111 | +fn is_diagnostic_name(cx: &LateContext<'_>, id: HirId, name: &str) -> bool { |
| 112 | + if let Some(def_id) = cx.typeck_results().type_dependent_def_id(id) { |
| 113 | + if let Some(item) = cx.tcx.get_diagnostic_name(def_id) { |
| 114 | + if item.as_str() == name { |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + false |
| 120 | +} |
0 commit comments