Skip to content

Commit f321f10

Browse files
committed
Fix rustdoc and clippy
1 parent 7e0f5b5 commit f321f10

31 files changed

+134
-121
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ impl AttributeExt for Attribute {
11451145
}
11461146

11471147
fn is_doc_comment(&self) -> bool {
1148+
// FIXME(jdonszelmann): make the 2nd check unnecessary here
11481149
matches!(self, Attribute::Parsed(AttributeKind::DocComment { .. }))
11491150
}
11501151

src/librustdoc/clean/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,13 +2737,13 @@ fn add_without_unwanted_attributes<'hir>(
27372737
import_parent: Option<DefId>,
27382738
) {
27392739
for attr in new_attrs {
2740-
if matches!(attr.kind, hir::AttrKind::DocComment(..)) {
2740+
if attr.is_doc_comment() {
27412741
attrs.push((Cow::Borrowed(attr), import_parent));
27422742
continue;
27432743
}
27442744
let mut attr = attr.clone();
2745-
match attr.kind {
2746-
hir::AttrKind::Normal(ref mut normal) => {
2745+
match attr {
2746+
hir::Attribute::Unparsed(ref mut normal) => {
27472747
if let [ident] = &*normal.path.segments {
27482748
let ident = ident.name;
27492749
if ident == sym::doc {
@@ -2755,7 +2755,11 @@ fn add_without_unwanted_attributes<'hir>(
27552755
}
27562756
}
27572757
}
2758-
_ => unreachable!(),
2758+
hir::Attribute::Parsed(..) => {
2759+
if is_inline {
2760+
attrs.push((Cow::Owned(attr), import_parent));
2761+
}
2762+
}
27592763
}
27602764
}
27612765
}

src/librustdoc/clean/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl ExternalCrate {
265265
let attr_value = attr.value_str().expect("syntax should already be validated");
266266
let Some(prim) = PrimitiveType::from_symbol(attr_value) else {
267267
span_bug!(
268-
attr.span,
268+
attr.span(),
269269
"primitive `{attr_value}` is not a member of `PrimitiveType`"
270270
);
271271
};

src/librustdoc/doctest/rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl HirCollector<'_> {
123123
.iter()
124124
.find(|attr| attr.doc_str().is_some())
125125
.map(|attr| {
126-
attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span)
126+
attr.span().ctxt().outer_expn().expansion_cause().unwrap_or(attr.span())
127127
})
128128
.unwrap_or(DUMMY_SP)
129129
};

src/rustdoc-json-types/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3030
/// This integer is incremented with every breaking change to the API,
3131
/// and is returned along with the JSON blob as [`Crate::format_version`].
3232
/// Consuming code should assert that this value matches the format version(s) that it supports.
33-
pub const FORMAT_VERSION: u32 = 39;
33+
pub const FORMAT_VERSION: u32 = 40;
3434

3535
/// The root of the emitted JSON blob.
3636
///
@@ -120,7 +120,9 @@ pub struct Item {
120120
pub docs: Option<String>,
121121
/// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs
122122
pub links: HashMap<String, Id>,
123-
/// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
123+
/// Stringified versions of parsed attributes on this item.
124+
/// Essentially debug printed (e.g. `#[inline]` becomes something similar to `#[attr="Inline(Hint)"]`).
125+
/// Equivalent to the hir pretty-printing of attributes.
124126
pub attrs: Vec<String>,
125127
/// Information about the item’s deprecation, if present.
126128
pub deprecation: Option<Deprecation>,

src/tools/clippy/clippy_lints/src/attrs/inline_always.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Att
2020
span_lint(
2121
cx,
2222
INLINE_ALWAYS,
23-
attr.span,
23+
attr.span(),
2424
format!("you have declared `#[inline(always)]` on `{name}`. This is usually a bad idea"),
2525
);
2626
}
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use rustc_attr_parsing::{find_attr, AttributeKind, ReprAttr};
12
use rustc_hir::Attribute;
23
use rustc_lint::LateContext;
3-
use rustc_span::{Span, sym};
4+
use rustc_span::Span;
45

56
use clippy_utils::diagnostics::span_lint_and_then;
67
use clippy_utils::msrvs;
@@ -14,30 +15,21 @@ pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute],
1415
}
1516

