mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 19:58:32 +00:00
Add allow_private_error
config option
This commit is contained in:
parent
75e1329aac
commit
f4b3bb19c1
@ -5465,4 +5465,5 @@ Released 2018-09-13
|
|||||||
[`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
|
[`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
|
||||||
[`accept-comment-above-attributes`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-attributes
|
[`accept-comment-above-attributes`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-attributes
|
||||||
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
|
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
|
||||||
|
[`allow-private-error`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-private-error
|
||||||
<!-- end autogenerated links to configuration documentation -->
|
<!-- end autogenerated links to configuration documentation -->
|
||||||
|
@ -730,3 +730,13 @@ Whether to allow `r#""#` when `r""` can be used
|
|||||||
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)
|
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)
|
||||||
|
|
||||||
|
|
||||||
|
## `allow-private-error`
|
||||||
|
Whether to allow private types named `Error` that implement `Error`
|
||||||
|
|
||||||
|
**Default Value:** `true` (`bool`)
|
||||||
|
|
||||||
|
---
|
||||||
|
**Affected lints:**
|
||||||
|
* [`error_impl_error`](https://rust-lang.github.io/rust-clippy/master/index.html#error_impl_error)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
use clippy_utils::{
|
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
|
||||||
diagnostics::{span_lint, span_lint_hir_and_then},
|
use clippy_utils::path_res;
|
||||||
path_res,
|
use clippy_utils::ty::implements_trait;
|
||||||
ty::implements_trait,
|
use rustc_hir::def_id::DefId;
|
||||||
};
|
use rustc_hir::{Item, ItemKind, Node};
|
||||||
use rustc_hir::{def_id::DefId, Item, ItemKind, Node};
|
|
||||||
use rustc_hir_analysis::hir_ty_to_ty;
|
use rustc_hir_analysis::hir_ty_to_ty;
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
@ -15,8 +14,9 @@ declare_clippy_lint! {
|
|||||||
///
|
///
|
||||||
/// ### Why is this bad?
|
/// ### Why is this bad?
|
||||||
/// It can become confusing when a codebase has 20 types all named `Error`, requiring either
|
/// It can become confusing when a codebase has 20 types all named `Error`, requiring either
|
||||||
/// aliasing them in the `use` statement them or qualifying them like `my_module::Error`. This
|
/// aliasing them in the `use` statement or qualifying them like `my_module::Error`. This
|
||||||
/// severely hinders readability.
|
/// hinders comprehension, as it requires you to memorize every variation of importing `Error`
|
||||||
|
/// used across a codebase.
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
@ -32,14 +32,22 @@ declare_clippy_lint! {
|
|||||||
restriction,
|
restriction,
|
||||||
"types named `Error` that implement `Error`"
|
"types named `Error` that implement `Error`"
|
||||||
}
|
}
|
||||||
declare_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]);
|
impl_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct ErrorImplError {
|
||||||
|
pub allow_private_error: bool,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
|
impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
|
||||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
||||||
|
let Self { allow_private_error } = *self;
|
||||||
let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) else {
|
let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
if allow_private_error && !cx.effective_visibilities.is_exported(item.owner_id.def_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
match item.kind {
|
match item.kind {
|
||||||
ItemKind::TyAlias(ty, _) if implements_trait(cx, hir_ty_to_ty(cx.tcx, ty), error_def_id, &[])
|
ItemKind::TyAlias(ty, _) if implements_trait(cx, hir_ty_to_ty(cx.tcx, ty), error_def_id, &[])
|
||||||
&& item.ident.name == sym::Error =>
|
&& item.ident.name == sym::Error =>
|
||||||
@ -71,6 +79,5 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
|
|||||||
}
|
}
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
{}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -551,6 +551,10 @@ define_Conf! {
|
|||||||
///
|
///
|
||||||
/// Whether to allow `r#""#` when `r""` can be used
|
/// Whether to allow `r#""#` when `r""` can be used
|
||||||
(allow_one_hash_in_raw_strings: bool = false),
|
(allow_one_hash_in_raw_strings: bool = false),
|
||||||
|
/// Lint: ERROR_IMPL_ERROR.
|
||||||
|
///
|
||||||
|
/// Whether to allow private types named `Error` that implement `Error`
|
||||||
|
(allow_private_error: bool = true),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search for the configuration file.
|
/// Search for the configuration file.
|
||||||
|
1
tests/ui-toml/error_impl_error/allow_private/clippy.toml
Normal file
1
tests/ui-toml/error_impl_error/allow_private/clippy.toml
Normal file
@ -0,0 +1 @@
|
|||||||
|
allow-private-error = true
|
@ -0,0 +1 @@
|
|||||||
|
allow-private-error = false
|
@ -0,0 +1,33 @@
|
|||||||
|
error: type named `Error` that implements `Error`
|
||||||
|
--> $DIR/error_impl_error.rs:10:16
|
||||||
|
|
|
||||||
|
LL | pub struct Error;
|
||||||
|
| ^^^^^
|
||||||
|
|
|
||||||
|
note: `Error` was implemented here
|
||||||
|
--> $DIR/error_impl_error.rs:18:5
|
||||||
|
|
|
||||||
|
LL | impl std::error::Error for Error {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
= note: `-D clippy::error-impl-error` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: type named `Error` that implements `Error`
|
||||||
|
--> $DIR/error_impl_error.rs:35:15
|
||||||
|
|
|
||||||
|
LL | pub union Error {
|
||||||
|
| ^^^^^
|
||||||
|
|
|
||||||
|
note: `Error` was implemented here
|
||||||
|
--> $DIR/error_impl_error.rs:52:5
|
||||||
|
|
|
||||||
|
LL | impl std::error::Error for Error {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: type alias named `Error` that implements `Error`
|
||||||
|
--> $DIR/error_impl_error.rs:56:14
|
||||||
|
|
|
||||||
|
LL | pub type Error = std::fmt::Error;
|
||||||
|
| ^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
@ -1,45 +1,45 @@
|
|||||||
error: type named `Error` that implements `Error`
|
error: type named `Error` that implements `Error`
|
||||||
--> $DIR/error_impl_error.rs:7:12
|
--> $DIR/error_impl_error.rs:10:16
|
||||||
|
|
|
|
||||||
LL | struct Error;
|
LL | pub struct Error;
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
|
||||||
note: `Error` was implemented here
|
note: `Error` was implemented here
|
||||||
--> $DIR/error_impl_error.rs:15:5
|
--> $DIR/error_impl_error.rs:18:5
|
||||||
|
|
|
|
||||||
LL | impl std::error::Error for Error {}
|
LL | impl std::error::Error for Error {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
= note: `-D clippy::error-impl-error` implied by `-D warnings`
|
= note: `-D clippy::error-impl-error` implied by `-D warnings`
|
||||||
|
|
||||||
error: type named `Error` that implements `Error`
|
error: type named `Error` that implements `Error`
|
||||||
--> $DIR/error_impl_error.rs:20:10
|
--> $DIR/error_impl_error.rs:23:10
|
||||||
|
|
|
|
||||||
LL | enum Error {}
|
LL | enum Error {}
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
|
||||||
note: `Error` was implemented here
|
note: `Error` was implemented here
|
||||||
--> $DIR/error_impl_error.rs:28:5
|
--> $DIR/error_impl_error.rs:31:5
|
||||||
|
|
|
|
||||||
LL | impl std::error::Error for Error {}
|
LL | impl std::error::Error for Error {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: type named `Error` that implements `Error`
|
error: type named `Error` that implements `Error`
|
||||||
--> $DIR/error_impl_error.rs:32:11
|
--> $DIR/error_impl_error.rs:35:15
|
||||||
|
|
|
|
||||||
LL | union Error {
|
LL | pub union Error {
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
|
||||||
note: `Error` was implemented here
|
note: `Error` was implemented here
|
||||||
--> $DIR/error_impl_error.rs:49:5
|
--> $DIR/error_impl_error.rs:52:5
|
||||||
|
|
|
|
||||||
LL | impl std::error::Error for Error {}
|
LL | impl std::error::Error for Error {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: type alias named `Error` that implements `Error`
|
error: type alias named `Error` that implements `Error`
|
||||||
--> $DIR/error_impl_error.rs:53:10
|
--> $DIR/error_impl_error.rs:56:14
|
||||||
|
|
|
|
||||||
LL | type Error = std::fmt::Error;
|
LL | pub type Error = std::fmt::Error;
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
@ -1,10 +1,13 @@
|
|||||||
|
//@revisions: allow_private disallow_private
|
||||||
|
//@[allow_private] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/error_impl_error/allow_private
|
||||||
|
//@[disallow_private] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/error_impl_error/disallow_private
|
||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
#![warn(clippy::error_impl_error)]
|
#![warn(clippy::error_impl_error)]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
mod a {
|
pub mod a {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Error;
|
pub struct Error;
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
impl std::fmt::Display for Error {
|
||||||
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
@ -28,8 +31,8 @@ mod b {
|
|||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod c {
|
pub mod c {
|
||||||
union Error {
|
pub union Error {
|
||||||
a: u32,
|
a: u32,
|
||||||
b: u32,
|
b: u32,
|
||||||
}
|
}
|
||||||
@ -49,8 +52,8 @@ mod c {
|
|||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod d {
|
pub mod d {
|
||||||
type Error = std::fmt::Error;
|
pub type Error = std::fmt::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
mod e {
|
mod e {
|
@ -6,6 +6,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
|
|||||||
allow-mixed-uninlined-format-args
|
allow-mixed-uninlined-format-args
|
||||||
allow-one-hash-in-raw-strings
|
allow-one-hash-in-raw-strings
|
||||||
allow-print-in-tests
|
allow-print-in-tests
|
||||||
|
allow-private-error
|
||||||
allow-private-module-inception
|
allow-private-module-inception
|
||||||
allow-unwrap-in-tests
|
allow-unwrap-in-tests
|
||||||
allowed-idents-below-min-chars
|
allowed-idents-below-min-chars
|
||||||
@ -75,6 +76,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
|
|||||||
allow-mixed-uninlined-format-args
|
allow-mixed-uninlined-format-args
|
||||||
allow-one-hash-in-raw-strings
|
allow-one-hash-in-raw-strings
|
||||||
allow-print-in-tests
|
allow-print-in-tests
|
||||||
|
allow-private-error
|
||||||
allow-private-module-inception
|
allow-private-module-inception
|
||||||
allow-unwrap-in-tests
|
allow-unwrap-in-tests
|
||||||
allowed-idents-below-min-chars
|
allowed-idents-below-min-chars
|
||||||
|
Loading…
Reference in New Issue
Block a user