|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::sym; |
| 3 | +use rustc_hir::Expr; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_middle::ty::{self, ExistentialPredicate, Ty}; |
| 6 | +use rustc_session::declare_lint_pass; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// |
| 11 | + /// Detects cases where a `&dyn Any` is constructed directly referencing a `Box<dyn Any>` or |
| 12 | + /// other value that dereferences to `dyn Any`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// |
| 16 | + /// The intention is usually to borrow the `dyn Any` stored inside the value, rather than the |
| 17 | + /// value itself. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```no_run |
| 21 | + /// let x: Box<dyn Any> = Box::new(()); |
| 22 | + /// let _: &dyn Any = &x; |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```no_run |
| 26 | + /// let x: Box<dyn Any> = Box::new(()); |
| 27 | + /// let _: &dyn Any = &**x; |
| 28 | + /// ``` |
| 29 | + #[clippy::version = "1.88.0"] |
| 30 | + pub COERCE_ANY_REF_TO_ANY, |
| 31 | + nursery, |
| 32 | + "coercing reference to values containing `dyn Any` to `&dyn Any` is usually not intended" |
| 33 | +} |
| 34 | +declare_lint_pass!(CoerceAnyRefToAny => [COERCE_ANY_REF_TO_ANY]); |
| 35 | + |
| 36 | +impl<'tcx> LateLintPass<'tcx> for CoerceAnyRefToAny { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { |
| 38 | + // If this expression has an effective type of `&dyn Any` ... |
| 39 | + { |
| 40 | + let coerced_ty = cx.typeck_results().expr_ty_adjusted(e); |
| 41 | + |
| 42 | + let ty::Ref(_, coerced_ref_ty, _) = *coerced_ty.kind() else { |
| 43 | + return; |
| 44 | + }; |
| 45 | + if !is_dyn_any(cx, coerced_ref_ty) { |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + let expr_ty = cx.typeck_results().expr_ty(e); |
| 51 | + let ty::Ref(_, expr_ref_ty, _) = *expr_ty.kind() else { |
| 52 | + return; |
| 53 | + }; |
| 54 | + // ... but only due to coercion ... |
| 55 | + if is_dyn_any(cx, expr_ref_ty) { |
| 56 | + return; |
| 57 | + } |
| 58 | + // ... and it also *derefs* to `dyn Any` ... |
| 59 | + let Some(target) = clippy_utils::ty::deref_chain(cx, expr_ty).skip(1).last() else { |
| 60 | + return; |
| 61 | + }; |
| 62 | + if !is_dyn_any(cx, target) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // ... that's probably not intended. |
| 67 | + span_lint( |
| 68 | + cx, |
| 69 | + COERCE_ANY_REF_TO_ANY, |
| 70 | + e.span, |
| 71 | + format!( |
| 72 | + "coercing `{}` to `&dyn Any` rather than dereferencing the dyn Any inside it", |
| 73 | + expr_ty |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +fn is_dyn_any(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { |
| 80 | + let ty::Dynamic(traits, ..) = ty.kind() else { |
| 81 | + return false; |
| 82 | + }; |
| 83 | + if traits.iter().all(|binder| { |
| 84 | + let Some(predicate) = binder.no_bound_vars() else { |
| 85 | + return false; |
| 86 | + }; |
| 87 | + let ExistentialPredicate::Trait(t) = predicate else { |
| 88 | + return false; |
| 89 | + }; |
| 90 | + !cx.tcx.is_diagnostic_item(sym::Any, t.def_id) |
| 91 | + }) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + true |
| 95 | +} |
0 commit comments