Skip to content

Commit 24af68f

Browse files
authored
Unrolled build for #147676
Rollup merge of #147676 - jdonszelmann:span-is-doc-comment, r=GuillaumeGomez Return spans out of `is_doc_comment` to reduce reliance on `.span()` on attributes r? `@GuillaumeGomez`
2 parents 57ef8d6 + 3941b42 commit 24af68f

File tree

10 files changed

+22
-17
lines changed

10 files changed

+22
-17
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ impl AttributeExt for Attribute {
8686
/// Returns `true` if it is a sugared doc comment (`///` or `//!` for example).
8787
/// So `#[doc = "doc"]` (which is a doc comment) and `#[doc(...)]` (which is not
8888
/// a doc comment) will return `false`.
89-
fn is_doc_comment(&self) -> bool {
89+
fn is_doc_comment(&self) -> Option<Span> {
9090
match self.kind {
91-
AttrKind::Normal(..) => false,
92-
AttrKind::DocComment(..) => true,
91+
AttrKind::Normal(..) => None,
92+
AttrKind::DocComment(..) => Some(self.span),
9393
}
9494
}
9595

@@ -776,7 +776,7 @@ pub trait AttributeExt: Debug {
776776
/// Returns `true` if it is a sugared doc comment (`///` or `//!` for example).
777777
/// So `#[doc = "doc"]` (which is a doc comment) and `#[doc(...)]` (which is not
778778
/// a doc comment) will return `false`.
779-
fn is_doc_comment(&self) -> bool;
779+
fn is_doc_comment(&self) -> Option<Span>;
780780

781781
#[inline]
782782
fn has_name(&self, name: Symbol) -> bool {
@@ -863,8 +863,9 @@ impl Attribute {
863863
AttributeExt::path_matches(self, name)
864864
}
865865

866+
// on ast attributes we return a bool since that's what most code already expects
866867
pub fn is_doc_comment(&self) -> bool {
867-
AttributeExt::is_doc_comment(self)
868+
AttributeExt::is_doc_comment(self).is_some()
868869
}
869870

870871
#[inline]

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ pub fn parse_version(s: Symbol) -> Option<RustcVersion> {
2828
}
2929

3030
pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool {
31-
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
31+
attr.is_doc_comment().is_some()
32+
|| attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
3233
}
3334

3435
pub fn is_doc_alias_attrs_contain_symbol<'tcx, T: AttributeExt + 'tcx>(

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,12 @@ impl AttributeExt for Attribute {
13041304
}
13051305

13061306
#[inline]
1307-
fn is_doc_comment(&self) -> bool {
1308-
matches!(self, Attribute::Parsed(AttributeKind::DocComment { .. }))
1307+
fn is_doc_comment(&self) -> Option<Span> {
1308+
if let Attribute::Parsed(AttributeKind::DocComment { span, .. }) = self {
1309+
Some(*span)
1310+
} else {
1311+
None
1312+
}
13091313
}
13101314

13111315
#[inline]
@@ -1423,7 +1427,7 @@ impl Attribute {
14231427
}
14241428

14251429
#[inline]
1426-
pub fn is_doc_comment(&self) -> bool {
1430+
pub fn is_doc_comment(&self) -> Option<Span> {
14271431
AttributeExt::is_doc_comment(self)
14281432
}
14291433

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ pub struct MissingDoc;
393393
impl_lint_pass!(MissingDoc => [MISSING_DOCS]);
394394

395395
fn has_doc(attr: &hir::Attribute) -> bool {
396-
if attr.is_doc_comment() {
396+
if attr.is_doc_comment().is_some() {
397397
return true;
398398
}
399399

compiler/rustc_query_system/src/ich/impls_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for [hir::Attribute] {
2222
let filtered: SmallVec<[&hir::Attribute; 8]> = self
2323
.iter()
2424
.filter(|attr| {
25-
!attr.is_doc_comment()
25+
attr.is_doc_comment().is_none()
2626
// FIXME(jdonszelmann) have a better way to handle ignored attrs
2727
&& !attr.ident().is_some_and(|ident| hcx.is_ignored_attr(ident.name))
2828
})

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ pub fn attrs_to_doc_fragments<'a, A: AttributeExt + Clone + 'a>(
216216
for (attr, item_id) in attrs {
217217
if let Some((doc_str, comment_kind)) = attr.doc_str_and_comment_kind() {
218218
let doc = beautify_doc_string(doc_str, comment_kind);
219-
let (span, kind, from_expansion) = if attr.is_doc_comment() {
220-
let span = attr.span();
219+
let (span, kind, from_expansion) = if let Some(span) = attr.is_doc_comment() {
221220
(span, DocFragmentKind::SugaredDoc, span.from_expansion())
222221
} else {
223222
let attr_span = attr.span();

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2719,7 +2719,7 @@ fn add_without_unwanted_attributes<'hir>(
27192719
import_parent: Option<DefId>,
27202720
) {
27212721
for attr in new_attrs {
2722-
if attr.is_doc_comment() {
2722+
if attr.is_doc_comment().is_some() {
27232723
attrs.push((Cow::Borrowed(attr), import_parent));
27242724
continue;
27252725
}

src/librustdoc/passes/propagate_doc_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn filter_non_cfg_tokens_from_list(args_tokens: &TokenStream) -> Vec<TokenTree>
6565
/// it and put them into `attrs`.
6666
fn add_only_cfg_attributes(attrs: &mut Vec<Attribute>, new_attrs: &[Attribute]) {
6767
for attr in new_attrs {
68-
if attr.is_doc_comment() {
68+
if attr.is_doc_comment().is_some() {
6969
continue;
7070
}
7171
let mut attr = attr.clone();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
4747
.tcx
4848
.hir_attrs(item.hir_id())
4949
.iter()
50-
.filter(|i| i.is_doc_comment())
50+
.filter(|i| i.is_doc_comment().is_some())
5151
.fold(item.span.shrink_to_lo(), |span, attr| span.to(attr.span()));
5252
let (Some(file), _, _, end_line, _) = sm.span_to_location_info(span) else {
5353
return;

src/tools/clippy/clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
475475

476476
fn include_attrs_in_span(cx: &LateContext<'_>, hir_id: HirId, span: Span) -> Span {
477477
span.to(cx.tcx.hir_attrs(hir_id).iter().fold(span, |acc, attr| {
478-
if attr.is_doc_comment() {
478+
if attr.is_doc_comment().is_some() {
479479
return acc;
480480
}
481481
acc.to(attr.span())

0 commit comments

Comments
 (0)