|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use rustc_hir::def_id::LocalDefId; |
| 3 | +use rustc_hir::intravisit::FnKind; |
| 4 | +use rustc_hir::{Body, FnDecl, FnRetTy, GenericArgs, GenericBound, ItemKind, TraitBoundModifier, TyKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty::ClauseKind; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_span::Span; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Looks for bounds in `impl Trait` in return position that are implied by other bounds. |
| 13 | + /// This is usually the case when a supertrait is explicitly specified, when it is already implied |
| 14 | + /// by a subtrait (e.g. `DerefMut: Deref`, so specifying `Deref` is unnecessary when `DerefMut` is specified). |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// Unnecessary complexity. |
| 18 | + /// |
| 19 | + /// ### Known problems |
| 20 | + /// This lint currently does not work with generic traits (i.e. will not lint even if redundant). |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust |
| 24 | + /// fn f() -> impl Deref<Target = i32> + DerefMut<Target = i32> { |
| 25 | + /// // ^^^^^^^^^^^^^^^^^^^ unnecessary bound, already implied by the `DerefMut` trait bound |
| 26 | + /// Box::new(123) |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```rust |
| 31 | + /// fn f() -> impl DerefMut<Target = i32> { |
| 32 | + /// Box::new(123) |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + #[clippy::version = "1.73.0"] |
| 36 | + pub IMPLIED_BOUNDS_IN_IMPL, |
| 37 | + complexity, |
| 38 | + "specifying bounds that are implied by other bounds in `impl Trait` type" |
| 39 | +} |
| 40 | +declare_lint_pass!(ImpliedBoundsInImpl => [IMPLIED_BOUNDS_IN_IMPL]); |
| 41 | + |
| 42 | +impl LateLintPass<'_> for ImpliedBoundsInImpl { |
| 43 | + fn check_fn( |
| 44 | + &mut self, |
| 45 | + cx: &LateContext<'_>, |
| 46 | + _: FnKind<'_>, |
| 47 | + decl: &FnDecl<'_>, |
| 48 | + _: &Body<'_>, |
| 49 | + _: Span, |
| 50 | + _: LocalDefId, |
| 51 | + ) { |
| 52 | + if let FnRetTy::Return(ty) = decl.output { |
| 53 | + if let TyKind::OpaqueDef(item_id, ..) = ty.kind |
| 54 | + && let item = cx.tcx.hir().item(item_id) |
| 55 | + && let ItemKind::OpaqueTy(opaque_ty) = item.kind |
| 56 | + { |
| 57 | + // Get all `DefId`s of (implied) trait predicates in all the bounds. |
| 58 | + // For `impl Deref + DerefMut` this will contain [`Deref`]. |
| 59 | + // The implied `Deref` comes from `DerefMut` because `trait DerefMut: Deref {}`. |
| 60 | + |
| 61 | + // N.B. Generic args on trait bounds are currently ignored and (G)ATs are fine to disregard, |
| 62 | + // because they must be the same for all of its supertraits. Example: |
| 63 | + // `impl Deref<Target = i32> + DerefMut<Target = u32>` is not allowed. |
| 64 | + // `DerefMut::Target` needs to match `Deref::Target` |
| 65 | + let implied_bounds = opaque_ty.bounds.iter().flat_map(|bound| { |
| 66 | + if let GenericBound::Trait(poly_trait, TraitBoundModifier::None) = bound |
| 67 | + && let [.., path] = poly_trait.trait_ref.path.segments |
| 68 | + && poly_trait.bound_generic_params.is_empty() |
| 69 | + && path.args.map_or(true, GenericArgs::is_empty) |
| 70 | + && let Some(trait_def_id) = path.res.opt_def_id() |
| 71 | + { |
| 72 | + cx.tcx.implied_predicates_of(trait_def_id).predicates |
| 73 | + } else { |
| 74 | + &[] |
| 75 | + } |
| 76 | + }).collect::<Vec<_>>(); |
| 77 | + |
| 78 | + // Lint all bounds in the `impl Trait` type that are also in the `implied_bounds` vec. |
| 79 | + for bound in opaque_ty.bounds { |
| 80 | + if let GenericBound::Trait(poly_trait, TraitBoundModifier::None) = bound |
| 81 | + && let Some(def_id) = poly_trait.trait_ref.path.res.opt_def_id() |
| 82 | + && implied_bounds.iter().any(|(clause, _)| { |
| 83 | + if let ClauseKind::Trait(tr) = clause.kind().skip_binder() { |
| 84 | + tr.def_id() == def_id |
| 85 | + } else { |
| 86 | + false |
| 87 | + } |
| 88 | + }) |
| 89 | + { |
| 90 | + span_lint(cx, IMPLIED_BOUNDS_IN_IMPL, poly_trait.span, "this bound is implied by another bound and can be removed"); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments