Skip to content

fix [empty_docs] trigger in proc-macro #12466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_data_structures::fx::FxHashSet;
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;
Expand Down Expand Up @@ -538,7 +538,16 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[

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
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/auxiliary/proc_macro_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<syn::Attribute> = vec![];
let doc_comment = "";
quote! {
#(#attrs)*
#[doc = #doc_comment]
#item
}
.into()
}
17 changes: 17 additions & 0 deletions tests/ui/empty_docs.rs
Original file line number Diff line number Diff line change
@@ -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 {
//!
Expand Down Expand Up @@ -67,3 +70,17 @@ mod outer {
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,
}
}
18 changes: 9 additions & 9 deletions tests/ui/empty_docs.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: empty doc comment
--> tests/ui/empty_docs.rs:6:5
--> tests/ui/empty_docs.rs:9:5
|
LL | //!
| ^^^
Expand All @@ -9,31 +9,31 @@ 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 | ///
| ^^^
|
= 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 | ///
| ^^^
|
= 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 = ""]
| ^^^^^^^^^^^
|
= 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 = ""]
Expand All @@ -42,31 +42,31 @@ 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 | ///
| ^^^
|
= 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 | /*! */
| ^^^^^^
|
= 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 | ///
| ^^^
|
= 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 | ///
| ^^^
Expand Down