|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::{is_automatically_derived, match_def_path, paths}; |
| 3 | +use rustc_ast::LitKind; |
| 4 | +use rustc_hir::{Block, BlockCheckMode, BodyId, Expr, ExprField, ExprKind, FnDecl, HirId, Impl, ImplItemKind, Item, ItemKind, Node, QPath, TraitRef, TyKind, UnsafeSource, Unsafety}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | + |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks for double comparisons that could be simplified to a single expression. |
| 12 | + /// |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// Readability. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// # let x = 1; |
| 20 | + /// # let y = 2; |
| 21 | + /// if x == y || x < y {} |
| 22 | + /// ``` |
| 23 | + /// |
| 24 | + /// Could be written as: |
| 25 | + /// |
| 26 | + /// ```rust |
| 27 | + /// # let x = 1; |
| 28 | + /// # let y = 2; |
| 29 | + /// if x <= y {} |
| 30 | + /// ``` |
| 31 | + pub DERIVABLE_IMPLS, |
| 32 | + complexity, |
| 33 | + "manual implementation of a trait which is equal to a derive" |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]); |
| 37 | + |
| 38 | +fn struct_fields<'hir>(e: &'hir Expr<'hir>) -> Option<&'hir [ExprField]> { |
| 39 | + match e.kind { |
| 40 | + ExprKind::Struct(_, fields, _) => Some(fields), |
| 41 | + ExprKind::Block(Block { expr, .. }, _) => expr.and_then(struct_fields), |
| 42 | + _ => None, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn is_path_self(e: &Expr<'_>) -> bool { |
| 47 | + if let ExprKind::Path(QPath::Resolved(_, p)) = e.kind { |
| 48 | + if p.segments.len() != 1 { |
| 49 | + return false; |
| 50 | + } |
| 51 | + if p.segments[0].ident.name == rustc_span::symbol::kw::SelfUpper { |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + false |
| 56 | +} |
| 57 | + |
| 58 | +fn tuple_fields<'hir>(e: &'hir Expr<'hir>) -> Option<&'hir [Expr<'hir>]> { |
| 59 | + match e.kind { |
| 60 | + ExprKind::Tup(fields) => Some(fields), |
| 61 | + ExprKind::Call(callee, args) => if is_path_self(callee) { |
| 62 | + Some(args) |
| 63 | + } else { |
| 64 | + None |
| 65 | + }, |
| 66 | + ExprKind::Block(Block { expr, .. }, _) => expr.and_then(tuple_fields), |
| 67 | + _ => None, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn is_default(_cx: &LateContext<'_>, e: &Expr<'_>) -> bool { |
| 72 | + if let ExprKind::Lit(lit) = &e.kind { |
| 73 | + match lit.node { |
| 74 | + LitKind::Bool(x) => !x, |
| 75 | + LitKind::Int(x, _) => x == 0, |
| 76 | + _ => false, |
| 77 | + } |
| 78 | + } else { |
| 79 | + false |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl<'tcx> LateLintPass<'tcx> for DerivableImpls { |
| 84 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { |
| 85 | + if let ItemKind::Impl(Impl { |
| 86 | + of_trait: Some(ref trait_ref), |
| 87 | + self_ty, |
| 88 | + items, |
| 89 | + .. |
| 90 | + }) = item.kind |
| 91 | + { |
| 92 | + let ty = cx.tcx.type_of(item.def_id); |
| 93 | + let attrs = cx.tcx.hir().attrs(item.hir_id()); |
| 94 | + if is_automatically_derived(attrs) { |
| 95 | + return; |
| 96 | + } |
| 97 | + if let Some(def_id) = trait_ref.trait_def_id() { |
| 98 | + if !match_def_path(cx, def_id, &paths::DEFAULT_TRAIT) { |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + let impl_item_hir = items[0].id.hir_id(); |
| 103 | + let func_expr = match cx.tcx.hir().find(impl_item_hir) { |
| 104 | + Some(Node::ImplItem(x)) => match &x.kind { |
| 105 | + ImplItemKind::Fn(_, b) => match cx.tcx.hir().find(b.hir_id) { |
| 106 | + Some(Node::Expr(e)) => e, |
| 107 | + _ => return, |
| 108 | + }, |
| 109 | + _ => return, |
| 110 | + }, |
| 111 | + _ => return, |
| 112 | + }; |
| 113 | + let should_emit = if let Some(s) = struct_fields(func_expr) { |
| 114 | + s.iter().all(|ef| is_default(cx, ef.expr)) |
| 115 | + } else if let Some(s) = tuple_fields(func_expr) { |
| 116 | + s.iter().all(|e| is_default(cx, e)) |
| 117 | + } else { |
| 118 | + return; |
| 119 | + }; |
| 120 | + if should_emit { |
| 121 | + span_lint(cx, DERIVABLE_IMPLS, item.span, "derivable impl of trait Default:"); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments