Skip to content

Commit cb5fcc8

Browse files
committed
Fix rustdoc and clippy
1 parent a556c9b commit cb5fcc8

22 files changed

+96
-73
lines changed

src/librustdoc/clean/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2714,13 +2714,13 @@ fn add_without_unwanted_attributes<'hir>(
27142714
import_parent: Option<DefId>,
27152715
) {
27162716
for attr in new_attrs {
2717-
if matches!(attr.kind, hir::AttrKind::DocComment(..)) {
2717+
if attr.is_doc_comment() {
27182718
attrs.push((Cow::Borrowed(attr), import_parent));
27192719
continue;
27202720
}
27212721
let mut attr = attr.clone();
2722-
match attr.kind {
2723-
hir::AttrKind::Normal(ref mut normal) => {
2722+
match attr {
2723+
hir::Attribute::Unparsed(ref mut normal) => {
27242724
if let [ident] = &*normal.path.segments {
27252725
let ident = ident.name;
27262726
if ident == sym::doc {
@@ -2732,7 +2732,11 @@ fn add_without_unwanted_attributes<'hir>(
27322732
}
27332733
}
27342734
}
2735-
_ => unreachable!(),
2735+
hir::Attribute::Parsed(..) => {
2736+
if is_inline {
2737+
attrs.push((Cow::Owned(attr), import_parent));
2738+
}
2739+
}
27362740
}
27372741
}
27382742
}

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl ExternalCrate {
268268
let attr_value = attr.value_str().expect("syntax should already be validated");
269269
let Some(prim) = PrimitiveType::from_symbol(attr_value) else {
270270
span_bug!(
271-
attr.span,
271+
attr.span(),
272272
"primitive `{attr_value}` is not a member of `PrimitiveType`"
273273
);
274274
};

src/librustdoc/doctest/rust.rs

+1-1
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/tools/clippy/clippy_lints/src/attrs/inline_always.rs

+1-1
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
}

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

+1-1
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

+1-1
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/disallowed_macros.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
use clippy_config::Conf;
23
use clippy_config::types::create_disallowed_map;
34
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
@@ -116,6 +117,7 @@ impl DisallowedMacros {
116117
}
117118
}
118119

120+
// TODO: early pass?
119121
impl_lint_pass!(DisallowedMacros => [DISALLOWED_MACROS]);
120122

121123
impl LateLintPass<'_> for DisallowedMacros {

src/tools/clippy/clippy_lints/src/doc/empty_line_after.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::{SpanRangeExt, snippet_indent};
33
use clippy_utils::tokenize_with_text;
44
use itertools::Itertools;
5-
use rustc_ast::AttrStyle;
65
use rustc_ast::token::CommentKind;
6+
use rustc_ast::AttrStyle;
77
use rustc_errors::{Applicability, Diag, SuggestionStyle};
8-
use rustc_hir::{AttrKind, Attribute, ItemKind, Node};
8+
use rustc_hir::{Attribute, ItemKind, Node};
9+
use rustc_attr_parsing::AttributeKind;
910
use rustc_lexer::TokenKind;
1011
use rustc_lint::LateContext;
1112
use rustc_span::{BytePos, ExpnKind, InnerSpan, Span, SpanData};
@@ -67,14 +68,14 @@ impl Stop {
6768
}
6869

6970
fn from_attr(cx: &LateContext<'_>, attr: &Attribute) -> Option<Self> {
70-
let SpanData { lo, hi, .. } = attr.span.data();
71+
let SpanData { lo, hi, .. } = attr.span().data();
7172
let file = cx.tcx.sess.source_map().lookup_source_file(lo);
7273

7374
Some(Self {
74-
span: attr.span,
75-
kind: match attr.kind {
76-
AttrKind::Normal(_) => StopKind::Attr,
77-
AttrKind::DocComment(comment_kind, _) => StopKind::Doc(comment_kind),
75+
span: attr.span(),
76+
kind: match attr {
77+
Attribute::Parsed(AttributeKind::DocComment{kind, ..}) => StopKind::Doc(*kind),
78+
_ => StopKind::Attr,
7879
},
7980
first: file.lookup_line(file.relative_position(lo))?,
8081
last: file.lookup_line(file.relative_position(hi))?,
@@ -300,7 +301,7 @@ fn check_gaps(cx: &LateContext<'_>, gaps: &[Gap<'_>]) -> bool {
300301
pub(super) fn check(cx: &LateContext<'_>, attrs: &[Attribute]) -> bool {
301302
let mut outer = attrs
302303
.iter()
303-
.filter(|attr| attr.style == AttrStyle::Outer && !attr.span.from_expansion())
304+
.filter(|attr| attr.style() == AttrStyle::Outer && !attr.span().from_expansion())
304305
.map(|attr| Stop::from_attr(cx, attr))
305306
.collect::<Option<Vec<_>>>()
306307
.unwrap_or_default();

src/tools/clippy/clippy_lints/src/doc/include_in_doc_without_cfg.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_opt;
3-
use rustc_ast::AttrStyle;
43
use rustc_errors::Applicability;
5-
use rustc_hir::{AttrArgs, AttrKind, Attribute};
6-
use rustc_lint::LateContext;
4+
use rustc_lint::EarlyContext;
5+
use rustc_ast::{Attribute, AttrKind, AttrArgs, AttrStyle};
76

87
use super::DOC_INCLUDE_WITHOUT_CFG;
98

10-
pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
9+
pub fn check(cx: &EarlyContext<'_>, attrs: &[Attribute]) {
1110
for attr in attrs {
1211
if !attr.span.from_expansion()
1312
&& let AttrKind::Normal(ref item) = attr.kind
1413
&& attr.doc_str().is_some()
15-
&& let AttrArgs::Eq { expr: meta, .. } = &item.args
14+
&& let AttrArgs::Eq { expr: meta, .. } = &item.item.args
1615
&& !attr.span.contains(meta.span)
1716
// Since the `include_str` is already expanded at this point, we can only take the
1817
// whole attribute snippet and then modify for our suggestion.

src/tools/clippy/clippy_lints/src/doc/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_data_structures::fx::FxHashSet;
2020
use rustc_errors::Applicability;
2121
use rustc_hir::intravisit::{self, Visitor};
2222
use rustc_hir::{AnonConst, Attribute, Expr, ImplItemKind, ItemKind, Node, Safety, TraitItemKind};
23-
use rustc_lint::{LateContext, LateLintPass, LintContext};
23+
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2424
use rustc_middle::hir::nested_filter;
2525
use rustc_middle::lint::in_external_macro;
2626
use rustc_middle::ty;
@@ -623,6 +623,13 @@ impl_lint_pass!(Documentation => [
623623
DOC_INCLUDE_WITHOUT_CFG,
624624
]);
625625

626+
627+
impl EarlyLintPass for Documentation {
628+
fn check_attributes(&mut self, cx: &EarlyContext<'_>, attrs: &[rustc_ast::Attribute]) {
629+
include_in_doc_without_cfg::check(cx, attrs);
630+
}
631+
}
632+
626633
impl<'tcx> LateLintPass<'tcx> for Documentation {
627634
fn check_attributes(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
628635
let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else {
@@ -750,14 +757,13 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
750757
Some(("fake".into(), "fake".into()))
751758
}
752759

753-
include_in_doc_without_cfg::check(cx, attrs);
754760
if suspicious_doc_comments::check(cx, attrs) || empty_line_after::check(cx, attrs) || is_doc_hidden(attrs) {
755761
return None;
756762
}
757763

758764
let (fragments, _) = attrs_to_doc_fragments(
759765
attrs.iter().filter_map(|attr| {
760-
if in_external_macro(cx.sess(), attr.span) {
766+
if in_external_macro(cx.sess(), attr.span()) {
761767
None
762768
} else {
763769
Some((attr, None))

src/tools/clippy/clippy_lints/src/doc/suspicious_doc_comments.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast::AttrStyle;
33
use rustc_ast::token::CommentKind;
44
use rustc_errors::Applicability;
55
use rustc_hir::Attribute;
6+
use rustc_attr_parsing::AttributeKind;
67
use rustc_lint::LateContext;
78
use rustc_span::Span;
89

@@ -36,15 +37,14 @@ fn collect_doc_replacements(attrs: &[Attribute]) -> Vec<(Span, String)> {
3637
attrs
3738
.iter()
3839
.filter_map(|attr| {
39-
if let Some((sym, com_kind)) = attr.doc_str_and_comment_kind()
40-
&& let AttrStyle::Outer = attr.style
41-
&& let Some(com) = sym.as_str().strip_prefix('!')
40+
if let Attribute::Parsed(AttributeKind::DocComment{ style: AttrStyle::Outer, kind, comment, ..}) = attr
41+
&& let Some(com) = comment.as_str().strip_prefix('!')
4242
{
43-
let sugg = match com_kind {
43+
let sugg = match kind {
4444
CommentKind::Line => format!("//!{com}"),
4545
CommentKind::Block => format!("/*!{com}*/"),
4646
};
47-
Some((attr.span, sugg))
47+
Some((attr.span(), sugg))
4848
} else {
4949
None
5050
}

src/tools/clippy/clippy_lints/src/doc/too_long_first_doc_paragraph.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_errors::Applicability;
22
use rustc_hir::{Attribute, Item, ItemKind};
3+
use rustc_attr_parsing::AttributeKind;
34
use rustc_lint::LateContext;
45

56
use clippy_utils::diagnostics::span_lint_and_then;
@@ -43,9 +44,9 @@ pub(super) fn check(
4344
let mut should_suggest_empty_doc = false;
4445

4546
for attr in attrs {
46-
if let Some(doc) = attr.doc_str() {
47-
spans.push(attr.span);
48-
let doc = doc.as_str();
47+
if let Attribute::Parsed(AttributeKind::DocComment {span, comment, ..}) = attr {
48+
spans.push(span);
49+
let doc = comment.as_str();
4950
let doc = doc.trim();
5051
if spans.len() == 1 {
5152
// We make this suggestion only if the first doc line ends with a punctuation
@@ -78,7 +79,7 @@ pub(super) fn check(
7879
&& let new_span = first_span.with_hi(second_span.lo()).with_lo(first_span.hi())
7980
&& let Some(snippet) = snippet_opt(cx, new_span)
8081
{
81-
let Some(first) = snippet_opt(cx, first_span) else {
82+
let Some(first) = snippet_opt(cx, *first_span) else {
8283
return;
8384
};
8485
let Some(comment_form) = first.get(..3) else {

src/tools/clippy/clippy_lints/src/four_forward_slashes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
4646
.hir()
4747
.attrs(item.hir_id())
4848
.iter()
49-
.fold(item.span.shrink_to_lo(), |span, attr| span.to(attr.span));
49+
.fold(item.span.shrink_to_lo(), |span, attr| span.to(attr.span()));
5050
let (Some(file), _, _, end_line, _) = sm.span_to_location_info(span) else {
5151
return;
5252
};

src/tools/clippy/clippy_lints/src/functions/must_use.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
9696
}
9797
}
9898

99+
// FIXME: needs to be an EARLY LINT. all attribute lints should be
99100
#[allow(clippy::too_many_arguments)]
100101
fn check_needless_must_use(
101102
cx: &LateContext<'_>,
@@ -118,7 +119,7 @@ fn check_needless_must_use(
118119
fn_header_span,
119120
"this unit-returning function has a `#[must_use]` attribute",
120121
|diag| {
121-
diag.span_suggestion(attr.span, "remove the attribute", "", Applicability::MachineApplicable);
122+
diag.span_suggestion(attr.span(), "remove the attribute", "", Applicability::MachineApplicable);
122123
},
123124
);
124125
} else {
@@ -131,7 +132,7 @@ fn check_needless_must_use(
131132
"this unit-returning function has a `#[must_use]` attribute",
132133
|diag| {
133134
let mut attrs_without_must_use = attrs.to_vec();
134-
attrs_without_must_use.retain(|a| a.id != attr.id);
135+
attrs_without_must_use.retain(|a| a.id() != attr.id());
135136
let sugg_str = attrs_without_must_use
136137
.iter()
137138
.map(|a| {
@@ -144,7 +145,7 @@ fn check_needless_must_use(
144145
.join(", ");
145146

146147
diag.span_suggestion(
147-
attrs[0].span.with_hi(attrs[attrs.len() - 1].span.hi()),
148+
attrs[0].span().with_hi(attrs[attrs.len() - 1].span().hi()),
148149
"change these attributes to",
149150
sugg_str,
150151
Applicability::MachineApplicable,

src/tools/clippy/clippy_lints/src/inconsistent_struct_constructor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn suggestion<'tcx>(
183183

184184
fn field_with_attrs_span(tcx: TyCtxt<'_>, field: &hir::ExprField<'_>) -> Span {
185185
if let Some(attr) = tcx.hir().attrs(field.hir_id).first() {
186-
field.span.with_lo(attr.span.lo())
186+
field.span.with_lo(attr.span().lo())
187187
} else {
188188
field.span
189189
}

src/tools/clippy/clippy_lints/src/inline_fn_without_body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
4242
span_lint_and_then(
4343
cx,
4444
INLINE_FN_WITHOUT_BODY,
45-
attr.span,
45+
attr.span(),
4646
format!("use of `#[inline]` on trait method `{}` which has no body", item.ident),
4747
|diag| {
48-
diag.suggest_remove_item(cx, attr.span, "remove", Applicability::MachineApplicable);
48+
diag.suggest_remove_item(cx, attr.span(), "remove", Applicability::MachineApplicable);
4949
},
5050
);
5151
}

0 commit comments

Comments
 (0)