Skip to content

Commit d680000

Browse files
committed
Add allow_private_error config option
1 parent ea3c0f7 commit d680000

File tree

11 files changed

+95
-32
lines changed

11 files changed

+95
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5463,4 +5463,5 @@ Released 2018-09-13
54635463
[`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
54645464
[`accept-comment-above-attributes`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-attributes
54655465
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
5466+
[`allow-private-error`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-private-error
54665467
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,13 @@ Whether to allow `r#""#` when `r""` can be used
730730
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)
731731

732732

733+
## `allow-private-error`
734+
Whether to allow private types named `Error` that implement `Error`
735+
736+
**Default Value:** `true` (`bool`)
737+
738+
---
739+
**Affected lints:**
740+
* [`error_impl_error`](https://rust-lang.github.io/rust-clippy/master/index.html#error_impl_error)
741+
742+

clippy_lints/src/error_impl_error.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
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};
76
use rustc_hir_analysis::hir_ty_to_ty;
87
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_session::{declare_tool_lint, impl_lint_pass};
109
use rustc_span::sym;
1110

1211
declare_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

3742
impl<'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
}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10811081
store.register_early_pass(|| Box::new(visibility::Visibility));
10821082
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions { msrv: msrv() }));
10831083
store.register_late_pass(|_| Box::new(manual_float_methods::ManualFloatMethods));
1084-
store.register_late_pass(|_| Box::new(error_impl_error::ErrorImplError));
1084+
let allow_private_error = conf.allow_private_error;
1085+
store.register_late_pass(move |_| Box::new(error_impl_error::ErrorImplError { allow_private_error }));
10851086
// add lints here, do not remove this comment, it's used in `new_lint`
10861087
}
10871088

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ 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),
554558
}
555559

556560
/// Search for the configuration file.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-private-error = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-private-error = false
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: type named `Error` that implements `Error`
2+
--> $DIR/error_impl_error.rs:10:16
3+
|
4+
LL | pub struct Error;
5+
| ^^^^^
6+
|
7+
note: `Error` was implemented here
8+
--> $DIR/error_impl_error.rs:18: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:35:15
16+
|
17+
LL | pub union Error {
18+
| ^^^^^
19+
|
20+
note: `Error` was implemented here
21+
--> $DIR/error_impl_error.rs:52:5
22+
|
23+
LL | impl std::error::Error for Error {}
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
error: type alias named `Error` that implements `Error`
27+
--> $DIR/error_impl_error.rs:56:14
28+
|
29+
LL | pub type Error = std::fmt::Error;
30+
| ^^^^^
31+
32+
error: aborting due to 3 previous errors
33+
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
error: type named `Error` that implements `Error`
2-
--> $DIR/error_impl_error.rs:7:12
2+
--> $DIR/error_impl_error.rs:10:16
33
|
4-
LL | struct Error;
5-
| ^^^^^
4+
LL | pub struct Error;
5+
| ^^^^^
66
|
77
note: `Error` was implemented here
8-
--> $DIR/error_impl_error.rs:15:5
8+
--> $DIR/error_impl_error.rs:18:5
99
|
1010
LL | impl std::error::Error for Error {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: `-D clippy::error-impl-error` implied by `-D warnings`
1313

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

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

3838
error: type alias named `Error` that implements `Error`
39-
--> $DIR/error_impl_error.rs:53:10
39+
--> $DIR/error_impl_error.rs:56:14
4040
|
41-
LL | type Error = std::fmt::Error;
42-
| ^^^^^
41+
LL | pub type Error = std::fmt::Error;
42+
| ^^^^^
4343

4444
error: aborting due to 4 previous errors
4545

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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
14
#![allow(unused)]
25
#![warn(clippy::error_impl_error)]
36
#![no_main]
47

5-
mod a {
8+
pub mod a {
69
#[derive(Debug)]
7-
struct Error;
10+
pub struct Error;
811

912
impl std::fmt::Display for Error {
1013
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -28,8 +31,8 @@ mod b {
2831
impl std::error::Error for Error {}
2932
}
3033

31-
mod c {
32-
union Error {
34+
pub mod c {
35+
pub union Error {
3336
a: u32,
3437
b: u32,
3538
}
@@ -49,8 +52,8 @@ mod c {
4952
impl std::error::Error for Error {}
5053
}
5154

52-
mod d {
53-
type Error = std::fmt::Error;
55+
pub mod d {
56+
pub type Error = std::fmt::Error;
5457
}
5558

5659
mod e {

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
66
allow-mixed-uninlined-format-args
77
allow-one-hash-in-raw-strings
88
allow-print-in-tests
9+
allow-private-error
910
allow-private-module-inception
1011
allow-unwrap-in-tests
1112
allowed-idents-below-min-chars
@@ -75,6 +76,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
7576
allow-mixed-uninlined-format-args
7677
allow-one-hash-in-raw-strings
7778
allow-print-in-tests
79+
allow-private-error
7880
allow-private-module-inception
7981
allow-unwrap-in-tests
8082
allowed-idents-below-min-chars

0 commit comments

Comments
 (0)