Skip to content

Commit 1bf28f7

Browse files
committed
chore: Apply some suggestions by -Dclippy::pedantic
`-Dclippy::pedantic` is not added to test-lang-rust-clippy.yml because not all sugestions could be applied at the moment. Added `-Dclippy::cargo` to CI. `multiple_crate_versions` is needed because at the moment there are two versions of `heck` and `hashbrown` coming as transitive dependencies
1 parent 1452bf7 commit 1bf28f7

File tree

13 files changed

+144
-66
lines changed

13 files changed

+144
-66
lines changed

.github/workflows/test-lang-rust-clippy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
with:
4747
toolchain: ${{ matrix.rust }}
4848
components: clippy
49-
- run: cargo clippy --all-features --all-targets
49+
- run: cargo clippy --all-features --all-targets --workspace -- -Dclippy::cargo -Aclippy::multiple_crate_versions

avro/src/serde/ser_schema/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,15 +1645,13 @@ impl<'s, W: Write> SchemaAwareWriteSerializer<'s, W> {
16451645
Schema::Union(union_schema) => {
16461646
for (i, variant_schema) in union_schema.schemas.iter().enumerate() {
16471647
match variant_schema {
1648-
Schema::Record(inner) => {
1649-
if inner.fields.len() == len {
1650-
encode_int(i as i32, &mut *self.writer)?;
1651-
return self.serialize_tuple_struct_with_schema(
1652-
name,
1653-
len,
1654-
variant_schema,
1655-
);
1656-
}
1648+
Schema::Record(inner) if inner.fields.len() == len => {
1649+
encode_int(i as i32, &mut *self.writer)?;
1650+
return self.serialize_tuple_struct_with_schema(
1651+
name,
1652+
len,
1653+
variant_schema,
1654+
);
16571655
}
16581656
Schema::Array(_) | Schema::Ref { name: _ } => {
16591657
encode_int(i as i32, &mut *self.writer)?;

avro/tests/append_to_existing.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ fn avro_3630_append_to_an_existing_file() -> TestResult {
4646
let new_bytes = writer.into_inner().expect("Cannot get the new bytes");
4747

4848
let reader = Reader::new(&*new_bytes).expect("Cannot read the new bytes");
49-
let mut i = 1;
50-
for value in reader {
49+
for (i, value) in (1..).zip(reader) {
5150
check(&value, i);
52-
i += 1
5351
}
5452

5553
Ok(())

avro_derive/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Set the `nightly` cfg value on nightly toolchains.
1919
//!
2020
//! We would prefer to just do `#![rustversion::attr(nightly, feature(proc_macro_diagnostic)]`
21-
//! but that's currently not possible, see <https://github.com/dtolnay/rustversion/issues/8>
21+
//! but that's currently not possible, see <https://github.com/dtolnay/rustversion/issues/8>.
2222
2323
#[rustversion::nightly]
2424
fn main() {

avro_derive/src/attributes/avro.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ impl ContainerAttributes {
6666
span,
6767
r#"`#[avro(name = "...")]` is deprecated."#,
6868
r#"Use `#[serde(rename = "...")]` instead."#,
69-
)
69+
);
7070
}
7171
if self.rename_all != RenameRule::None {
7272
super::warn(
7373
span,
7474
r#"`#[avro(rename_all = "..")]` is deprecated"#,
7575
r#"Use `#[serde(rename_all = "..")]` instead"#,
76-
)
76+
);
7777
}
7878
}
7979
}
@@ -98,7 +98,7 @@ impl VariantAttributes {
9898
span,
9999
r#"`#[avro(rename = "..")]` is deprecated"#,
100100
r#"Use `#[serde(rename = "..")]` instead"#,
101-
)
101+
);
102102
}
103103
}
104104
}
@@ -177,28 +177,28 @@ impl FieldAttributes {
177177
span,
178178
r#"`#[avro(alias = "..")]` is deprecated"#,
179179
r#"Use `#[serde(alias = "..")]` instead"#,
180-
)
180+
);
181181
}
182182
if self.rename.is_some() {
183183
super::warn(
184184
span,
185185
r#"`#[avro(rename = "..")]` is deprecated"#,
186186
r#"Use `#[serde(rename = "..")]` instead"#,
187-
)
187+
);
188188
}
189189
if self.skip {
190190
super::warn(
191191
span,
192192
"`#[avro(skip)]` is deprecated",
193193
"Use `#[serde(skip)]` instead",
194-
)
194+
);
195195
}
196196
if self.flatten {
197197
super::warn(
198198
span,
199199
"`#[avro(flatten)]` is deprecated",
200200
"Use `#[serde(flatten)]` instead",
201-
)
201+
);
202202
}
203203
}
204204
}

