Skip to content

Commit 6451a52

Browse files
Wip
1 parent 45449b4 commit 6451a52

File tree

7 files changed

+31
-33
lines changed

7 files changed

+31
-33
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg_trace.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_feature::AttributeTemplate;
22
use rustc_hir::attrs::{AttributeKind, CfgEntry};
3-
use rustc_span::{Symbol, sym};
3+
use rustc_span::{Symbol, sym, Span};
44

55
use crate::attributes::{CombineAttributeParser, ConvertFn};
66
use crate::context::{AcceptContext, Stage};
@@ -12,8 +12,8 @@ pub(crate) struct CfgTraceParser;
1212

1313
impl<S: Stage> CombineAttributeParser<S> for CfgTraceParser {
1414
const PATH: &[Symbol] = &[sym::cfg_trace];
15-
type Item = CfgEntry;
16-
const CONVERT: ConvertFn<Self::Item> = AttributeKind::CfgTrace;
15+
type Item = (CfgEntry, Span);
16+
const CONVERT: ConvertFn<Self::Item> = |c, _| AttributeKind::CfgTrace(c);
1717
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
1818
const TEMPLATE: AttributeTemplate = CFG_TEMPLATE;
1919

@@ -28,7 +28,7 @@ impl<S: Stage> CombineAttributeParser<S> for CfgTraceParser {
2828
return None;
2929
};
3030

31-
parse_cfg_entry(cx, entry).ok()
31+
Some((parse_cfg_entry(cx, entry).ok()?, cx.attr_span))
3232
}
3333
}
3434

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ pub enum AttributeKind {
653653

654654
/// Represents `#[cfg]` trace attributes
655655
/// These are attributes left behind in the HIR after `#[cfg]` attributes are processed
656-
CfgTrace(ThinVec<CfgEntry>, Span),
656+
CfgTrace(ThinVec<(CfgEntry, Span)>),
657657

658658
/// Represents `#[rustc_coinductive]`.
659659
Coinductive(Span),

src/librustdoc/clean/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,9 @@ pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute>
739739
}
740740
continue;
741741
} else if !cfg_info.parent_is_doc_cfg
742-
&& let hir::Attribute::Parsed(AttributeKind::CfgTrace(cfgs, _)) = attr
742+
&& let hir::Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) = attr
743743
{
744-
for new_cfg in cfgs {
744+
for (new_cfg, _) in cfgs {
745745
cfg_info.current_cfg &= Cfg(new_cfg.clone());
746746
}
747747
}

src/tools/clippy/clippy_lints/src/incompatible_msrv.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_middle::ty::{self, TyCtxt};
99
use rustc_session::impl_lint_pass;
1010
use rustc_span::def_id::{CrateNum, DefId};
1111
use rustc_span::{ExpnKind, Span};
12+
use rustc_hir::attrs::AttributeKind;
13+
use rustc_hir::find_attr;
1214

1315
declare_clippy_lint! {
1416
/// ### What it does
@@ -268,11 +270,6 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
268270
/// attribute.
269271
fn is_under_cfg_attribute(cx: &LateContext<'_>, hir_id: HirId) -> bool {
270272
cx.tcx.hir_parent_id_iter(hir_id).any(|id| {
271-
cx.tcx.hir_attrs(id).iter().any(|attr| {
272-
matches!(
273-
attr.ident().map(|ident| ident.name),
274-
Some(sym::cfg_trace | sym::cfg_attr_trace)
275-
)
276-
})
273+
find_attr!(cx.tcx.hir_attrs(id), AttributeKind::CfgTrace(..) | AttributeKind::CfgAttrTrace(..))
277274
})
278275
}

src/tools/clippy/clippy_lints/src/methods/is_empty.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use clippy_utils::res::MaybeResPath;
55
use clippy_utils::{find_binding_init, get_parent_expr, is_inside_always_const_context};
66
use rustc_hir::{Expr, HirId};
77
use rustc_lint::{LateContext, LintContext};
8-
use rustc_span::sym;
8+
use rustc_hir::attrs::AttributeKind;
9+
use rustc_hir::find_attr;
910

1011
use super::CONST_IS_EMPTY;
1112

@@ -40,7 +41,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, receiver: &Expr<'_
4041
fn is_under_cfg(cx: &LateContext<'_>, id: HirId) -> bool {
4142
cx.tcx
4243
.hir_parent_id_iter(id)
43-
.any(|id| cx.tcx.hir_attrs(id).iter().any(|attr| attr.has_name(sym::cfg_trace)))
44+
.any(|id| find_attr!(cx.tcx.hir_attrs(id), AttributeKind::CfgTrace(..)))
4445
}
4546

4647
/// Similar to [`clippy_utils::expr_or_init`], but does not go up the chain if the initialization

src/tools/clippy/clippy_lints/src/new_without_default.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::ty::AssocKind;
1010
use rustc_session::impl_lint_pass;
1111
use rustc_span::sym;
12+
use rustc_hir::Attribute;
13+
use rustc_hir::attrs::AttributeKind;
1214

1315
declare_clippy_lint! {
1416
/// ### What it does
@@ -121,7 +123,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
121123
let attrs_sugg = {
122124
let mut sugg = String::new();
123125
for attr in cx.tcx.hir_attrs(assoc_item_hir_id) {
124-
if !attr.has_name(sym::cfg_trace) {
126+
let Attribute::Parsed(AttributeKind::CfgTrace(attrs)) = attr else {
125127
// This might be some other attribute that the `impl Default` ought to inherit.
126128
// But it could also be one of the many attributes that:
127129
// - can't be put on an impl block -- like `#[inline]`
@@ -131,10 +133,13 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
131133
// reduce the applicability
132134
app = Applicability::MaybeIncorrect;
133135
continue;
136+
};
137+
138+
for (_, attr_span) in attrs {
139+
sugg.push_str(&snippet_with_applicability(cx.sess(), *attr_span, "_", &mut app));
140+
sugg.push('\n');
134141
}
135142

136-
sugg.push_str(&snippet_with_applicability(cx.sess(), attr.span(), "_", &mut app));
137-
sugg.push('\n');
138143
}
139144
sugg
140145
};

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ use rustc_middle::ty::{
121121
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, GenericArgKind, GenericArgsRef, IntTy, Ty, TyCtxt,
122122
TypeFlags, TypeVisitableExt, UintTy, UpvarCapture,
123123
};
124+
use rustc_hir::attrs::CfgEntry;
124125
use rustc_span::hygiene::{ExpnKind, MacroKind};
125126
use rustc_span::source_map::SourceMap;
126127
use rustc_span::symbol::{Ident, Symbol, kw};
@@ -2401,17 +2402,12 @@ pub fn is_test_function(tcx: TyCtxt<'_>, fn_def_id: LocalDefId) -> bool {
24012402
/// This only checks directly applied attributes, to see if a node is inside a `#[cfg(test)]` parent
24022403
/// use [`is_in_cfg_test`]
24032404
pub fn is_cfg_test(tcx: TyCtxt<'_>, id: HirId) -> bool {
2404-
tcx.hir_attrs(id).iter().any(|attr| {
2405-
if attr.has_name(sym::cfg_trace)
2406-
&& let Some(items) = attr.meta_item_list()
2407-
&& let [item] = &*items
2408-
&& item.has_name(sym::test)
2409-
{
2410-
true
2411-
} else {
2412-
false
2413-
}
2414-
})
2405+
if let Some(cfgs) = find_attr!(tcx.hir_attrs(id), AttributeKind::CfgTrace(cfgs) => cfgs)
2406+
&& cfgs.iter().any(|(cfg, _)| { matches!(cfg, CfgEntry::NameValue { name: sym::test, ..})}) {
2407+
true
2408+
} else {
2409+
false
2410+
}
24152411
}
24162412

24172413
/// Checks if any parent node of `HirId` has `#[cfg(test)]` attribute applied
@@ -2426,11 +2422,10 @@ pub fn is_in_test(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
24262422

24272423
/// Checks if the item of any of its parents has `#[cfg(...)]` attribute applied.
24282424
pub fn inherits_cfg(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2429-
tcx.has_attr(def_id, sym::cfg_trace)
2430-
|| tcx
2425+
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::CfgTrace(..))
2426+
|| find_attr!(tcx
24312427
.hir_parent_iter(tcx.local_def_id_to_hir_id(def_id))
2432-
.flat_map(|(parent_id, _)| tcx.hir_attrs(parent_id))
2433-
.any(|attr| attr.has_name(sym::cfg_trace))
2428+
.flat_map(|(parent_id, _)| tcx.hir_attrs(parent_id)), AttributeKind::CfgTrace(..))
24342429
}
24352430

24362431
/// Walks up the HIR tree from the given expression in an attempt to find where the value is

0 commit comments

Comments
 (0)