1617
fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) {
17-
if let Some(items) = attrs.iter().find_map(|attr| {
18-
if attr.ident().is_some_and(|ident| matches!(ident.name, sym::repr)) {
19-
attr.meta_item_list()
20-
} else {
21-
None
18+
if let Some(reprs) = find_attr!(attrs, AttributeKind::Repr(r) => r) {
19+
let packed_span = reprs.iter().find(|(r, _)| matches!(r, ReprAttr::ReprPacked(..))).map(|(_, s)| *s);
20+
21+
if let Some(packed_span) = packed_span && !reprs.iter().any(|(x, _)| *x == ReprAttr::ReprC || *x == ReprAttr::ReprRust) {
22+
span_lint_and_then(
23+
cx,
24+
REPR_PACKED_WITHOUT_ABI,
25+
item_span,
26+
"item uses `packed` representation without ABI-qualification",
27+
|diag| {
28+
diag.warn("unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI")
29+
.help("qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`")
30+
.span_label(packed_span, "`packed` representation set here");
31+
},
32+
);
2233
}
23-
}) && let Some(packed) = items
24-
.iter()
25-
.find(|item| item.ident().is_some_and(|ident| matches!(ident.name, sym::packed)))
26-
&& !items.iter().any(|item| {
27-
item.ident()
28-
.is_some_and(|ident| matches!(ident.name, sym::C | sym::Rust))
29-
})
30-
{
31-
span_lint_and_then(
32-
cx,
33-
REPR_PACKED_WITHOUT_ABI,
34-
item_span,
35-
"item uses `packed` representation without ABI-qualification",
36-
|diag| {
37-
diag.warn("unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI")
38-
.help("qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`")
39-
.span_label(packed.span(), "`packed` representation set here");
40-
},
41-
);
4234
}
4335
}

src/tools/clippy/clippy_lints/src/attrs/unnecessary_clippy_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check(
1515
) {
1616
if cfg_attr.has_name(sym::clippy)
1717
&& let Some(ident) = behind_cfg_attr.ident()
18-
&& Level::from_symbol(ident.name, Some(attr.id)).is_some()
18+
&& Level::from_symbol(ident.name, || Some(attr.id)).is_some()
1919
&& let Some(items) = behind_cfg_attr.meta_item_list()
2020
{
2121
let nb_items = items.len();

src/tools/clippy/clippy_lints/src/attrs/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(super) fn is_word(nmi: &MetaItemInner, expected: Symbol) -> bool {
1717
}
1818

1919
pub(super) fn is_lint_level(symbol: Symbol, attr_id: AttrId) -> bool {
20-
Level::from_symbol(symbol, Some(attr_id)).is_some()
20+
Level::from_symbol(symbol, || Some(attr_id)).is_some()
2121
}
2222

2323
pub(super) fn is_relevant_item(cx: &LateContext<'_>, item: &Item<'_>) -> bool {

src/tools/clippy/clippy_lints/src/default_union_representation.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use rustc_attr_parsing::{find_attr, AttributeKind, ReprAttr};
23
use rustc_hir::{HirId, Item, ItemKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_middle::ty::layout::LayoutOf;
56
use rustc_middle::ty::{self, FieldDef};
67
use rustc_session::declare_lint_pass;
7-
use rustc_span::sym;
88

99
declare_clippy_lint! {
1010
/// ### What it does
@@ -97,16 +97,7 @@ fn is_zst<'tcx>(cx: &LateContext<'tcx>, field: &FieldDef, args: ty::GenericArgsR
9797
}
9898

9999
fn has_c_repr_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool {
100-
cx.tcx.hir().attrs(hir_id).iter().any(|attr| {
101-
if attr.has_name(sym::repr) {
102-
if let Some(items) = attr.meta_item_list() {
103-
for item in items {
104-
if item.is_word() && matches!(item.name_or_empty(), sym::C) {
105-
return true;
106-
}
107-
}
108-
}
109-
}
110-
false
111-
})
100+
let attrs = cx.tcx.hir().attrs(hir_id);
101+
102+
find_attr!(attrs, AttributeKind::Repr(r) if r.iter().any(|(x, _)| *x == ReprAttr::ReprC))
112103
}

0 commit comments

Comments
 (0)