@@ -6,7 +6,7 @@ use clippy_utils::{
6
6
use rustc_hir:: { def_id:: DefId , Item , ItemKind , Node } ;
7
7
use rustc_hir_analysis:: hir_ty_to_ty;
8
8
use rustc_lint:: { LateContext , LateLintPass } ;
9
- use rustc_session:: { declare_lint_pass , declare_tool_lint } ;
9
+ use rustc_session:: { declare_tool_lint , impl_lint_pass } ;
10
10
use rustc_span:: sym;
11
11
12
12
declare_clippy_lint ! {
@@ -15,8 +15,9 @@ declare_clippy_lint! {
15
15
///
16
16
/// ### Why is this bad?
17
17
/// 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
+ /// hinders comprehension, as it requires you to memorize every variation of importing `Error`
20
+ /// used across a codebase.
20
21
///
21
22
/// ### Example
22
23
/// ```rust,ignore
@@ -32,14 +33,22 @@ declare_clippy_lint! {
32
33
restriction,
33
34
"types named `Error` that implement `Error`"
34
35
}
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
+ }
36
42
37
43
impl < ' tcx > LateLintPass < ' tcx > for ErrorImplError {
38
44
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
45
+ let Self { allow_private_error } = * self ;
39
46
let Some ( error_def_id) = cx. tcx . get_diagnostic_item ( sym:: Error ) else {
40
47
return ;
41
48
} ;
42
-
49
+ if allow_private_error && !cx. effective_visibilities . is_exported ( item. owner_id . def_id ) {
50
+ return ;
51
+ }
43
52
match item. kind {
44
53
ItemKind :: TyAlias ( ty, _) if implements_trait ( cx, hir_ty_to_ty ( cx. tcx , ty) , error_def_id, & [ ] )
45
54
&& item. ident . name == sym:: Error =>
@@ -71,6 +80,5 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
71
80
}
72
81
_ => { } ,
73
82
}
74
- { }
75
83
}
76
84
}
0 commit comments