1 use crate::utils::{snippet_opt, span_lint_and_help, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_hir::{Item, ItemKind};
4 use rustc_errors::Applicability;
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Warns on any exported `enum`s that are not tagged `#[non_exhaustive]`
12 /// **Why is this bad?** Exhaustive enums are typically fine, but a project which does
13 /// not wish to make a stability commitment around enums may wish to disable them by default.
15 /// **Known problems:** None.
34 "default lint description"
37 declare_lint_pass!(ExhaustiveItems => [EXHAUSTIVE_ENUMS]);
39 impl LateLintPass<'_> for ExhaustiveItems {
40 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
42 if let ItemKind::Enum(..) = item.kind;
43 if cx.access_levels.is_exported(item.hir_id);
44 if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
46 if let Some(snippet) = snippet_opt(cx, item.span) {
51 "enums should not be exhaustive",
52 "try adding #[non_exhaustive]",
53 format!("#[non_exhaustive]\n{}", snippet),
54 Applicability::MaybeIncorrect,
61 "enums should not be exhaustive",
63 "try adding #[non_exhaustive]",