Skip to content

Commit

Permalink
Rollup merge of rust-lang#83816 - JohnTitor:unused-doc-comments-on-ma…
Browse files Browse the repository at this point in the history
…cros, r=varkor

Trigger `unused_doc_comments` on macros at once

Fixes rust-lang#83768
  • Loading branch information
Dylan-DPC authored Apr 7, 2021
2 parents a13a520 + 815de0e commit 2bb0f49
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
14 changes: 12 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,13 +1067,23 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
// since they will not be detected after macro expansion.
fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
let features = self.cx.ecfg.features.unwrap();
for attr in attrs.iter() {
let mut attrs = attrs.iter().peekable();
let mut span: Option<Span> = None;
while let Some(attr) = attrs.next() {
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
validate_attr::check_meta(&self.cx.sess.parse_sess, attr);

let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
span = Some(current_span);

if attrs.peek().map_or(false, |next_attr| next_attr.doc_str().is_some()) {
continue;
}

if attr.doc_str().is_some() {
self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
&UNUSED_DOC_COMMENTS,
attr.span,
current_span,
ast::CRATE_NODE_ID,
"unused doc comment",
BuiltinLintDiagnostics::UnusedDocComment(attr.span),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ fn warn_if_doc(cx: &EarlyContext<'_>, node_span: Span, node_kind: &str, attrs: &
Some(sugared_span.map_or(attr.span, |span| span.with_hi(attr.span.hi())));
}

if attrs.peek().map(|next_attr| next_attr.is_doc_comment()).unwrap_or_default() {
if attrs.peek().map_or(false, |next_attr| next_attr.is_doc_comment()) {
continue;
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/unused/unused-doc-comments-for-macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![deny(unused_doc_comments)]
#![feature(rustc_attrs)]

macro_rules! foo { () => {}; }

fn main() {
/// line1 //~ ERROR: unused doc comment
/// line2
/// line3
foo!();

// Ensure we still detect another doc-comment block.
/// line1 //~ ERROR: unused doc comment
/// line2
/// line3
foo!();
}
31 changes: 31 additions & 0 deletions src/test/ui/unused/unused-doc-comments-for-macros.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: unused doc comment
--> $DIR/unused-doc-comments-for-macros.rs:7:5
|
LL | / /// line1
LL | | /// line2
LL | | /// line3
| |_____--------^
| |
| rustdoc does not generate documentation for macro invocations
|
note: the lint level is defined here
--> $DIR/unused-doc-comments-for-macros.rs:1:9
|
LL | #![deny(unused_doc_comments)]
| ^^^^^^^^^^^^^^^^^^^
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion

error: unused doc comment
--> $DIR/unused-doc-comments-for-macros.rs:13:5
|
LL | / /// line1
LL | | /// line2
LL | | /// line3
| |_____--------^
| |
| rustdoc does not generate documentation for macro invocations
|
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion

error: aborting due to 2 previous errors

File renamed without changes.
File renamed without changes.

0 comments on commit 2bb0f49

Please sign in to comment.