|
| 1 | +use crate::context::LintContext; |
| 2 | +use crate::rustc_middle::ty::TypeFoldable; |
| 3 | +use crate::LateContext; |
| 4 | +use crate::LateLintPass; |
| 5 | +use rustc_hir::def::DefKind; |
| 6 | +use rustc_hir::{Expr, ExprKind}; |
| 7 | +use rustc_middle::ty; |
| 8 | +use rustc_span::symbol::sym; |
| 9 | + |
| 10 | +declare_lint! { |
| 11 | + /// The `noop_method_call` lint detects specific calls to noop methods |
| 12 | + /// such as a calling `<&T as Clone>::clone` where `T: !Clone`. |
| 13 | + /// |
| 14 | + /// ### Example |
| 15 | + /// |
| 16 | + /// ```rust |
| 17 | + /// # #![allow(unused)] |
| 18 | + /// struct Foo; |
| 19 | + /// let foo = &Foo; |
| 20 | + /// let clone: &Foo = foo.clone(); |
| 21 | + /// ``` |
| 22 | + /// |
| 23 | + /// {{produces}} |
| 24 | + /// |
| 25 | + /// ### Explanation |
| 26 | + /// |
| 27 | + /// Some method calls are noops meaning that they do nothing. Usually such methods |
| 28 | + /// are the result of blanket implementations that happen to create some method invocations |
| 29 | + /// that end up not doing anything. For instance, `Clone` is implemented on all `&T`, but |
| 30 | + /// calling `clone` on a `&T` where `T` does not implement clone, actually doesn't do anything |
| 31 | + /// as references are copy. This lint detects these calls and warns the user about them. |
| 32 | + pub NOOP_METHOD_CALL, |
| 33 | + Warn, |
| 34 | + "detects the use of well-known noop methods" |
| 35 | +} |
| 36 | + |
| 37 | +declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL]); |
| 38 | + |
| 39 | +impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { |
| 40 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 41 | + // We only care about method calls |
| 42 | + if let ExprKind::MethodCall(..) = expr.kind { |
| 43 | + // Get the `DefId` only when dealing with an `AssocFn` |
| 44 | + if let Some((DefKind::AssocFn, did)) = |
| 45 | + cx.typeck_results().type_dependent_def(expr.hir_id) |
| 46 | + { |
| 47 | + // Check that we're dealing with a trait method |
| 48 | + if let Some(trait_id) = cx.tcx.trait_of_item(did) { |
| 49 | + let substs = cx.typeck_results().node_substs(expr.hir_id); |
| 50 | + // We can't resolve on types that recursively require monomorphization, |
| 51 | + // so check that we don't need to perfom substitution |
| 52 | + if !substs.needs_subst() { |
| 53 | + let param_env = cx.tcx.param_env(trait_id); |
| 54 | + // Resolve the trait method instance |
| 55 | + if let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, param_env, did, substs) { |
| 56 | + // Check that it implements the noop diagnostic |
| 57 | + if cx.tcx.is_diagnostic_item(sym::ref_clone_method, i.def_id()) { |
| 58 | + let span = expr.span; |
| 59 | + |
| 60 | + cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| { |
| 61 | + let message = "Call to noop method"; |
| 62 | + lint.build(&message).emit() |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments