Skip to content

Commit 80e78bc

Browse files
committed
reduce amount of copies in derive macro, general lint fixes
1 parent b0b52a0 commit 80e78bc

File tree

8 files changed

+113
-84
lines changed

8 files changed

+113
-84
lines changed

derive/src/difference.rs

Lines changed: 85 additions & 67 deletions
Large diffs are not rendered by default.

derive/src/parse.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,23 @@ impl Data {
173173
}
174174

175175
impl Generic {
176-
pub fn full(&self) -> String {
176+
pub fn full(&self) -> &str {
177177
match &self {
178-
Generic::Const { name, .. } => name.clone(),
179-
Generic::Regular { name, .. } => name.clone(),
180-
Generic::Lifetime { name, .. } => name.clone(),
181-
Generic::WhereBounded { name, .. } => name.clone(),
178+
Generic::Const { name, .. } => name,
179+
Generic::Regular { name, .. } => name,
180+
Generic::Lifetime { name, .. } => name,
181+
Generic::WhereBounded { name, .. } => name,
182182
}
183183
}
184184

185-
fn lifetime_prefix(&self) -> &str {
185+
fn lifetime_prefix(&self) -> &'static str {
186186
match &self {
187187
Generic::Lifetime { .. } => "\'",
188188
_ => "",
189189
}
190190
}
191191

192-
fn const_prefix(&self) -> &str {
192+
fn const_prefix(&self) -> &'static str {
193193
match &self {
194194
Generic::Const { .. } => "const ",
195195
_ => "",
@@ -1421,7 +1421,7 @@ fn get_all_bounds<T: Iterator<Item = TokenTree> + Clone>(source: &mut Peekable<T
14211421
// let mut generic_bounds = Vec::new();
14221422
// let mut in_type = true;
14231423
while let Some(gen) = next_generic(source) {
1424-
if already.insert(gen.full()) {
1424+
if already.insert(gen.full().to_owned()) {
14251425
ret.push(gen);
14261426
} else {
14271427
match (
@@ -1453,7 +1453,7 @@ fn get_all_bounds<T: Iterator<Item = TokenTree> + Clone>(source: &mut Peekable<T
14531453
panic!("mismatched generic types")
14541454
}
14551455
}
1456-
}
1456+
};
14571457
let Some(_) = next_exact_punct(source, ",") else {
14581458
break;
14591459
};
@@ -1470,7 +1470,7 @@ fn get_all_bounds<T: Iterator<Item = TokenTree> + Clone>(source: &mut Peekable<T
14701470
}
14711471

14721472
while let Some(gen) = next_generic(source) {
1473-
if already.insert(gen.full()) {
1473+
if already.insert(gen.full().to_owned()) {
14741474
let gen = match gen {
14751475
Generic::Regular { name, bounds, .. } => Generic::WhereBounded { name, bounds },
14761476
where_bounded @ Generic::WhereBounded { .. } => where_bounded,

derive/src/shared.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub enum CollectionStrategy {
2323
}
2424

2525
#[cfg(feature = "generated_setters")]
26-
pub fn attrs_setter(attributes: &[crate::parse::Attribute]) -> (bool, bool, Option<String>) {
26+
pub fn attrs_setter(attributes: &[crate::parse::Attribute]) -> (bool, bool, Option<&str>) {
2727
let skip = attributes
2828
.iter()
2929
.any(|attr| attr.tokens.len() == 1 && attr.tokens[0] == "skip_setter");
@@ -33,7 +33,7 @@ pub fn attrs_setter(attributes: &[crate::parse::Attribute]) -> (bool, bool, Opti
3333

3434
let Some(name_override) = attributes.iter().find_map(|attr| {
3535
if attr.tokens.len() == 2 && attr.tokens[0] == "setter_name" {
36-
Some(attr.tokens[1].clone())
36+
Some(&attr.tokens[1])
3737
} else {
3838
None
3939
}
@@ -87,7 +87,7 @@ pub fn attrs_collection_type(attributes: &[crate::parse::Attribute]) -> Option<C
8787
pub fn attrs_map_strategy(attributes: &[crate::parse::Attribute]) -> Option<MapStrategy> {
8888
attributes.iter().find_map(|attr| {
8989
if attr.tokens.len() == 2 && attr.tokens[0] == "map_equality" {
90-
let strategy = match attr.tokens[1].clone().as_str() {
90+
let strategy = match attr.tokens[1].as_str() {
9191
"key_only" => MapStrategy::KeyOnly,
9292
"key_and_value" => MapStrategy::KeyAndValue,
9393
_ => {
@@ -101,10 +101,10 @@ pub fn attrs_map_strategy(attributes: &[crate::parse::Attribute]) -> Option<MapS
101101
})
102102
}
103103

104-
pub fn attrs_expose(attributes: &[crate::parse::Attribute]) -> Option<Option<String>> {
104+
pub fn attrs_expose(attributes: &[crate::parse::Attribute]) -> Option<Option<&str>> {
105105
attributes.iter().find_map(|attr| match attr.tokens.len() {
106106
1 if attr.tokens[0].starts_with("expose") => Some(None),
107-
2.. if attr.tokens[0] == "expose" => Some(Some(attr.tokens[1].to_string())),
107+
2.. if attr.tokens[0] == "expose" => Some(Some(attr.tokens[1].as_str())),
108108
_ => None,
109109
})
110110
}

justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fmt:
2+
cargo fmt && cargo fmt --manifest-path ./derive/Cargo.toml
3+
clippy:
4+
cargo clippy --all-features && cargo clippy --all-features --manifest-path ./derive/Cargo.toml
5+
test:
6+
cargo test && cargo test --all-features

src/collections/ordered_array_like.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ fn create_full_change_table<T: PartialEq>(
174174
entry[0] = ChangeInternal::Insert(i * INSERT_COST);
175175
}
176176

177+
#[allow(clippy::needless_range_loop)]
177178
for j in 0..=source.len() {
178179
table[0][j] = ChangeInternal::Delete(j * DELETE_COST)
179180
}

tests/derives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct TestDeriveAll<
3030
[i32; N]: Default,
3131
[B; N]: Default,
3232
dyn Fn(&B): PartialEq + Clone + core::fmt::Debug,
33-
(dyn core::fmt::Debug + Send + 'static): Debug,
33+
dyn core::fmt::Debug + Send + 'static: Debug,
3434
{
3535
f1: (),
3636
f2: [A; N],

tests/enums.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use std::{
66
fmt::Debug,
77
num::Wrapping,
88
};
9-
use structdiff::{Difference, StructDiff};
9+
use structdiff::Difference;
1010

11+
#[allow(dead_code)]
1112
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1213
#[derive(Debug, PartialEq, Clone, Difference, Default)]
1314
#[difference(setters)]
@@ -19,6 +20,7 @@ pub struct Test {
1920
pub test5: Option<usize>,
2021
}
2122

23+
#[allow(dead_code)]
2224
#[derive(Debug, PartialEq, Clone, Difference)]
2325
#[difference(setters)]
2426
pub struct TestSkip<A>

tests/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use nanorand::{Rng, WyRand};
66
use nanoserde::{DeBin, SerBin};
77
#[cfg(feature = "serde")]
88
use serde::{Deserialize, Serialize};
9+
#[cfg_attr(not(feature = "generated_setters"), expect(unused_imports))]
910
use structdiff::{Difference, StructDiff};
1011

1112
pub trait RandValue
@@ -96,6 +97,7 @@ impl RandValue for TestEnum {
9697

9798
#[derive(Difference, Default, PartialEq, Debug, Clone)]
9899
#[difference(setters)]
100+
#[allow(unused)]
99101
pub struct TestSetters {
100102
#[difference(setter_name = "testing123", recurse)]
101103
pub f0: Test,

0 commit comments

Comments
 (0)