Skip to content

Commit d71fbb9

Browse files
committed
Auto merge of rust-lang#11107 - Centri3:error_impl_error, r=Jarcho
New lint [`error_impl_error`] Closes rust-lang#11101 changelog: New lint [`error_impl_error`]
2 parents 0b63e95 + 19b0e84 commit d71fbb9

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,6 +4819,7 @@ Released 2018-09-13
48194819
[`equatable_if_let`]: https://rust-lang.github.io/rust-clippy/master/index.html#equatable_if_let
48204820
[`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op
48214821
[`err_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#err_expect
4822+
[`error_impl_error`]: https://rust-lang.github.io/rust-clippy/master/index.html#error_impl_error
48224823
[`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence
48234824
[`excessive_nesting`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_nesting
48244825
[`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
155155
crate::enum_variants::MODULE_INCEPTION_INFO,
156156
crate::enum_variants::MODULE_NAME_REPETITIONS_INFO,
157157
crate::equatable_if_let::EQUATABLE_IF_LET_INFO,
158+
crate::error_impl_error::ERROR_IMPL_ERROR_INFO,
158159
crate::escape::BOXED_LOCAL_INFO,
159160
crate::eta_reduction::REDUNDANT_CLOSURE_INFO,
160161
crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO,

clippy_lints/src/error_impl_error.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ mod entry;
120120
mod enum_clike;
121121
mod enum_variants;
122122
mod equatable_if_let;
123+
mod error_impl_error;
123124
mod escape;
124125
mod eta_reduction;
125126
mod excessive_bools;
@@ -1080,6 +1081,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10801081
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions { msrv: msrv() }));
10811082
store.register_late_pass(|_| Box::new(manual_float_methods::ManualFloatMethods));
10821083
store.register_late_pass(|_| Box::new(four_forward_slashes::FourForwardSlashes));
1084+
store.register_late_pass(|_| Box::new(error_impl_error::ErrorImplError));
10831085
// add lints here, do not remove this comment, it's used in `new_lint`
10841086
}
10851087

tests/ui/error_impl_error.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#![allow(unused)]
2+
#![warn(clippy::error_impl_error)]
3+
#![no_main]
4+
5+
pub mod a {
6+
#[derive(Debug)]
7+
pub struct Error;
8+
9+
impl std::fmt::Display for Error {
10+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11+
todo!()
12+
}
13+
}
14+
15+
impl std::error::Error for Error {}
16+
}
17+
18+
mod b {
19+
#[derive(Debug)]
20+
pub(super) enum Error {}
21+
22+
impl std::fmt::Display for Error {
23+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24+
todo!()
25+
}
26+
}
27+
28+
impl std::error::Error for Error {}
29+
}
30+
31+
pub mod c {
32+
pub union Error {
33+
a: u32,
34+
b: u32,
35+
}
36+
37+
impl std::fmt::Debug for Error {
38+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
todo!()
40+
}
41+
}
42+
43+
impl std::fmt::Display for Error {
44+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45+
todo!()
46+
}
47+
}
48+
49+
impl std::error::Error for Error {}
50+
}
51+
52+
pub mod d {
53+
pub type Error = std::fmt::Error;
54+
}
55+
56+
mod e {
57+
#[derive(Debug)]
58+
pub(super) struct MyError;
59+
60+
impl std::fmt::Display for MyError {
61+
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62+
todo!()
63+
}
64+
}
65+
66+
impl std::error::Error for MyError {}
67+
}
68+
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;
90+
}

tests/ui/error_impl_error.stderr

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error: exported type named `Error` that implements `Error`
2+
--> $DIR/error_impl_error.rs:7:16
3+
|
4+
LL | pub struct Error;
5+
| ^^^^^
6+
|
7+
note: `Error` was implemented here
8+
--> $DIR/error_impl_error.rs:15:5
9+
|
10+
LL | impl std::error::Error for Error {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: `-D clippy::error-impl-error` implied by `-D warnings`
13+
14+
error: exported type named `Error` that implements `Error`
15+
--> $DIR/error_impl_error.rs:20:21
16+
|
17+
LL | pub(super) enum Error {}
18+
| ^^^^^
19+
|
20+
note: `Error` was implemented here
21+
--> $DIR/error_impl_error.rs:28:5
22+
|
23+
LL | impl std::error::Error for Error {}
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
error: exported type named `Error` that implements `Error`
27+
--> $DIR/error_impl_error.rs:32:15
28+
|
29+
LL | pub union Error {
30+
| ^^^^^
31+
|
32+
note: `Error` was implemented here
33+
--> $DIR/error_impl_error.rs:49:5
34+
|
35+
LL | impl std::error::Error for Error {}
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
38+
error: exported type alias named `Error` that implements `Error`
39+
--> $DIR/error_impl_error.rs:53:14
40+
|
41+
LL | pub type Error = std::fmt::Error;
42+
| ^^^^^
43+
44+
error: aborting due to 4 previous errors
45+

0 commit comments

Comments
 (0)