Don't lint manual_non_exhaustive when enum explicitly marked as non_exhaustive

There are cases where users create a unit variant for the purposes
of tracking the number of variants for an nonexhaustive enum.
We should check if an enum is explicitly marked as nonexhaustive
before reporting `manual_non_exhaustive` in these cases. Fixes #11583
This commit is contained in:
Victor Song 2023-09-30 22:57:54 -05:00
parent 0e43a04fab
commit e683e3eeac
3 changed files with 5 additions and 22 deletions

View File

@ -3,6 +3,7 @@
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_opt;
use rustc_ast::ast::{self, VisibilityKind};
use rustc_ast::attr;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
@ -158,7 +159,8 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
let mut iter = def.variants.iter().filter_map(|v| {
(matches!(v.data, hir::VariantData::Unit(_, _))
&& v.ident.as_str().starts_with('_')
&& is_doc_hidden(cx.tcx.hir().attrs(v.hir_id)))
&& is_doc_hidden(cx.tcx.hir().attrs(v.hir_id))
&& !attr::contains_name(cx.tcx.hir().attrs(item.hir_id()), sym::non_exhaustive))
.then_some((v.def_id, v.span))
});
if let Some((id, span)) = iter.next()

View File

@ -10,10 +10,9 @@ enum E {
_C,
}
// user forgot to remove the marker
// if the user explicitly marks as nonexhaustive we shouldn't warn them
#[non_exhaustive]
enum Ep {
//~^ ERROR: this seems like a manual implementation of the non-exhaustive pattern
A,
B,
#[doc(hidden)]

View File

@ -22,23 +22,5 @@ LL | _C,
= note: `-D clippy::manual-non-exhaustive` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_non_exhaustive)]`
error: this seems like a manual implementation of the non-exhaustive pattern
--> $DIR/manual_non_exhaustive_enum.rs:15:1
|
LL | / enum Ep {
LL | |
LL | | A,
LL | | B,
LL | | #[doc(hidden)]
LL | | _C,
LL | | }
| |_^
|
help: remove this variant
--> $DIR/manual_non_exhaustive_enum.rs:20:5
|
LL | _C,
| ^^
error: aborting due to 2 previous errors
error: aborting due to previous error