|
| 1 | +use crate::utils::{get_trait_def_id, paths, span_lint}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc::ty::{GenericPredicates, Predicate, ProjectionPredicate, TraitPredicate}; |
| 4 | +use rustc_hir::{Expr, ExprKind, StmtKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// **What it does:** Checks for functions that expect closures of type |
| 10 | + /// Fn(...) -> Ord where the implemented closure has a semi-colon |
| 11 | + /// at the end of the last statement. |
| 12 | + /// |
| 13 | + /// **Why is this bad?** Likely the semi-colon is unintentional which |
| 14 | + /// returns () instead of the result of last statement. Since () implements Ord |
| 15 | + /// it doesn't cause a compilation error |
| 16 | + /// |
| 17 | + /// **Known problems:** If returning unit is intentional, then there is no |
| 18 | + /// way of specifying this without triggering needless_return lint |
| 19 | + /// |
| 20 | + /// **Example:** |
| 21 | + /// |
| 22 | + /// ```rust |
| 23 | + /// let mut twins = vec!((1,1), (2,2)); |
| 24 | + /// twins.sort_by_key(|x| { x.1; }); |
| 25 | + /// ``` |
| 26 | + pub UNINTENTIONAL_UNIT_RETURN, |
| 27 | + nursery, |
| 28 | + "fn arguments of type Fn(...) -> Once having last statements with a semi-colon, suggesting to remove the semi-colon if it is unintentional." |
| 29 | +} |
| 30 | + |
| 31 | +declare_lint_pass!(UnintentionalUnitReturn => [UNINTENTIONAL_UNIT_RETURN]); |
| 32 | + |
| 33 | +fn unwrap_trait_pred<'tcx>(cx: &LateContext<'_, 'tcx>, pred: &Predicate<'tcx>) -> Option<TraitPredicate<'tcx>> { |
| 34 | + if let Predicate::Trait(poly_trait_pred, _) = pred { |
| 35 | + let trait_pred = cx.tcx.erase_late_bound_regions(&poly_trait_pred); |
| 36 | + Some(trait_pred) |
| 37 | + } else { |
| 38 | + None |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +fn get_predicates_for_trait_path<'tcx>( |
| 43 | + cx: &LateContext<'_, 'tcx>, |
| 44 | + generics: GenericPredicates<'tcx>, |
| 45 | + trait_path: &[&str], |
| 46 | +) -> Vec<&'tcx Predicate<'tcx>> { |
| 47 | + let mut preds = Vec::new(); |
| 48 | + generics.predicates.iter().for_each(|(pred, _)| { |
| 49 | + if let Some(trait_pred) = unwrap_trait_pred(cx, pred) { |
| 50 | + if let Some(trait_def_id) = get_trait_def_id(cx, trait_path) { |
| 51 | + if trait_def_id == trait_pred.trait_ref.def_id { |
| 52 | + preds.push(pred); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + preds |
| 58 | +} |
| 59 | + |
| 60 | +fn get_projection_pred<'tcx>( |
| 61 | + cx: &LateContext<'_, 'tcx>, |
| 62 | + generics: GenericPredicates<'tcx>, |
| 63 | + pred: TraitPredicate<'tcx>, |
| 64 | +) -> Option<ProjectionPredicate<'tcx>> { |
| 65 | + generics.predicates.iter().find_map(|(proj_pred, _)| { |
| 66 | + if let Predicate::Projection(proj_pred) = proj_pred { |
| 67 | + let projection_pred = cx.tcx.erase_late_bound_regions(proj_pred); |
| 68 | + if projection_pred.projection_ty.substs == pred.trait_ref.substs { |
| 69 | + return Some(projection_pred); |
| 70 | + } |
| 71 | + } |
| 72 | + None |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +fn get_args_to_check<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &'tcx Expr<'tcx>) -> Vec<usize> { |
| 77 | + let mut args_to_check = Vec::new(); |
| 78 | + if let Some(def_id) = cx.tables.type_dependent_def_id(expr.hir_id) { |
| 79 | + let fn_sig = cx.tcx.fn_sig(def_id); |
| 80 | + let generics = cx.tcx.predicates_of(def_id); |
| 81 | + let fn_mut_preds = get_predicates_for_trait_path(cx, generics, &paths::FN_MUT); |
| 82 | + let ord_preds = get_predicates_for_trait_path(cx, generics, &paths::ORD); |
| 83 | + // Trying to call erase_late_bound_regions on fn_sig.inputs() gives the following error |
| 84 | + // The trait `rustc::ty::TypeFoldable<'_>` is not implemented for `&[&rustc::ty::TyS<'_>]` |
| 85 | + let inputs_output = cx.tcx.erase_late_bound_regions(&fn_sig.inputs_and_output()); |
| 86 | + inputs_output.iter().enumerate().for_each(|(i, inp)| { |
| 87 | + // Ignore output param |
| 88 | + if i == inputs_output.len() - 1 { |
| 89 | + return; |
| 90 | + } |
| 91 | + fn_mut_preds.iter().for_each(|pred| { |
| 92 | + let trait_pred = unwrap_trait_pred(cx, pred).unwrap(); |
| 93 | + if trait_pred.self_ty() == *inp { |
| 94 | + if let Some(projection_pred) = get_projection_pred(cx, generics, trait_pred) { |
| 95 | + let ret_is_ord = ord_preds |
| 96 | + .iter() |
| 97 | + .any(|ord_pred| unwrap_trait_pred(cx, ord_pred).unwrap().self_ty() == projection_pred.ty); |
| 98 | + if ret_is_ord { |
| 99 | + args_to_check.push(i); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | + } |
| 106 | + args_to_check |
| 107 | +} |
| 108 | + |
| 109 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnintentionalUnitReturn { |
| 110 | + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) { |
| 111 | + let arg_indices = get_args_to_check(cx, expr); |
| 112 | + if_chain! { |
| 113 | + if let ExprKind::MethodCall(_, _, ref args) = expr.kind; |
| 114 | + then { |
| 115 | + for i in arg_indices { |
| 116 | + if_chain! { |
| 117 | + if i < args.len(); |
| 118 | + if let ExprKind::Closure(_, _fn_decl, body_id, _span, _) = args[i].kind; |
| 119 | + let body = cx.tcx.hir().body(body_id); |
| 120 | + if let ExprKind::Block(block, _) = body.value.kind; |
| 121 | + if let Some(stmt) = block.stmts.last(); |
| 122 | + if let StmtKind::Semi(_) = stmt.kind; |
| 123 | + then { |
| 124 | + //TODO : Maybe only filter the closures where the last statement return type also is an unit |
| 125 | + span_lint(cx, |
| 126 | + UNINTENTIONAL_UNIT_RETURN, |
| 127 | + stmt.span, |
| 128 | + "Semi-colon on the last line of this closure returns \ |
| 129 | + the unit type which also implements Ord."); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments