about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-04-07 13:07:12 +0200
committerGitHub <noreply@github.com>2021-04-07 13:07:12 +0200
commit4d5bb1ca222143627674a0da99c5b3f7bd7be77d (patch)
treec502cc6e8526aa671f3cc20102a4284219781672
parent2c55bacfbf5bfdf3f38a21cbda41bd8a3439fdd6 (diff)
parent815de0e50a2bd81cae5f4cc5e424b33cd68deff9 (diff)
downloadrust-4d5bb1ca222143627674a0da99c5b3f7bd7be77d.tar.gz
rust-4d5bb1ca222143627674a0da99c5b3f7bd7be77d.zip
Rollup merge of #83816 - JohnTitor:unused-doc-comments-on-macros, r=varkor
Trigger `unused_doc_comments` on macros at once

Fixes #83768
-rw-r--r--compiler/rustc_expand/src/expand.rs14
-rw-r--r--compiler/rustc_lint/src/builtin.rs2
-rw-r--r--src/test/ui/unused/unused-doc-comments-for-macros.rs17
-rw-r--r--src/test/ui/unused/unused-doc-comments-for-macros.stderr31
-rw-r--r--src/test/ui/unused/useless-comment.rs (renamed from src/test/ui/useless-comment.rs)0
-rw-r--r--src/test/ui/unused/useless-comment.stderr (renamed from src/test/ui/useless-comment.stderr)0
6 files changed, 61 insertions, 3 deletions
diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs
index b732e449374..0f4441d020b 100644
--- a/compiler/rustc_expand/src/expand.rs
+++ b/compiler/rustc_expand/src/expand.rs
@@ -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),
diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs
index 3f16bb9f442..b9de144b0eb 100644
--- a/compiler/rustc_lint/src/builtin.rs
+++ b/compiler/rustc_lint/src/builtin.rs
@@ -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;
         }
 
diff --git a/src/test/ui/unused/unused-doc-comments-for-macros.rs b/src/test/ui/unused/unused-doc-comments-for-macros.rs
new file mode 100644
index 00000000000..05828ebb2c3
--- /dev/null
+++ b/src/test/ui/unused/unused-doc-comments-for-macros.rs
@@ -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!();
+}
diff --git a/src/test/ui/unused/unused-doc-comments-for-macros.stderr b/src/test/ui/unused/unused-doc-comments-for-macros.stderr
new file mode 100644
index 00000000000..f4f5bb71e55
--- /dev/null
+++ b/src/test/ui/unused/unused-doc-comments-for-macros.stderr
@@ -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
+
diff --git a/src/test/ui/useless-comment.rs b/src/test/ui/unused/useless-comment.rs
index 7d2e5ab6f2b..7d2e5ab6f2b 100644
--- a/src/test/ui/useless-comment.rs
+++ b/src/test/ui/unused/useless-comment.rs
diff --git a/src/test/ui/useless-comment.stderr b/src/test/ui/unused/useless-comment.stderr
index 5a0af8db7c5..5a0af8db7c5 100644
--- a/src/test/ui/useless-comment.stderr
+++ b/src/test/ui/unused/useless-comment.stderr