Skip to content

Commit 4d5bb1c

Browse files
authored
Rollup merge of #83816 - JohnTitor:unused-doc-comments-on-macros, r=varkor
Trigger `unused_doc_comments` on macros at once Fixes #83768
2 parents 2c55bac + 815de0e commit 4d5bb1c

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

compiler/rustc_expand/src/expand.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1067,13 +1067,23 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10671067
// since they will not be detected after macro expansion.
10681068
fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
10691069
let features = self.cx.ecfg.features.unwrap();
1070-
for attr in attrs.iter() {
1070+
let mut attrs = attrs.iter().peekable();
1071+
let mut span: Option<Span> = None;
1072+
while let Some(attr) = attrs.next() {
10711073
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
10721074
validate_attr::check_meta(&self.cx.sess.parse_sess, attr);
1075+
1076+
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
1077+
span = Some(current_span);
1078+
1079+
if attrs.peek().map_or(false, |next_attr| next_attr.doc_str().is_some()) {
1080+
continue;
1081+
}
1082+
10731083
if attr.doc_str().is_some() {
10741084
self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
10751085
&UNUSED_DOC_COMMENTS,
1076-
attr.span,
1086+
current_span,
10771087
ast::CRATE_NODE_ID,
10781088
"unused doc comment",
10791089
BuiltinLintDiagnostics::UnusedDocComment(attr.span),

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ fn warn_if_doc(cx: &EarlyContext<'_>, node_span: Span, node_kind: &str, attrs: &
989989
Some(sugared_span.map_or(attr.span, |span| span.with_hi(attr.span.hi())));
990990
}
991991

992-
if attrs.peek().map(|next_attr| next_attr.is_doc_comment()).unwrap_or_default() {
992+
if attrs.peek().map_or(false, |next_attr| next_attr.is_doc_comment()) {
993993
continue;
994994
}
995995

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(unused_doc_comments)]
2+
#![feature(rustc_attrs)]
3+
4+
macro_rules! foo { () => {}; }
5+
6+
fn main() {
7+
/// line1 //~ ERROR: unused doc comment
8+
/// line2
9+
/// line3
10+
foo!();
11+
12+
// Ensure we still detect another doc-comment block.
13+
/// line1 //~ ERROR: unused doc comment
14+
/// line2
15+
/// line3
16+
foo!();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: unused doc comment
2+
--> $DIR/unused-doc-comments-for-macros.rs:7:5
3+
|
4+
LL | / /// line1
5+
LL | | /// line2
6+
LL | | /// line3
7+
| |_____--------^
8+
| |
9+
| rustdoc does not generate documentation for macro invocations
10+
|
11+
note: the lint level is defined here
12+
--> $DIR/unused-doc-comments-for-macros.rs:1:9
13+
|
14+
LL | #![deny(unused_doc_comments)]
15+
| ^^^^^^^^^^^^^^^^^^^
16+
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion
17+
18+
error: unused doc comment
19+
--> $DIR/unused-doc-comments-for-macros.rs:13:5
20+
|
21+
LL | / /// line1
22+
LL | | /// line2
23+
LL | | /// line3
24+
| |_____--------^
25+
| |
26+
| rustdoc does not generate documentation for macro invocations
27+
|
28+
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion
29+
30+
error: aborting due to 2 previous errors
31+

0 commit comments

Comments
 (0)