1- use clippy_utils:: {
2- diagnostics:: { span_lint, span_lint_hir_and_then} ,
3- path_res,
4- ty:: implements_trait,
5- } ;
6- use rustc_hir:: { def_id:: DefId , Item , ItemKind , Node } ;
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 ;
5+ use rustc_hir:: { Item , ItemKind , Node } ;
76use rustc_hir_analysis:: hir_ty_to_ty;
87use rustc_lint:: { LateContext , LateLintPass } ;
9- use rustc_session:: { declare_lint_pass , declare_tool_lint } ;
8+ use rustc_session:: { declare_tool_lint , impl_lint_pass } ;
109use rustc_span:: sym;
1110
1211declare_clippy_lint ! {
@@ -15,8 +14,9 @@ declare_clippy_lint! {
1514 ///
1615 /// ### Why is this bad?
1716 /// 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.
17+ /// aliasing them in the `use` statement or qualifying them like `my_module::Error`. This
18+ /// hinders comprehension, as it requires you to memorize every variation of importing `Error`
19+ /// used across a codebase.
2020 ///
2121 /// ### Example
2222 /// ```rust,ignore
@@ -32,14 +32,22 @@ declare_clippy_lint! {
3232 restriction,
3333 "types named `Error` that implement `Error`"
3434}
35- declare_lint_pass ! ( ErrorImplError => [ ERROR_IMPL_ERROR ] ) ;
35+ impl_lint_pass ! ( ErrorImplError => [ ERROR_IMPL_ERROR ] ) ;
36+
37+ #[ derive( Clone , Copy ) ]
38+ pub struct ErrorImplError {
39+ pub allow_private_error : bool ,
40+ }
3641
3742impl < ' tcx > LateLintPass < ' tcx > for ErrorImplError {
3843 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
44+ let Self { allow_private_error } = * self ;
3945 let Some ( error_def_id) = cx. tcx . get_diagnostic_item ( sym:: Error ) else {
4046 return ;
4147 } ;
42-
48+ if allow_private_error && !cx. effective_visibilities . is_exported ( item. owner_id . def_id ) {
49+ return ;
50+ }
4351 match item. kind {
4452 ItemKind :: TyAlias ( ty, _) if implements_trait ( cx, hir_ty_to_ty ( cx. tcx , ty) , error_def_id, & [ ] )
4553 && item. ident . name == sym:: Error =>
@@ -71,6 +79,5 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
7179 }
7280 _ => { } ,
7381 }
74- { }
7582 }
7683}
0 commit comments