@@ -6,7 +6,7 @@ use clippy_utils::{
66use rustc_hir:: { def_id:: DefId , Item , ItemKind , Node } ;
77use rustc_hir_analysis:: hir_ty_to_ty;
88use rustc_lint:: { LateContext , LateLintPass } ;
9- use rustc_session:: { declare_lint_pass , declare_tool_lint } ;
9+ use rustc_session:: { declare_tool_lint , impl_lint_pass } ;
1010use rustc_span:: sym;
1111
1212declare_clippy_lint ! {
@@ -15,8 +15,9 @@ declare_clippy_lint! {
1515 ///
1616 /// ### Why is this bad?
1717 /// It can become confusing when a codebase has 20 types all named `Error`, requiring either
18- /// aliasing them in the `use` statement them or qualifying them like `my_module::Error`. This
19- /// severely hinders readability.
18+ /// aliasing them in the `use` statement or qualifying them like `my_module::Error`. This
19+ /// severely hinders comprehension, as it requires you to memorize every variation of importing
20+ /// `Error` used across a codebase.
2021 ///
2122 /// ### Example
2223 /// ```rust,ignore
@@ -32,14 +33,22 @@ declare_clippy_lint! {
3233 restriction,
3334 "types named `Error` that implement `Error`"
3435}
35- declare_lint_pass ! ( ErrorImplError => [ ERROR_IMPL_ERROR ] ) ;
36+ impl_lint_pass ! ( ErrorImplError => [ ERROR_IMPL_ERROR ] ) ;
37+
38+ #[ derive( Clone , Copy ) ]
39+ pub struct ErrorImplError {
40+ pub allow_private_error : bool ,
41+ }
3642
3743impl < ' tcx > LateLintPass < ' tcx > for ErrorImplError {
3844 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
45+ let Self { allow_private_error } = * self ;
3946 let Some ( error_def_id) = cx. tcx . get_diagnostic_item ( sym:: Error ) else {
4047 return ;
4148 } ;
42-
49+ if allow_private_error && !cx. effective_visibilities . is_exported ( item. owner_id . def_id ) {
50+ return ;
51+ }
4352 match item. kind {
4453 ItemKind :: TyAlias ( ty, _) if implements_trait ( cx, hir_ty_to_ty ( cx. tcx , ty) , error_def_id, & [ ] )
4554 && item. ident . name == sym:: Error =>
@@ -71,6 +80,5 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
7180 }
7281 _ => { } ,
7382 }
74- { }
7583 }
7684}
0 commit comments