add rewrite_struct proc-macro and test case

This commit is contained in:
J-ZhengLi 2024-03-08 09:32:38 +08:00
parent a79db2aa51
commit 3a6cac7c06
3 changed files with 64 additions and 19 deletions

View File

@ -12,7 +12,7 @@ use syn::spanned::Spanned;
use syn::token::Star;
use syn::{
parse_macro_input, parse_quote, FnArg, ImplItem, ItemFn, ItemImpl, ItemTrait, Lifetime, Pat, PatIdent, PatType,
Signature, TraitItem, Type,
Signature, TraitItem, Type, ItemStruct, Visibility,
};
#[proc_macro_attribute]
@ -101,9 +101,7 @@ pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut item = parse_macro_input!(item as ItemFn);
let span = item.block.brace_token.span;
if item.sig.asyncness.is_some() {
item.sig.asyncness = None;
}
item.sig.asyncness = None;
let crate_name = quote! { fake_crate };
let block = item.block;
@ -128,7 +126,7 @@ pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut async_fn = syn::parse_macro_input!(input as syn::ItemFn);
let mut async_fn = parse_macro_input!(input as syn::ItemFn);
for stmt in &mut async_fn.block.stmts {
if let syn::Stmt::Expr(syn::Expr::Match(syn::ExprMatch { expr: scrutinee, .. }), _) = stmt {
@ -145,3 +143,23 @@ pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream
quote!(#async_fn).into()
}
#[proc_macro_attribute]
pub fn rewrite_struct(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut item_struct = parse_macro_input!(input as syn::ItemStruct);
// remove struct attributes including doc comments.
item_struct.attrs = vec![];
if let Visibility::Public(token) = item_struct.vis {
// set vis to `pub(crate)` to trigger `missing_docs_in_private_items` lint.
let new_vis: Visibility = syn::parse_quote_spanned!(token.span() => pub(crate));
item_struct.vis = new_vis;
}
if let syn::Fields::Named(fields) = &mut item_struct.fields {
for field in &mut fields.named {
// remove all attributes from fields as well.
field.attrs = vec![];
}
}
quote!(#item_struct).into()
}

View File

@ -1,5 +1,6 @@
//@needs-asm-support
//@aux-build: proc_macros.rs
//@aux-build: proc_macro_attr.rs
#![warn(clippy::missing_docs_in_private_items)]
// When denying at the crate level, be sure to not get random warnings from the
@ -8,6 +9,8 @@
//! Some garbage docs for the crate here
#![doc = "More garbage"]
#[macro_use]
extern crate proc_macro_attr;
extern crate proc_macros;
use proc_macros::with_span;
@ -112,3 +115,12 @@ with_span!(span pub enum FooPm3 { A, B(u32), C { field: u32 }});
with_span!(span pub fn foo_pm() {});
with_span!(span pub static FOO_PM: u32 = 0;);
with_span!(span pub const FOO2_PM: u32 = 0;);
// issue #12197
// Undocumented field originated inside of spanned proc-macro attribute
/// Some dox for struct.
#[rewrite_struct]
pub struct Test {
/// Dox
a: u8,
}

View File

@ -1,5 +1,5 @@
error: missing documentation for a type alias
--> tests/ui/missing_doc.rs:16:1
--> tests/ui/missing_doc.rs:19:1
|
LL | type Typedef = String;
| ^^^^^^^^^^^^^^^^^^^^^^
@ -8,19 +8,19 @@ LL | type Typedef = String;
= help: to override `-D warnings` add `#[allow(clippy::missing_docs_in_private_items)]`
error: missing documentation for a module
--> tests/ui/missing_doc.rs:19:1
--> tests/ui/missing_doc.rs:22:1
|
LL | mod module_no_dox {}
| ^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a function
--> tests/ui/missing_doc.rs:25:1
--> tests/ui/missing_doc.rs:28:1
|
LL | fn foo3() {}
| ^^^^^^^^^^^^
error: missing documentation for an enum
--> tests/ui/missing_doc.rs:39:1
--> tests/ui/missing_doc.rs:42:1
|
LL | / enum Baz {
LL | | BazA { a: isize, b: isize },
@ -29,43 +29,43 @@ LL | | }
| |_^
error: missing documentation for a variant
--> tests/ui/missing_doc.rs:40:5
--> tests/ui/missing_doc.rs:43:5
|
LL | BazA { a: isize, b: isize },
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a struct field
--> tests/ui/missing_doc.rs:40:12
--> tests/ui/missing_doc.rs:43:12
|
LL | BazA { a: isize, b: isize },
| ^^^^^^^^
error: missing documentation for a struct field
--> tests/ui/missing_doc.rs:40:22
--> tests/ui/missing_doc.rs:43:22
|
LL | BazA { a: isize, b: isize },
| ^^^^^^^^
error: missing documentation for a variant
--> tests/ui/missing_doc.rs:41:5
--> tests/ui/missing_doc.rs:44:5
|
LL | BarB,
| ^^^^
error: missing documentation for a constant
--> tests/ui/missing_doc.rs:65:1
--> tests/ui/missing_doc.rs:68:1
|
LL | const FOO: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^
error: missing documentation for a static
--> tests/ui/missing_doc.rs:74:1
--> tests/ui/missing_doc.rs:77:1
|
LL | static BAR: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a module
--> tests/ui/missing_doc.rs:83:1
--> tests/ui/missing_doc.rs:86:1
|
LL | / mod internal_impl {
LL | | /// dox
@ -77,16 +77,31 @@ LL | | }
| |_^
error: missing documentation for a function
--> tests/ui/missing_doc.rs:88:5
--> tests/ui/missing_doc.rs:91:5
|
LL | fn undocumented3() {}
| ^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a function
--> tests/ui/missing_doc.rs:94:9
--> tests/ui/missing_doc.rs:97:9
|
LL | fn also_undocumented2() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 13 previous errors
error: missing documentation for a struct
--> tests/ui/missing_doc.rs:123:1
|
LL | / pub struct Test {
LL | | /// Dox
LL | | a: u8,
LL | | }
| |_^
error: missing documentation for a struct field
--> tests/ui/missing_doc.rs:125:5
|
LL | a: u8,
| ^^^^^
error: aborting due to 15 previous errors