|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_then, get_parent_node, match_qpath, path_res, ty::implements_trait}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, Node, PatKind}; |
| 4 | +use rustc_hir_analysis::hir_ty_to_ty; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty::EarlyBinder; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_span::sym; |
| 9 | +use std::borrow::Cow; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for manual implementations of both `PartialOrd` and `Ord` when only `Ord` is |
| 14 | + /// necessary. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// If both `PartialOrd` and `Ord` are implemented, they must agree. This is commonly done by |
| 18 | + /// wrapping the result of `cmp` in `Some` for `partial_cmp`. Not doing this may silently |
| 19 | + /// introduce an error upon refactoring. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust,ignore |
| 23 | + /// #[derive(Eq, PartialEq)] |
| 24 | + /// struct A(u32); |
| 25 | + /// |
| 26 | + /// impl Ord for A { |
| 27 | + /// fn cmp(&self, other: &Self) -> Ordering { |
| 28 | + /// todo!(); |
| 29 | + /// } |
| 30 | + /// } |
| 31 | + /// |
| 32 | + /// impl PartialOrd for A { |
| 33 | + /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 34 | + /// todo!(); |
| 35 | + /// } |
| 36 | + /// } |
| 37 | + /// ``` |
| 38 | + /// Use instead: |
| 39 | + /// ```rust,ignore |
| 40 | + /// #[derive(Eq, PartialEq)] |
| 41 | + /// struct A(u32); |
| 42 | + /// |
| 43 | + /// impl Ord for A { |
| 44 | + /// fn cmp(&self, other: &Self) -> Ordering { |
| 45 | + /// todo!(); |
| 46 | + /// } |
| 47 | + /// } |
| 48 | + /// |
| 49 | + /// impl PartialOrd for A { |
| 50 | + /// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 51 | + /// Some(self.cmp(other)) |
| 52 | + /// } |
| 53 | + /// } |
| 54 | + /// ``` |
| 55 | + #[clippy::version = "1.72.0"] |
| 56 | + pub NEEDLESS_PARTIAL_ORD_IMPL, |
| 57 | + correctness, |
| 58 | + "manual implementation of `PartialOrd` when `Ord` is already implemented" |
| 59 | +} |
| 60 | +declare_lint_pass!(NeedlessImpls => [NEEDLESS_PARTIAL_ORD_IMPL]); |
| 61 | + |
| 62 | +impl LateLintPass<'_> for NeedlessImpls { |
| 63 | + fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { |
| 64 | + let node = get_parent_node(cx.tcx, impl_item.hir_id()); |
| 65 | + let Some(Node::Item(item)) = node else { |
| 66 | + return; |
| 67 | + }; |
| 68 | + let ItemKind::Impl(imp) = item.kind else { |
| 69 | + return; |
| 70 | + }; |
| 71 | + let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else { |
| 72 | + return; |
| 73 | + }; |
| 74 | + let trait_impl_def_id = trait_impl.def_id; |
| 75 | + if cx.tcx.is_automatically_derived(item.owner_id.to_def_id()) { |
| 76 | + return; |
| 77 | + } |
| 78 | + let ImplItemKind::Fn(_, impl_item_id) = cx.tcx.hir().impl_item(impl_item.impl_item_id()).kind else { |
| 79 | + return; |
| 80 | + }; |
| 81 | + let body = cx.tcx.hir().body(impl_item_id); |
| 82 | + let ExprKind::Block(block, ..) = body.value.kind else { |
| 83 | + return; |
| 84 | + }; |
| 85 | + |
| 86 | + if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl_def_id) |
| 87 | + && impl_item.ident.name == sym::partial_cmp |
| 88 | + && let Some(ord_def_id) = cx |
| 89 | + .tcx |
| 90 | + .diagnostic_items(trait_impl.def_id.krate) |
| 91 | + .name_to_id |
| 92 | + .get(&sym::Ord) |
| 93 | + && implements_trait( |
| 94 | + cx, |
| 95 | + hir_ty_to_ty(cx.tcx, imp.self_ty), |
| 96 | + *ord_def_id, |
| 97 | + trait_impl.substs, |
| 98 | + ) |
| 99 | + { |
| 100 | + if block.stmts.is_empty() |
| 101 | + && let Some(expr) = block.expr |
| 102 | + && let ExprKind::Call(Expr { kind: ExprKind::Path(some_path), .. }, [cmp_expr]) = expr.kind |
| 103 | + // TODO: We can likely use the `option_some_variant` lang item instead, but this may be tricky |
| 104 | + // due to the fact that `expr` is the constructor, not `option_some_variant` itself. |
| 105 | + && match_qpath(some_path, &["Some"]) |
| 106 | + && let ExprKind::MethodCall(cmp_path, _, [other_expr], ..) = cmp_expr.kind |
| 107 | + && cmp_path.ident.name == sym::cmp |
| 108 | + && let Res::Local(..) = path_res(cx, other_expr) |
| 109 | + {} else { |
| 110 | + span_lint_and_then( |
| 111 | + cx, |
| 112 | + NEEDLESS_PARTIAL_ORD_IMPL, |
| 113 | + item.span, |
| 114 | + "manual implementation of `PartialOrd` when `Ord` is already implemented", |
| 115 | + |diag| { |
| 116 | + let (help, app) = if let Some(other) = body.params.get(0) |
| 117 | + && let PatKind::Binding(_, _, other_ident, ..) = other.pat.kind |
| 118 | + { |
| 119 | + ( |
| 120 | + Cow::Owned(format!("{{ Some(self.cmp({})) }}", other_ident.name)), |
| 121 | + Applicability::Unspecified, |
| 122 | + ) |
| 123 | + } else { |
| 124 | + (Cow::Borrowed("{ Some(self.cmp(...)) }"), Applicability::HasPlaceholders) |
| 125 | + }; |
| 126 | + |
| 127 | + diag.span_suggestion( |
| 128 | + block.span, |
| 129 | + "change this to", |
| 130 | + help, |
| 131 | + app, |
| 132 | + ); |
| 133 | + } |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments