Skip to content

Commit 1adf54d

Browse files
committed
New lint [error_impl_error]
1 parent 3f4e599 commit 1adf54d

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,6 +4785,7 @@ Released 2018-09-13
47854785
[`equatable_if_let`]: https://rust-lang.github.io/rust-clippy/master/index.html#equatable_if_let
47864786
[`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op
47874787
[`err_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#err_expect
4788+
[`error_impl_error`]: https://rust-lang.github.io/rust-clippy/master/index.html#error_impl_error
47884789
[`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence
47894790
[`excessive_nesting`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_nesting
47904791
[`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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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};
7+
use rustc_hir_analysis::hir_ty_to_ty;
8+
use rustc_lint::{LateContext, LateLintPass};
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 them or qualifying them like `my_module::Error`. This
19+
/// severely hinders readability.
20+
///
21+
/// ### Example
22+
/// ```rust,ignore
23+
/// #[derive(Debug)]
24+
/// pub enum Error { ... }
25+
///
26+
/// impl std::fmt::Display for Error { ... }
27+
///
28+
/// impl std::error::Error for Error { ... }
29+
/// ```
30+
#[clippy::version = "1.72.0"]
31+
pub ERROR_IMPL_ERROR,
32+
restriction,
33+
"types named `Error` that implement `Error`"
34+
}
35+
declare_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]);
36+
37+
impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
38+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
39+
let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) else {
40+
return;
41+
};
42+
43+
match item.kind {
44+
ItemKind::TyAlias(ty, _) if implements_trait(cx, hir_ty_to_ty(cx.tcx, ty), error_def_id, &[])
45+
&& item.ident.name == sym::Error =>
46+
{
47+
span_lint(
48+
cx,
49+
ERROR_IMPL_ERROR,
50+
item.ident.span,
51+
"type alias named `Error` that implements `Error`",
52+
);
53+
},
54+
ItemKind::Impl(imp) if let Some(trait_def_id) = imp.of_trait.and_then(|t| t.trait_def_id())
55+
&& error_def_id == trait_def_id
56+
&& let Some(def_id) = path_res(cx, imp.self_ty).opt_def_id().and_then(DefId::as_local)
57+
&& let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id)
58+
&& let Node::Item(ty_item) = cx.tcx.hir().get(hir_id)
59+
&& ty_item.ident.name == sym::Error =>
60+
{
61+
span_lint_hir_and_then(
62+
cx,
63+
ERROR_IMPL_ERROR,
64+
hir_id,
65+
ty_item.ident.span,
66+
"type named `Error` that implements `Error`",
67+
|diag| {
68+
diag.span_note(item.span, "`Error` was implemented here");
69+
}
70+
);
71+
}
72+
_ => {},
73+
}
74+
{}
75+
}
76+
}

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;
@@ -1073,6 +1074,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10731074
store.register_late_pass(|_| Box::new(manual_range_patterns::ManualRangePatterns));
10741075
store.register_early_pass(|| Box::new(visibility::Visibility));
10751076
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions { msrv: msrv() }));
1077+
store.register_late_pass(|_| Box::new(error_impl_error::ErrorImplError));
10761078
// add lints here, do not remove this comment, it's used in `new_lint`
10771079
}
10781080

tests/ui/error_impl_error.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#![allow(unused)]
2+
#![warn(clippy::error_impl_error)]
3+
#![no_main]
4+
5+
mod a {
6+
#[derive(Debug)]
7+
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+
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+
mod c {
32+
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+
mod d {
53+
type Error = std::fmt::Error;
54+
}
55+
56+
mod e {
57+
#[derive(Debug)]
58+
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+
mod f {
70+
type MyError = std::fmt::Error;
71+
}

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: type named `Error` that implements `Error`
2+
--> $DIR/error_impl_error.rs:7:12
3+
|
4+
LL | 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: type named `Error` that implements `Error`
15+
--> $DIR/error_impl_error.rs:20:10
16+
|
17+
LL | 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: type named `Error` that implements `Error`
27+
--> $DIR/error_impl_error.rs:32:11
28+
|
29+
LL | 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: type alias named `Error` that implements `Error`
39+
--> $DIR/error_impl_error.rs:53:10
40+
|
41+
LL | type Error = std::fmt::Error;
42+
| ^^^^^
43+
44+
error: aborting due to 4 previous errors
45+

0 commit comments

Comments
 (0)