Skip to content

Commit 9c9ee34

Browse files
Port #[non_exhaustive] to the new attribute parsing infrastructure
1 parent 2e9b294 commit 9c9ee34

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

clippy_lints/src/exhaustive_items.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use rustc_errors::Applicability;
44
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
7-
use rustc_span::sym;
7+
use rustc_attr_data_structures::AttributeKind;
8+
use rustc_attr_data_structures::find_attr;
9+
810

911
declare_clippy_lint! {
1012
/// ### What it does
@@ -85,7 +87,7 @@ impl LateLintPass<'_> for ExhaustiveItems {
8587
};
8688
if cx.effective_visibilities.is_exported(item.owner_id.def_id)
8789
&& let attrs = cx.tcx.hir_attrs(item.hir_id())
88-
&& !attrs.iter().any(|a| a.has_name(sym::non_exhaustive))
90+
&& !find_attr!(attrs, AttributeKind::NonExhaustive(..))
8991
&& fields.iter().all(|f| cx.tcx.visibility(f.def_id).is_public())
9092
{
9193
span_lint_and_then(cx, lint, item.span, msg, |diag| {

clippy_lints/src/manual_non_exhaustive.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ use clippy_utils::is_doc_hidden;
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::source::snippet_indent;
66
use itertools::Itertools;
7-
use rustc_ast::attr;
87
use rustc_data_structures::fx::FxHashSet;
98
use rustc_errors::Applicability;
109
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1110
use rustc_hir::{Expr, ExprKind, Item, ItemKind, QPath, TyKind, VariantData};
1211
use rustc_lint::{LateContext, LateLintPass};
1312
use rustc_session::impl_lint_pass;
1413
use rustc_span::def_id::LocalDefId;
15-
use rustc_span::{Span, sym};
14+
use rustc_span::Span;
15+
use rustc_attr_data_structures::find_attr;
16+
use rustc_attr_data_structures::AttributeKind;
1617

1718
declare_clippy_lint! {
1819
/// ### What it does
@@ -93,7 +94,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustive {
9394
.then_some((v.def_id, v.span))
9495
});
9596
if let Ok((id, span)) = iter.exactly_one()
96-
&& !attr::contains_name(cx.tcx.hir_attrs(item.hir_id()), sym::non_exhaustive)
97+
&& !find_attr!(cx.tcx.hir_attrs(item.hir_id()), AttributeKind::NonExhaustive(..))
9798
{
9899
self.potential_enums.push((item.owner_id.def_id, id, item.span, span));
99100
}
@@ -113,10 +114,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustive {
113114
item.span,
114115
"this seems like a manual implementation of the non-exhaustive pattern",
115116
|diag| {
116-
if let Some(non_exhaustive) =
117-
attr::find_by_name(cx.tcx.hir_attrs(item.hir_id()), sym::non_exhaustive)
117+
if let Some(non_exhaustive_span) =
118+
find_attr!(cx.tcx.hir_attrs(item.hir_id()), AttributeKind::NonExhaustive(span) => *span)
118119
{
119-
diag.span_note(non_exhaustive.span(), "the struct is already non-exhaustive");
120+
diag.span_note(non_exhaustive_span, "the struct is already non-exhaustive");
120121
} else {
121122
let indent = snippet_indent(cx, item.span).unwrap_or_default();
122123
diag.span_suggestion_verbose(

clippy_utils/src/attrs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use rustc_middle::ty::{AdtDef, TyCtxt};
77
use rustc_session::Session;
88
use rustc_span::{Span, Symbol};
99
use std::str::FromStr;
10-
10+
use rustc_attr_data_structures::find_attr;
1111
use crate::source::SpanRangeExt;
1212
use crate::{sym, tokenize_with_text};
13+
use rustc_attr_data_structures::AttributeKind;
1314

1415
/// Deprecation status of attributes known by Clippy.
1516
pub enum DeprecationStatus {
@@ -165,13 +166,13 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
165166

166167
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
167168
adt.is_variant_list_non_exhaustive()
168-
|| tcx.has_attr(adt.did(), sym::non_exhaustive)
169+
|| find_attr!(tcx.get_all_attrs(adt.did()), AttributeKind::NonExhaustive(..))
169170
|| adt.variants().iter().any(|variant_def| {
170-
variant_def.is_field_list_non_exhaustive() || tcx.has_attr(variant_def.def_id, sym::non_exhaustive)
171+
variant_def.is_field_list_non_exhaustive() || find_attr!(tcx.get_all_attrs(variant_def.def_id), AttributeKind::NonExhaustive(..))
171172
})
172173
|| adt
173174
.all_fields()
174-
.any(|field_def| tcx.has_attr(field_def.did, sym::non_exhaustive))
175+
.any(|field_def| find_attr!(tcx.get_all_attrs(field_def.did), AttributeKind::NonExhaustive(..)))
175176
}
176177

177178
/// Checks if the given span contains a `#[cfg(..)]` attribute

0 commit comments

Comments
 (0)