From 10677d69015738e7c4434105c3077ea009d10980 Mon Sep 17 00:00:00 2001 From: J-ZhengLi Date: Tue, 12 Mar 2024 09:19:35 +0800 Subject: [PATCH 1/2] add `with_empty_docs` attr macro & test cases for issue #12377 --- tests/ui/auxiliary/proc_macro_attr.rs | 13 +++++++++ tests/ui/empty_docs.rs | 17 ++++++++++++ tests/ui/empty_docs.stderr | 38 ++++++++++++++++++++------- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/tests/ui/auxiliary/proc_macro_attr.rs b/tests/ui/auxiliary/proc_macro_attr.rs index b550d13b0b9..a6f3b164c9b 100644 --- a/tests/ui/auxiliary/proc_macro_attr.rs +++ b/tests/ui/auxiliary/proc_macro_attr.rs @@ -163,3 +163,16 @@ pub fn rewrite_struct(_args: TokenStream, input: TokenStream) -> TokenStream { quote!(#item_struct).into() } + +#[proc_macro_attribute] +pub fn with_empty_docs(_attr: TokenStream, input: TokenStream) -> TokenStream { + let item = parse_macro_input!(input as syn::Item); + let attrs: Vec = vec![]; + let doc_comment = ""; + quote! { + #(#attrs)* + #[doc = #doc_comment] + #item + } + .into() +} diff --git a/tests/ui/empty_docs.rs b/tests/ui/empty_docs.rs index 272fab7d5ca..00e64eebc5f 100644 --- a/tests/ui/empty_docs.rs +++ b/tests/ui/empty_docs.rs @@ -1,6 +1,9 @@ +//@aux-build:proc_macro_attr.rs + #![allow(unused)] #![warn(clippy::empty_docs)] #![allow(clippy::mixed_attributes_style)] +#![feature(extern_types)] mod outer { //! @@ -67,3 +70,17 @@ union Unite { y: i32, } } + +mod issue_12377 { + use proc_macro_attr::with_empty_docs; + + #[with_empty_docs] + extern "C" { + type Test; + } + + #[with_empty_docs] + struct Foo { + a: u8, + } +} diff --git a/tests/ui/empty_docs.stderr b/tests/ui/empty_docs.stderr index f12aead6aa7..c6618ca3232 100644 --- a/tests/ui/empty_docs.stderr +++ b/tests/ui/empty_docs.stderr @@ -1,5 +1,5 @@ error: empty doc comment - --> tests/ui/empty_docs.rs:6:5 + --> tests/ui/empty_docs.rs:9:5 | LL | //! | ^^^ @@ -9,7 +9,7 @@ LL | //! = help: to override `-D warnings` add `#[allow(clippy::empty_docs)]` error: empty doc comment - --> tests/ui/empty_docs.rs:14:5 + --> tests/ui/empty_docs.rs:17:5 | LL | /// | ^^^ @@ -17,7 +17,7 @@ LL | /// = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:16:9 + --> tests/ui/empty_docs.rs:19:9 | LL | /// | ^^^ @@ -25,7 +25,7 @@ LL | /// = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:27:5 + --> tests/ui/empty_docs.rs:30:5 | LL | #[doc = ""] | ^^^^^^^^^^^ @@ -33,7 +33,7 @@ LL | #[doc = ""] = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:30:5 + --> tests/ui/empty_docs.rs:33:5 | LL | / #[doc = ""] LL | | #[doc = ""] @@ -42,7 +42,7 @@ LL | | #[doc = ""] = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:37:5 + --> tests/ui/empty_docs.rs:40:5 | LL | /// | ^^^ @@ -50,7 +50,7 @@ LL | /// = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:50:13 + --> tests/ui/empty_docs.rs:53:13 | LL | /*! */ | ^^^^^^ @@ -58,7 +58,7 @@ LL | /*! */ = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:58:13 + --> tests/ui/empty_docs.rs:61:13 | LL | /// | ^^^ @@ -66,12 +66,30 @@ LL | /// = help: consider removing or filling it error: empty doc comment - --> tests/ui/empty_docs.rs:66:9 + --> tests/ui/empty_docs.rs:69:9 | LL | /// | ^^^ | = help: consider removing or filling it -error: aborting due to 9 previous errors +error: empty doc comment + --> tests/ui/empty_docs.rs:77:5 + | +LL | #[with_empty_docs] + | ^^^^^^^^^^^^^^^^^^ + | + = help: consider removing or filling it + = note: this error originates in the attribute macro `with_empty_docs` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: empty doc comment + --> tests/ui/empty_docs.rs:82:5 + | +LL | #[with_empty_docs] + | ^^^^^^^^^^^^^^^^^^ + | + = help: consider removing or filling it + = note: this error originates in the attribute macro `with_empty_docs` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 11 previous errors From 3cd6fd15a5b9f654b08d22949a21273d44ef64de Mon Sep 17 00:00:00 2001 From: J-ZhengLi Date: Tue, 12 Mar 2024 10:40:40 +0800 Subject: [PATCH 2/2] fix [`empty_docs`] trigger in proc-macro --- clippy_lints/src/doc/mod.rs | 13 +++++++++++-- tests/ui/empty_docs.stderr | 20 +------------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/clippy_lints/src/doc/mod.rs b/clippy_lints/src/doc/mod.rs index 003d26b7b89..b4087c43282 100644 --- a/clippy_lints/src/doc/mod.rs +++ b/clippy_lints/src/doc/mod.rs @@ -14,7 +14,7 @@ use rustc_hir as hir; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{AnonConst, Expr}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::hir::nested_filter; use rustc_middle::lint::in_external_macro; use rustc_middle::ty; @@ -538,7 +538,16 @@ fn fake_broken_link_callback<'a>(_: BrokenLink<'_>) -> Option<(CowStr<'a>, CowSt suspicious_doc_comments::check(cx, attrs); - let (fragments, _) = attrs_to_doc_fragments(attrs.iter().map(|attr| (attr, None)), true); + let (fragments, _) = attrs_to_doc_fragments( + attrs.iter().filter_map(|attr| { + if in_external_macro(cx.sess(), attr.span) { + None + } else { + Some((attr, None)) + } + }), + true, + ); let mut doc = fragments.iter().fold(String::new(), |mut acc, fragment| { add_doc_fragment(&mut acc, fragment); acc diff --git a/tests/ui/empty_docs.stderr b/tests/ui/empty_docs.stderr index c6618ca3232..28ebea22c5d 100644 --- a/tests/ui/empty_docs.stderr +++ b/tests/ui/empty_docs.stderr @@ -73,23 +73,5 @@ LL | /// | = help: consider removing or filling it -error: empty doc comment - --> tests/ui/empty_docs.rs:77:5 - | -LL | #[with_empty_docs] - | ^^^^^^^^^^^^^^^^^^ - | - = help: consider removing or filling it - = note: this error originates in the attribute macro `with_empty_docs` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: empty doc comment - --> tests/ui/empty_docs.rs:82:5 - | -LL | #[with_empty_docs] - | ^^^^^^^^^^^^^^^^^^ - | - = help: consider removing or filling it - = note: this error originates in the attribute macro `with_empty_docs` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: aborting due to 11 previous errors +error: aborting due to 9 previous errors