|
| 1 | +use rustc_hir::{Body, Item, ItemKind, QPath, TyKind}; |
| 2 | +use rustc_span::{sym, MacroKind}; |
| 3 | + |
| 4 | +use crate::{lints::NonLocalDefinitionsDiag, LateContext, LateLintPass, LintContext}; |
| 5 | + |
| 6 | +declare_lint! { |
| 7 | + /// The `non_local_definitions` lint checks for `impl` blocks and `#[macro_export]` |
| 8 | + /// macro inside bodies (functions, enum discriminant, ...). |
| 9 | + /// |
| 10 | + /// ### Example |
| 11 | + /// |
| 12 | + /// ```rust |
| 13 | + /// trait MyTrait {} |
| 14 | + /// struct MyStruct; |
| 15 | + /// |
| 16 | + /// fn foo() { |
| 17 | + /// impl MyTrait for MyStruct {} |
| 18 | + /// } |
| 19 | + /// ``` |
| 20 | + /// |
| 21 | + /// {{produces}} |
| 22 | + /// |
| 23 | + /// ### Explanation |
| 24 | + /// |
| 25 | + /// Creating non local definitions go against expectation and can create decrepencies |
| 26 | + /// in tooling. In should be avoided. |
| 27 | + pub NON_LOCAL_DEFINITIONS, |
| 28 | + Warn, |
| 29 | + "checks for non local definitions" |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Default)] |
| 33 | +pub struct NonLocalDefinitions { |
| 34 | + is_in_body: u32, |
| 35 | +} |
| 36 | + |
| 37 | +impl_lint_pass!(NonLocalDefinitions => [NON_LOCAL_DEFINITIONS]); |
| 38 | + |
| 39 | +impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { |
| 40 | + fn check_body(&mut self, _cx: &LateContext<'tcx>, _body: &'tcx Body<'tcx>) { |
| 41 | + self.is_in_body += 1; |
| 42 | + } |
| 43 | + |
| 44 | + fn check_body_post(&mut self, _cx: &LateContext<'tcx>, _body: &'tcx Body<'tcx>) { |
| 45 | + self.is_in_body -= 1; |
| 46 | + } |
| 47 | + |
| 48 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 49 | + if self.is_in_body > 0 { |
| 50 | + match item.kind { |
| 51 | + ItemKind::Impl(impl_) => 'block: { |
| 52 | + let TyKind::Path(QPath::Resolved(_, self_ty_path)) = impl_.self_ty.kind else { |
| 53 | + break 'block; |
| 54 | + }; |
| 55 | + let Some(self_ty_did) = self_ty_path.res.opt_def_id() else { |
| 56 | + break 'block; |
| 57 | + }; |
| 58 | + let parent_self_ty = cx.tcx.parent(self_ty_did); |
| 59 | + |
| 60 | + let parent_impl = cx.tcx.parent(item.owner_id.def_id.into()); |
| 61 | + if parent_impl != parent_self_ty |
| 62 | + && impl_ |
| 63 | + .of_trait |
| 64 | + .map(|trait_| trait_.trait_def_id()) |
| 65 | + .flatten() |
| 66 | + .map(|did| cx.tcx.parent(did) != parent_impl) |
| 67 | + .unwrap_or(true) |
| 68 | + { |
| 69 | + cx.emit_span_lint( |
| 70 | + NON_LOCAL_DEFINITIONS, |
| 71 | + item.span, |
| 72 | + NonLocalDefinitionsDiag { depth: self.is_in_body }, |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + ItemKind::Macro(_macro, MacroKind::Bang) => { |
| 77 | + if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) { |
| 78 | + cx.emit_span_lint( |
| 79 | + NON_LOCAL_DEFINITIONS, |
| 80 | + item.span, |
| 81 | + NonLocalDefinitionsDiag { depth: self.is_in_body }, |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + _ => {} |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments