From d7d65d1acca8243530727e8c3016d1cabd42cb4b Mon Sep 17 00:00:00 2001 From: Antonio Yang Date: Wed, 2 Oct 2024 09:27:52 +0800 Subject: [PATCH 1/3] lint without default features --- struct-patch-derive/src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/struct-patch-derive/src/lib.rs b/struct-patch-derive/src/lib.rs index 2223cbc..c274e71 100644 --- a/struct-patch-derive/src/lib.rs +++ b/struct-patch-derive/src/lib.rs @@ -32,9 +32,11 @@ struct Patch { fields: Vec, } +#[cfg(feature = "op")] enum Addable { Disable, AddTriat, + #[cfg(feature = "op")] AddFn(Ident), } @@ -43,6 +45,7 @@ struct Field { ty: Type, attributes: Vec, retyped: bool, + #[cfg(feature = "op")] addable: Addable, } @@ -400,6 +403,8 @@ impl Field { let mut attributes = vec![]; let mut field_type = None; let mut skip = false; + + #[cfg(feature = "op")] let mut addable = Addable::Disable; for attr in attrs { @@ -432,15 +437,31 @@ impl Field { let expr: LitStr = meta.value()?.parse()?; field_type = Some(expr.parse()?) } + #[cfg(feature = "op")] ADDABLE => { // #[patch(addable)] addable = Addable::AddTriat; } + #[cfg(not(feature = "op"))] + ADDABLE => { + return Err(syn::Error::new( + ident.span(), + "`addable` needs `op` feature" + )); + } + #[cfg(feature = "op")] ADD => { // #[patch(add=fn)] let f: Ident = meta.value()?.parse()?; addable = Addable::AddFn(f); } + #[cfg(not(feature = "op"))] + ADD => { + return Err(syn::Error::new( + ident.span(), + "`add` needs `op` feature" + )); + } _ => { return Err(meta.error(format_args!( "unknown patch field attribute `{}`", @@ -460,6 +481,7 @@ impl Field { retyped: field_type.is_some(), ty: field_type.unwrap_or(ty), attributes, + #[cfg(feature = "op")] addable, })) } From 55d9e5daeb32f68fbf968852d2fa712b15df48e3 Mon Sep 17 00:00:00 2001 From: Antonio Yang Date: Wed, 2 Oct 2024 09:36:23 +0800 Subject: [PATCH 2/3] lint example/op without default feature --- struct-patch/examples/op.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/struct-patch/examples/op.rs b/struct-patch/examples/op.rs index f8b216f..bc35b05 100644 --- a/struct-patch/examples/op.rs +++ b/struct-patch/examples/op.rs @@ -1,20 +1,22 @@ -use struct_patch::Patch; - +#[cfg(feature = "op")] fn str_concat(a: String, b: String) -> String { format!("{}, {}", a, b) } -#[derive(Clone, Debug, Default, Patch, PartialEq)] -#[patch(attribute(derive(Clone, Debug, Default)))] -struct Item { - field_complete: bool, - #[patch(addable)] - field_int: usize, - #[patch(add=str_concat)] - field_string: String, -} #[cfg(feature = "op")] fn main() { + use struct_patch::Patch; + + #[derive(Clone, Debug, Default, Patch, PartialEq)] + #[patch(attribute(derive(Clone, Debug, Default)))] + struct Item { + field_complete: bool, + #[patch(addable)] + field_int: usize, + #[patch(add=str_concat)] + field_string: String, + } + let mut item = Item::default(); let mut patch: ItemPatch = Item::new_empty_patch(); From f853858078f89a1e91b6f641b71f49ec826453b8 Mon Sep 17 00:00:00 2001 From: Antonio Yang Date: Wed, 2 Oct 2024 10:30:16 +0800 Subject: [PATCH 3/3] lint test code --- struct-patch-derive/src/lib.rs | 1 + struct-patch/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/struct-patch-derive/src/lib.rs b/struct-patch-derive/src/lib.rs index c274e71..9006272 100644 --- a/struct-patch-derive/src/lib.rs +++ b/struct-patch-derive/src/lib.rs @@ -560,6 +560,7 @@ mod tests { .unwrap(), attributes: vec![], retyped: true, + #[cfg(feature = "op")] addable: Addable::Disable, }], }; diff --git a/struct-patch/src/lib.rs b/struct-patch/src/lib.rs index a6fb3f4..f9ec811 100644 --- a/struct-patch/src/lib.rs +++ b/struct-patch/src/lib.rs @@ -109,6 +109,7 @@ mod tests { #[test] fn test_derive() { + #[allow(dead_code)] #[derive(Patch)] #[patch(attribute(derive(Copy, Clone, PartialEq, Debug)))] struct Item;