2023-11-02 11:35:56 -05:00
|
|
|
use clippy_config::msrvs::{self, Msrv};
|
2022-06-30 03:50:09 -05:00
|
|
|
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
|
2022-12-01 11:29:38 -06:00
|
|
|
use clippy_utils::is_doc_hidden;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::snippet_opt;
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_ast::ast::{self, VisibilityKind};
|
2023-10-06 10:35:45 -05:00
|
|
|
use rustc_ast::attr;
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-05-11 13:23:47 -05:00
|
|
|
use rustc_errors::Applicability;
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
|
|
|
|
use rustc_hir::{self as hir, Expr, ExprKind, QPath};
|
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::impl_lint_pass;
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_span::def_id::{DefId, LocalDefId};
|
2020-11-05 07:29:48 -06:00
|
|
|
use rustc_span::{sym, Span};
|
2020-05-11 13:23:47 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for manual implementations of the non-exhaustive pattern.
|
2020-05-11 13:23:47 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Using the #[non_exhaustive] attribute expresses better the intent
|
2020-05-11 13:23:47 -05:00
|
|
|
/// and allows possible optimizations when applied to enums.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2020-05-11 13:23:47 -05:00
|
|
|
/// struct S {
|
|
|
|
/// pub a: i32,
|
|
|
|
/// pub b: i32,
|
|
|
|
/// _c: (),
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// enum E {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// #[doc(hidden)]
|
|
|
|
/// _C,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct T(pub i32, pub i32, ());
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2020-05-11 13:23:47 -05:00
|
|
|
/// #[non_exhaustive]
|
|
|
|
/// struct S {
|
|
|
|
/// pub a: i32,
|
|
|
|
/// pub b: i32,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[non_exhaustive]
|
|
|
|
/// enum E {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[non_exhaustive]
|
|
|
|
/// struct T(pub i32, pub i32);
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.45.0"]
|
2020-05-11 13:23:47 -05:00
|
|
|
pub MANUAL_NON_EXHAUSTIVE,
|
|
|
|
style,
|
|
|
|
"manual implementations of the non-exhaustive pattern can be simplified using #[non_exhaustive]"
|
|
|
|
}
|
|
|
|
|
2022-05-21 06:24:00 -05:00
|
|
|
#[expect(clippy::module_name_repetitions)]
|
2022-05-05 09:12:52 -05:00
|
|
|
pub struct ManualNonExhaustiveStruct {
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv: Msrv,
|
2020-12-06 08:01:03 -06:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
impl ManualNonExhaustiveStruct {
|
2020-12-06 08:01:03 -06:00
|
|
|
#[must_use]
|
2022-12-01 11:29:38 -06:00
|
|
|
pub fn new(msrv: Msrv) -> Self {
|
2020-12-06 08:01:03 -06:00
|
|
|
Self { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
impl_lint_pass!(ManualNonExhaustiveStruct => [MANUAL_NON_EXHAUSTIVE]);
|
2020-05-11 13:23:47 -05:00
|
|
|
|
2022-05-21 06:24:00 -05:00
|
|
|
#[expect(clippy::module_name_repetitions)]
|
2022-05-05 09:12:52 -05:00
|
|
|
pub struct ManualNonExhaustiveEnum {
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv: Msrv,
|
2022-05-05 09:12:52 -05:00
|
|
|
constructed_enum_variants: FxHashSet<(DefId, DefId)>,
|
|
|
|
potential_enums: Vec<(LocalDefId, LocalDefId, Span, Span)>,
|
|
|
|
}
|
2020-12-06 08:01:03 -06:00
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
impl ManualNonExhaustiveEnum {
|
|
|
|
#[must_use]
|
2022-12-01 11:29:38 -06:00
|
|
|
pub fn new(msrv: Msrv) -> Self {
|
2022-05-05 09:12:52 -05:00
|
|
|
Self {
|
|
|
|
msrv,
|
|
|
|
constructed_enum_variants: FxHashSet::default(),
|
|
|
|
potential_enums: Vec::new(),
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
impl_lint_pass!(ManualNonExhaustiveEnum => [MANUAL_NON_EXHAUSTIVE]);
|
2020-05-11 13:23:47 -05:00
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
impl EarlyLintPass for ManualNonExhaustiveStruct {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
|
2022-12-01 11:29:38 -06:00
|
|
|
if !self.msrv.meets(msrvs::NON_EXHAUSTIVE) {
|
2022-05-05 09:12:52 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let ast::ItemKind::Struct(variant_data, _) = &item.kind {
|
|
|
|
let (fields, delimiter) = match variant_data {
|
2023-12-19 16:47:30 -06:00
|
|
|
ast::VariantData::Struct { fields, .. } => (&**fields, '{'),
|
2022-05-05 09:12:52 -05:00
|
|
|
ast::VariantData::Tuple(fields, _) => (&**fields, '('),
|
|
|
|
ast::VariantData::Unit(_) => return,
|
|
|
|
};
|
|
|
|
if fields.len() <= 1 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let mut iter = fields.iter().filter_map(|f| match f.vis.kind {
|
|
|
|
VisibilityKind::Public => None,
|
|
|
|
VisibilityKind::Inherited => Some(Ok(f)),
|
2022-06-04 06:34:07 -05:00
|
|
|
VisibilityKind::Restricted { .. } => Some(Err(())),
|
2022-05-05 09:12:52 -05:00
|
|
|
});
|
|
|
|
if let Some(Ok(field)) = iter.next()
|
|
|
|
&& iter.next().is_none()
|
|
|
|
&& field.ty.kind.is_unit()
|
|
|
|
{
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
MANUAL_NON_EXHAUSTIVE,
|
|
|
|
item.span,
|
|
|
|
"this seems like a manual implementation of the non-exhaustive pattern",
|
|
|
|
|diag| {
|
|
|
|
if !item.attrs.iter().any(|attr| attr.has_name(sym::non_exhaustive))
|
|
|
|
&& let header_span = cx.sess().source_map().span_until_char(item.span, delimiter)
|
|
|
|
&& let Some(snippet) = snippet_opt(cx, header_span)
|
|
|
|
{
|
2020-05-11 13:23:47 -05:00
|
|
|
diag.span_suggestion(
|
|
|
|
header_span,
|
|
|
|
"add the attribute",
|
2022-10-06 02:44:38 -05:00
|
|
|
format!("#[non_exhaustive] {snippet}"),
|
2020-05-11 13:23:47 -05:00
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
diag.span_help(field.span, "remove this field");
|
2023-11-02 11:35:56 -05:00
|
|
|
},
|
2022-05-05 09:12:52 -05:00
|
|
|
);
|
|
|
|
}
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
|
|
|
|
extract_msrv_attr!(EarlyContext);
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustiveEnum {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
2022-12-01 11:29:38 -06:00
|
|
|
if !self.msrv.meets(msrvs::NON_EXHAUSTIVE) {
|
2022-05-05 09:12:52 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-05-11 13:23:47 -05:00
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
if let hir::ItemKind::Enum(def, _) = &item.kind
|
|
|
|
&& def.variants.len() > 1
|
|
|
|
{
|
|
|
|
let mut iter = def.variants.iter().filter_map(|v| {
|
2022-11-05 10:33:58 -05:00
|
|
|
(matches!(v.data, hir::VariantData::Unit(_, _))
|
2023-10-06 10:35:45 -05:00
|
|
|
&& is_doc_hidden(cx.tcx.hir().attrs(v.hir_id))
|
|
|
|
&& !attr::contains_name(cx.tcx.hir().attrs(item.hir_id()), sym::non_exhaustive))
|
2023-01-22 12:00:33 -06:00
|
|
|
.then_some((v.def_id, v.span))
|
2022-05-05 09:12:52 -05:00
|
|
|
});
|
|
|
|
if let Some((id, span)) = iter.next()
|
|
|
|
&& iter.next().is_none()
|
|
|
|
{
|
2022-10-26 22:02:18 -05:00
|
|
|
self.potential_enums.push((item.owner_id.def_id, id, item.span, span));
|
2022-05-05 09:12:52 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
|
|
|
if let ExprKind::Path(QPath::Resolved(None, p)) = &e.kind
|
|
|
|
&& let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), id) = p.res
|
|
|
|
{
|
|
|
|
let variant_id = cx.tcx.parent(id);
|
|
|
|
let enum_id = cx.tcx.parent(variant_id);
|
2020-05-11 13:23:47 -05:00
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
self.constructed_enum_variants.insert((enum_id, variant_id));
|
|
|
|
}
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:12:52 -05:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
|
|
|
|
for &(enum_id, _, enum_span, variant_span) in
|
|
|
|
self.potential_enums.iter().filter(|&&(enum_id, variant_id, _, _)| {
|
|
|
|
!self
|
|
|
|
.constructed_enum_variants
|
|
|
|
.contains(&(enum_id.to_def_id(), variant_id.to_def_id()))
|
|
|
|
})
|
|
|
|
{
|
2023-11-24 10:28:19 -06:00
|
|
|
let hir_id = cx.tcx.local_def_id_to_hir_id(enum_id);
|
2022-06-30 03:50:09 -05:00
|
|
|
span_lint_hir_and_then(
|
2020-05-11 13:23:47 -05:00
|
|
|
cx,
|
|
|
|
MANUAL_NON_EXHAUSTIVE,
|
2022-06-30 03:50:09 -05:00
|
|
|
hir_id,
|
2022-05-05 09:12:52 -05:00
|
|
|
enum_span,
|
2020-05-11 13:23:47 -05:00
|
|
|
"this seems like a manual implementation of the non-exhaustive pattern",
|
|
|
|
|diag| {
|
2023-10-06 10:35:45 -05:00
|
|
|
let header_span = cx.sess().source_map().span_until_char(enum_span, '{');
|
|
|
|
if let Some(snippet) = snippet_opt(cx, header_span) {
|
|
|
|
diag.span_suggestion(
|
|
|
|
header_span,
|
|
|
|
"add the attribute",
|
|
|
|
format!("#[non_exhaustive] {snippet}"),
|
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
diag.span_help(variant_span, "remove this variant");
|
|
|
|
},
|
|
|
|
);
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
2020-05-11 13:23:47 -05:00
|
|
|
}
|