2023-07-31 23:53:53 +02:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
|
|
|
|
use clippy_utils::path_res;
|
|
|
|
use clippy_utils::ty::implements_trait;
|
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
|
|
|
use rustc_hir::{Item, ItemKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty::Visibility;
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-07-31 23:53:53 +02:00
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for types named `Error` that implement `Error`.
|
|
|
|
///
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-22 22:21:01 -07:00
|
|
|
/// ### Why restrict this?
|
2023-07-31 23:53:53 +02:00
|
|
|
/// It can become confusing when a codebase has 20 types all named `Error`, requiring either
|
|
|
|
/// aliasing them in the `use` statement or qualifying them like `my_module::Error`. This
|
|
|
|
/// hinders comprehension, as it requires you to memorize every variation of importing `Error`
|
|
|
|
/// used across a codebase.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// pub enum Error { ... }
|
|
|
|
///
|
|
|
|
/// impl std::fmt::Display for Error { ... }
|
|
|
|
///
|
|
|
|
/// impl std::error::Error for Error { ... }
|
|
|
|
/// ```
|
2023-10-06 17:35:45 +02:00
|
|
|
#[clippy::version = "1.73.0"]
|
2023-07-31 23:53:53 +02:00
|
|
|
pub ERROR_IMPL_ERROR,
|
|
|
|
restriction,
|
|
|
|
"exported types named `Error` that implement `Error`"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(ErrorImplError => [ERROR_IMPL_ERROR]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
|
|
|
let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
match item.kind {
|
2023-11-02 17:35:56 +01:00
|
|
|
ItemKind::TyAlias(..)
|
|
|
|
if item.ident.name == sym::Error
|
|
|
|
&& is_visible_outside_module(cx, item.owner_id.def_id)
|
|
|
|
&& let ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
|
|
|
|
&& implements_trait(cx, ty, error_def_id, &[]) =>
|
2023-07-31 23:53:53 +02:00
|
|
|
{
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
ERROR_IMPL_ERROR,
|
|
|
|
item.ident.span,
|
|
|
|
"exported type alias named `Error` that implements `Error`",
|
|
|
|
);
|
|
|
|
},
|
2023-11-02 17:35:56 +01:00
|
|
|
ItemKind::Impl(imp)
|
|
|
|
if let Some(trait_def_id) = imp.of_trait.and_then(|t| t.trait_def_id())
|
|
|
|
&& error_def_id == trait_def_id
|
|
|
|
&& let Some(def_id) = path_res(cx, imp.self_ty).opt_def_id().and_then(DefId::as_local)
|
2023-11-24 19:28:19 +03:00
|
|
|
&& let hir_id = cx.tcx.local_def_id_to_hir_id(def_id)
|
2023-11-02 17:35:56 +01:00
|
|
|
&& let Some(ident) = cx.tcx.opt_item_ident(def_id.to_def_id())
|
|
|
|
&& ident.name == sym::Error
|
|
|
|
&& is_visible_outside_module(cx, def_id) =>
|
2023-07-31 23:53:53 +02:00
|
|
|
{
|
|
|
|
span_lint_hir_and_then(
|
|
|
|
cx,
|
|
|
|
ERROR_IMPL_ERROR,
|
|
|
|
hir_id,
|
|
|
|
ident.span,
|
|
|
|
"exported type named `Error` that implements `Error`",
|
|
|
|
|diag| {
|
|
|
|
diag.span_note(item.span, "`Error` was implemented here");
|
2023-11-02 17:35:56 +01:00
|
|
|
},
|
2023-07-31 23:53:53 +02:00
|
|
|
);
|
2023-11-02 17:35:56 +01:00
|
|
|
},
|
2023-07-31 23:53:53 +02:00
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do not lint private `Error`s, i.e., ones without any `pub` (minus `pub(self)` of course) and
|
|
|
|
/// which aren't reexported
|
|
|
|
fn is_visible_outside_module(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
|
|
|
|
!matches!(
|
|
|
|
cx.tcx.visibility(def_id),
|
|
|
|
Visibility::Restricted(mod_def_id) if cx.tcx.parent_module_from_def_id(def_id).to_def_id() == mod_def_id
|
|
|
|
)
|
|
|
|
}
|