|
| 1 | +use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then}; |
| 2 | +use clippy_utils::path_res; |
| 3 | +use clippy_utils::ty::implements_trait; |
| 4 | +use rustc_hir::def_id::{DefId, LocalDefId}; |
| 5 | +use rustc_hir::{Item, ItemKind}; |
| 6 | +use rustc_hir_analysis::hir_ty_to_ty; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_middle::ty::Visibility; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | +use rustc_span::sym; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for types named `Error` that implement `Error`. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// It can become confusing when a codebase has 20 types all named `Error`, requiring either |
| 18 | + /// aliasing them in the `use` statement or qualifying them like `my_module::Error`. This |
| 19 | + /// hinders comprehension, as it requires you to memorize every variation of importing `Error` |
| 20 | + /// used across a codebase. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust,ignore |
| 24 | + /// #[derive(Debug)] |
| 25 | + /// pub enum Error { ... } |
| 26 | + /// |
| 27 | + /// impl std::fmt::Display for Error { ... } |
| 28 | + /// |
| 29 | + /// impl std::error::Error for Error { ... } |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.72.0"] |
| 32 | + pub ERROR_IMPL_ERROR, |
| 33 | + restriction, |
| 34 | + "exported types named `Error` that implement `Error`" |
| 35 | +} |
| 36 | +declare_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]); |
| 37 | + |
| 38 | +impl<'tcx> LateLintPass<'tcx> for ErrorImplError { |
| 39 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 40 | + let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) else { |
| 41 | + return; |
| 42 | + }; |
| 43 | + |
| 44 | + match item.kind { |
| 45 | + ItemKind::TyAlias(ty, _) if implements_trait(cx, hir_ty_to_ty(cx.tcx, ty), error_def_id, &[]) |
| 46 | + && item.ident.name == sym::Error |
| 47 | + && is_visible_outside_module(cx, item.owner_id.def_id) => |
| 48 | + { |
| 49 | + span_lint( |
| 50 | + cx, |
| 51 | + ERROR_IMPL_ERROR, |
| 52 | + item.ident.span, |
| 53 | + "exported type alias named `Error` that implements `Error`", |
| 54 | + ); |
| 55 | + }, |
| 56 | + ItemKind::Impl(imp) if let Some(trait_def_id) = imp.of_trait.and_then(|t| t.trait_def_id()) |
| 57 | + && error_def_id == trait_def_id |
| 58 | + && let Some(def_id) = path_res(cx, imp.self_ty).opt_def_id().and_then(DefId::as_local) |
| 59 | + && let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id) |
| 60 | + && let Some(ident) = cx.tcx.opt_item_ident(def_id.to_def_id()) |
| 61 | + && ident.name == sym::Error |
| 62 | + && is_visible_outside_module(cx, def_id) => |
| 63 | + { |
| 64 | + span_lint_hir_and_then( |
| 65 | + cx, |
| 66 | + ERROR_IMPL_ERROR, |
| 67 | + hir_id, |
| 68 | + ident.span, |
| 69 | + "exported type named `Error` that implements `Error`", |
| 70 | + |diag| { |
| 71 | + diag.span_note(item.span, "`Error` was implemented here"); |
| 72 | + } |
| 73 | + ); |
| 74 | + } |
| 75 | + _ => {}, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Do not lint private `Error`s, i.e., ones without any `pub` (minus `pub(self)` of course) and |
| 81 | +/// which aren't reexported |
| 82 | +fn is_visible_outside_module(cx: &LateContext<'_>, def_id: LocalDefId) -> bool { |
| 83 | + !matches!( |
| 84 | + cx.tcx.visibility(def_id), |
| 85 | + Visibility::Restricted(mod_def_id) if cx.tcx.parent_module_from_def_id(def_id).to_def_id() == mod_def_id |
| 86 | + ) |
| 87 | +} |
0 commit comments