Skip to content

Commit 09a7c64

Browse files
committed
Remove the configuration option
Also no longer lints non-exported types now
1 parent f4b3bb1 commit 09a7c64

File tree

7 files changed

+56
-75
lines changed

7 files changed

+56
-75
lines changed

clippy_lints/src/error_impl_error.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
22
use clippy_utils::path_res;
33
use clippy_utils::ty::implements_trait;
4-
use rustc_hir::def_id::DefId;
4+
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::{Item, ItemKind, Node};
66
use rustc_hir_analysis::hir_ty_to_ty;
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_session::{declare_tool_lint, impl_lint_pass};
8+
use rustc_middle::ty::Visibility;
9+
use rustc_session::{declare_lint_pass, declare_tool_lint};
910
use rustc_span::sym;
1011

1112
declare_clippy_lint! {
@@ -32,46 +33,40 @@ declare_clippy_lint! {
3233
restriction,
3334
"types named `Error` that implement `Error`"
3435
}
35-
impl_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]);
36-
37-
#[derive(Clone, Copy)]
38-
pub struct ErrorImplError {
39-
pub allow_private_error: bool,
40-
}
36+
declare_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]);
4137

4238
impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
4339
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
44-
let Self { allow_private_error } = *self;
4540
let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) else {
4641
return;
4742
};
48-
if allow_private_error && !cx.effective_visibilities.is_exported(item.owner_id.def_id) {
49-
return;
50-
}
43+
5144
match item.kind {
5245
ItemKind::TyAlias(ty, _) if implements_trait(cx, hir_ty_to_ty(cx.tcx, ty), error_def_id, &[])
53-
&& item.ident.name == sym::Error =>
46+
&& item.ident.name == sym::Error
47+
&& is_visible_outside_module(cx, item.owner_id.def_id) =>
5448
{
5549
span_lint(
5650
cx,
5751
ERROR_IMPL_ERROR,
5852
item.ident.span,
59-
"type alias named `Error` that implements `Error`",
53+
"exported type alias named `Error` that implements `Error`",
6054
);
6155
},
6256
ItemKind::Impl(imp) if let Some(trait_def_id) = imp.of_trait.and_then(|t| t.trait_def_id())
6357
&& error_def_id == trait_def_id
6458
&& let Some(def_id) = path_res(cx, imp.self_ty).opt_def_id().and_then(DefId::as_local)
6559
&& let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id)
6660
&& let Node::Item(ty_item) = cx.tcx.hir().get(hir_id)
67-
&& ty_item.ident.name == sym::Error =>
61+
&& ty_item.ident.name == sym::Error
62+
&& is_visible_outside_module(cx, ty_item.owner_id.def_id) =>
6863
{
6964
span_lint_hir_and_then(
7065
cx,
7166
ERROR_IMPL_ERROR,
7267
hir_id,
7368
ty_item.ident.span,
74-
"type named `Error` that implements `Error`",
69+
"exported type named `Error` that implements `Error`",
7570
|diag| {
7671
diag.span_note(item.span, "`Error` was implemented here");
7772
}
@@ -81,3 +76,12 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
8176
}
8277
}
8378
}
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+
}

clippy_lints/src/utils/conf.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,6 @@ define_Conf! {
551551
///
552552
/// Whether to allow `r#""#` when `r""` can be used
553553
(allow_one_hash_in_raw_strings: bool = false),
554-
/// Lint: ERROR_IMPL_ERROR.
555-
///
556-
/// Whether to allow private types named `Error` that implement `Error`
557-
(allow_private_error: bool = true),
558554
}
559555

560556
/// Search for the configuration file.

tests/ui-toml/error_impl_error/allow_private/clippy.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ui-toml/error_impl_error/disallow_private/clippy.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ui-toml/error_impl_error/error_impl_error.allow_private.stderr

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/ui-toml/error_impl_error/error_impl_error.rs renamed to tests/ui/error_impl_error.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//@revisions: allow_private disallow_private
2-
//@[allow_private] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/error_impl_error/allow_private
3-
//@[disallow_private] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/error_impl_error/disallow_private
41
#![allow(unused)]
52
#![warn(clippy::error_impl_error)]
63
#![no_main]
@@ -20,7 +17,7 @@ pub mod a {
2017

2118
mod b {
2219
#[derive(Debug)]
23-
enum Error {}
20+
pub(super) enum Error {}
2421

2522
impl std::fmt::Display for Error {
2623
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -58,7 +55,7 @@ pub mod d {
5855

5956
mod e {
6057
#[derive(Debug)]
61-
struct MyError;
58+
pub(super) struct MyError;
6259

6360
impl std::fmt::Display for MyError {
6461
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -69,6 +66,25 @@ mod e {
6966
impl std::error::Error for MyError {}
7067
}
7168

72-
mod f {
73-
type MyError = std::fmt::Error;
69+
pub mod f {
70+
pub type MyError = std::fmt::Error;
71+
}
72+
73+
// Do not lint module-private types
74+
75+
mod g {
76+
#[derive(Debug)]
77+
enum Error {}
78+
79+
impl std::fmt::Display for Error {
80+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81+
todo!()
82+
}
83+
}
84+
85+
impl std::error::Error for Error {}
86+
}
87+
88+
mod h {
89+
type Error = std::fmt::Error;
7490
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
error: type named `Error` that implements `Error`
2-
--> $DIR/error_impl_error.rs:10:16
1+
error: exported type named `Error` that implements `Error`
2+
--> $DIR/error_impl_error.rs:7:16
33
|
44
LL | pub struct Error;
55
| ^^^^^
66
|
77
note: `Error` was implemented here
8-
--> $DIR/error_impl_error.rs:18:5
8+
--> $DIR/error_impl_error.rs:15:5
99
|
1010
LL | impl std::error::Error for Error {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: `-D clippy::error-impl-error` implied by `-D warnings`
1313

14-
error: type named `Error` that implements `Error`
15-
--> $DIR/error_impl_error.rs:23:10
14+
error: exported type named `Error` that implements `Error`
15+
--> $DIR/error_impl_error.rs:20:21
1616
|
17-
LL | enum Error {}
18-
| ^^^^^
17+
LL | pub(super) enum Error {}
18+
| ^^^^^
1919
|
2020
note: `Error` was implemented here
21-
--> $DIR/error_impl_error.rs:31:5
21+
--> $DIR/error_impl_error.rs:28:5
2222
|
2323
LL | impl std::error::Error for Error {}
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525

26-
error: type named `Error` that implements `Error`
27-
--> $DIR/error_impl_error.rs:35:15
26+
error: exported type named `Error` that implements `Error`
27+
--> $DIR/error_impl_error.rs:32:15
2828
|
2929
LL | pub union Error {
3030
| ^^^^^
3131
|
3232
note: `Error` was implemented here
33-
--> $DIR/error_impl_error.rs:52:5
33+
--> $DIR/error_impl_error.rs:49:5
3434
|
3535
LL | impl std::error::Error for Error {}
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3737

38-
error: type alias named `Error` that implements `Error`
39-
--> $DIR/error_impl_error.rs:56:14
38+
error: exported type alias named `Error` that implements `Error`
39+
--> $DIR/error_impl_error.rs:53:14
4040
|
4141
LL | pub type Error = std::fmt::Error;
4242
| ^^^^^

0 commit comments

Comments
 (0)