Skip to content

Commit 7f77790

Browse files
committed
Suppress clippy::too_many_arguments in generated code
This is generally not something users can do anything to fix. This commit suppresses it if a generated function as > 5 arguments. Fixes #2
1 parent 689239b commit 7f77790

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

src/substruct.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,18 @@ impl<'a> Emitter<'a> {
185185
let name = &substruct.ident;
186186
let (impl_generics, ty_generics, where_clause) = substruct.generics.split_for_impl();
187187

188+
let mut attrs = Vec::<syn::Attribute>::new();
188189
let method = syn::Ident::new(
189190
&format!("into_{}", self.input.ident.to_string().to_snake_case()),
190191
Span::call_site(),
191192
);
192-
let doc: syn::Attribute = syn::parse_quote!(
193+
attrs.push(syn::parse_quote!(
193194
#[doc = concat!("Convert `self` into a [`", stringify!(#original), "`].")]
194-
);
195+
));
195196

196197
let fields = match &self.input.data {
197198
syn::Data::Enum(_) => panic!("Attempted to emit conversions for an enum"),
198-
// Emitting conversions for an enum doesn't make sense
199+
// Emitting conversions for a union doesn't make sense
199200
syn::Data::Union(_) => return,
200201
// Unit structs have no fields and so they have no conversions
201202
syn::Data::Struct(data) if matches!(data.fields, syn::Fields::Unit) => return,
@@ -238,11 +239,15 @@ impl<'a> Emitter<'a> {
238239
.collect();
239240
let exc: Vec<_> = excluded.keys().collect();
240241

242+
if args.len() > 5 {
243+
attrs.push(syn::parse_quote!(#[allow(clippy::too_many_arguments)]))
244+
}
245+
241246
self.tokens.extend(quote::quote! {
242247
impl #impl_generics #name #ty_generics
243248
#where_clause
244249
{
245-
#doc
250+
#( #attrs )*
246251
pub fn #method(self, #( #args: #types, )*) -> #original #ty_generics {
247252
#original {
248253
#( #inc_dst: self.#inc_src, )*

tests/trybuild.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#[test]
22
fn trybuild() {
3+
// This still compiles the crate normally, it just means that we also get
4+
// clippy warnings as well.
5+
std::env::set_var("RUSTC_WORKSPACE_WRAPPER", "clippy-driver");
6+
37
let t = trybuild::TestCases::new();
48
t.compile_fail("tests/ui/fail/*.rs");
59
t.pass("tests/ui/pass/*.rs");

tests/ui/fail/union-invalid-field.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ error[E0609]: no field `y` on type `C`
66
|
77
help: a field with a similar name exists
88
|
9-
15 | value.x = 1;
10-
| ~
9+
15 - value.y = 1;
10+
15 + value.x = 1;
11+
|
1112

1213
error[E0609]: no field `z` on type `C`
1314
--> tests/ui/fail/union-invalid-field.rs:16:11
@@ -17,5 +18,6 @@ error[E0609]: no field `z` on type `C`
1718
|
1819
help: a field with a similar name exists
1920
|
20-
16 | value.x = 1;
21-
| ~
21+
16 - value.z = 1;
22+
16 + value.x = 1;
23+
|

tests/ui/pass/large-struct.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![deny(clippy::too_many_arguments)]
2+
#![allow(dead_code)]
3+
4+
use substruct::substruct;
5+
6+
#[substruct(A, B)]
7+
struct Large {
8+
#[substruct(B)]
9+
pub v0: u32,
10+
#[substruct(B)]
11+
pub v1: u32,
12+
#[substruct(B)]
13+
pub v2: u32,
14+
#[substruct(B)]
15+
pub v3: u32,
16+
#[substruct(B)]
17+
pub v4: u32,
18+
#[substruct(B)]
19+
pub v5: u32,
20+
#[substruct(B)]
21+
pub v6: u32,
22+
#[substruct(B)]
23+
pub v7: u32,
24+
#[substruct(B)]
25+
pub v8: u32,
26+
pub v9: u32,
27+
}
28+
29+
fn main() {}

0 commit comments

Comments
 (0)