avro_derive/src/attributes/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub enum With {
202202
impl With {
203203
fn from_avro_and_serde(
204204
avro: &avro::With,
205-
serde: &Option<String>,
205+
serde: Option<&String>,
206206
span: Span,
207207
) -> Result<Self, syn::Error> {
208208
match &avro {
@@ -327,7 +327,7 @@ impl FieldOptions {
327327
"`#[serde(skip_serializing)]` and `#[serde(skip_serializing_if)]` are incompatible with `#[avro(default = false)]`"
328328
));
329329
}
330-
let with = match With::from_avro_and_serde(&avro.with, &serde.with, span) {
330+
let with = match With::from_avro_and_serde(&avro.with, serde.with.as_ref(), span) {
331331
Ok(with) => with,
332332
Err(error) => {
333333
errors.push(error);
@@ -389,7 +389,7 @@ fn darling_to_syn(e: darling::Error) -> Vec<syn::Error> {
389389
fn warn(span: Span, message: &str, help: &str) {
390390
proc_macro::Diagnostic::spanned(span.unwrap(), proc_macro::Level::Warning, message)
391391
.help(help)
392-
.emit()
392+
.emit();
393393
}
394394

395395
#[cfg(not(nightly))]

avro_derive/src/case.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
use darling::FromMeta;
2222
use syn::Lit;
2323

24-
use self::RenameRule::*;
24+
use self::RenameRule::{
25+
CamelCase, KebabCase, LowerCase, None, PascalCase, ScreamingKebabCase, ScreamingSnakeCase,
26+
SnakeCase, UpperCase,
27+
};
2528
use std::fmt::{self, Debug, Display};
2629

2730
/// The different possible ways to change case of fields in a struct, or variants in an enum.
@@ -111,7 +114,7 @@ impl RenameRule {
111114
pub fn apply_to_field(self, field: &str) -> String {
112115
match self {
113116
None | LowerCase | SnakeCase => field.to_owned(),
114-
UpperCase => field.to_ascii_uppercase(),
117+
UpperCase | ScreamingSnakeCase => field.to_ascii_uppercase(),
115118
PascalCase => {
116119
let mut pascal = String::new();
117120
let mut capitalize = true;
@@ -131,7 +134,6 @@ impl RenameRule {
131134
let pascal = PascalCase.apply_to_field(field);
132135
pascal[..1].to_ascii_lowercase() + &pascal[1..]
133136
}
134-
ScreamingSnakeCase => field.to_ascii_uppercase(),
135137
KebabCase => field.replace('_', "-"),
136138
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
137139
}

avro_derive/src/enums/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syn::{Attribute, DataEnum, Fields, Meta};
2424
/// Generate a schema definition for a enum.
2525
pub fn get_data_enum_schema_def(
2626
container_attrs: &NamedTypeOptions,
27-
data_enum: DataEnum,
27+
data_enum: &DataEnum,
2828
ident_span: Span,
2929
) -> Result<TokenStream, Vec<syn::Error>> {
3030
if data_enum.variants.iter().all(|v| Fields::Unit == v.fields) {

avro_derive/src/enums/plain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ use syn::{DataEnum, Fields};
2626

2727
pub fn schema_def(
2828
container_attrs: &NamedTypeOptions,
29-
data_enum: DataEnum,
29+
data_enum: &DataEnum,
3030
ident_span: Span,
3131
) -> Result<TokenStream, Vec<syn::Error>> {
3232
let doc = preserve_optional(container_attrs.doc.as_ref());
3333
let enum_aliases = aliases(&container_attrs.aliases);
3434
if data_enum.variants.iter().all(|v| Fields::Unit == v.fields) {
35-
let default_value = default_enum_variant(&data_enum, ident_span)?;
35+
let default_value = default_enum_variant(data_enum, ident_span)?;
3636
let default = preserve_optional(default_value);
3737
let mut symbols = Vec::new();
3838
for variant in &data_enum.variants {

avro_derive/src/lib.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::{
5151
pub fn proc_macro_derive_avro_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
5252
let input = parse_macro_input!(input as DeriveInput);
5353
derive_avro_schema(input)
54-
.unwrap_or_else(to_compile_errors)
54+
.unwrap_or_else(|errs| to_compile_errors(errs.as_slice()))
5555
.into()
5656
}
5757

@@ -68,16 +68,16 @@ fn derive_avro_schema(input: DeriveInput) -> Result<TokenStream, Vec<syn::Error>
6868
let (schema_def, record_fields) =
6969
get_struct_schema_def(&named_type_options, data_struct, input.ident.span())?;
7070
(
71-
handle_named_schemas(named_type_options.name, schema_def),
71+
handle_named_schemas(&named_type_options.name, &schema_def),
7272
record_fields,
7373
)
7474
};
7575
Ok(create_trait_definition(
76-
input.ident,
76+
&input.ident,
7777
&input.generics,
78-
get_schema_impl,
79-
get_record_fields_impl,
80-
named_type_options.default,
78+
&get_schema_impl,
79+
&get_record_fields_impl,
80+
&named_type_options.default,
8181
))
8282
}
8383
syn::Data::Enum(data_enum) => {
@@ -89,14 +89,14 @@ fn derive_avro_schema(input: DeriveInput) -> Result<TokenStream, Vec<syn::Error>
8989
)]);
9090
}
9191
let schema_def =
92-
get_data_enum_schema_def(&named_type_options, data_enum, input.ident.span())?;
93-
let inner = handle_named_schemas(named_type_options.name, schema_def);
92+
get_data_enum_schema_def(&named_type_options, &data_enum, input.ident.span())?;
93+
let inner = handle_named_schemas(&named_type_options.name, &schema_def);
9494
Ok(create_trait_definition(
95-
input.ident,
95+
&input.ident,
9696
&input.generics,
97-
inner,
98-
quote! { ::std::option::Option::None },
99-
named_type_options.default,
97+
&inner,
98+
&quote! { ::std::option::Option::None },
99+
&named_type_options.default,
100100
))
101101
}
102102
syn::Data::Union(_) => Err(vec![syn::Error::new(
@@ -108,11 +108,11 @@ fn derive_avro_schema(input: DeriveInput) -> Result<TokenStream, Vec<syn::Error>
108108

109109
/// Generate the trait definition with the correct generics
110110
fn create_trait_definition(
111-
ident: Ident,
111+
ident: &Ident,
112112
generics: &Generics,
113-
get_schema_impl: TokenStream,
114-
get_record_fields_impl: TokenStream,
115-
field_default_impl: TokenStream,
113+
get_schema_impl: &TokenStream,
114+
get_record_fields_impl: &TokenStream,
115+
field_default_impl: &TokenStream,
116116
) -> TokenStream {
117117
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
118118
quote! {
@@ -134,7 +134,7 @@ fn create_trait_definition(
134134
}
135135

136136
/// Generate the code to check `named_schemas` if this schema already exist
137-
fn handle_named_schemas(full_schema_name: String, schema_def: TokenStream) -> TokenStream {
137+
fn handle_named_schemas(full_schema_name: &str, schema_def: &TokenStream) -> TokenStream {
138138
quote! {
139139
let name = ::apache_avro::schema::Name::new_with_enclosing_namespace(#full_schema_name, enclosing_namespace).expect(concat!("Unable to parse schema name ", #full_schema_name));
140140
if named_schemas.contains(&name) {
@@ -448,15 +448,16 @@ fn type_to_field_default_expr(ty: &Type) -> Result<TokenStream, Vec<syn::Error>>
448448
}
449449

450450
/// Stolen from serde
451-
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
451+
fn to_compile_errors(errors: &[syn::Error]) -> proc_macro2::TokenStream {
452452
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
453453
quote!(#(#compile_errors)*)
454454
}
455455

456456
fn preserve_optional(op: Option<impl quote::ToTokens>) -> TokenStream {
457-
match op {
458-
Some(tt) => quote! {::std::option::Option::Some(#tt.into())},
459-
None => quote! {::std::option::Option::None},
457+
if let Some(tt) = op {
458+
quote! {::std::option::Option::Some(#tt.into())}
459+
} else {
460+
quote! {::std::option::Option::None}
460461
}
461462
}
462463

0 commit comments

Comments
 (0)