Rollup merge of #152780 - camsteffen:less-clone, r=Kivooeo

Remove some clones in deriving

Just factoring away a few `.clone()`s.
This commit is contained in:
Jonathan Brouwer 2026-02-18 18:55:19 +01:00 committed by GitHub
commit c7a4bd6f9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 19 deletions

View file

@ -67,7 +67,7 @@ fn default_struct_substructure(
cx: &ExtCtxt<'_>,
trait_span: Span,
substr: &Substructure<'_>,
summary: &StaticFields,
summary: &StaticFields<'_>,
) -> BlockOrExpr {
let expr = match summary {
Unnamed(_, IsTuple::No) => cx.expr_ident(trait_span, substr.type_ident),
@ -78,16 +78,16 @@ fn default_struct_substructure(
Named(fields) => {
let default_fields = fields
.iter()
.map(|(ident, span, default_val)| {
.map(|&(ident, span, default_val)| {
let value = match default_val {
// We use `Default::default()`.
None => default_call(cx, *span),
None => default_call(cx, span),
// We use the field default const expression.
Some(val) => {
cx.expr(val.value.span, ast::ExprKind::ConstBlock(val.clone()))
}
};
cx.field_imm(*span, *ident, value)
cx.field_imm(span, ident, value)
})
.collect();
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)

View file

@ -303,11 +303,11 @@ pub(crate) enum IsTuple {
}
/// Fields for a static method
pub(crate) enum StaticFields {
pub(crate) enum StaticFields<'a> {
/// Tuple and unit structs/enum variants like this.
Unnamed(Vec<Span>, IsTuple),
/// Normal structs/struct variants.
Named(Vec<(Ident, Span, Option<AnonConst>)>),
Named(Vec<(Ident, Span, Option<&'a AnonConst>)>),
}
/// A summary of the possible sets of fields.
@ -331,7 +331,7 @@ pub(crate) enum SubstructureFields<'a> {
EnumDiscr(FieldInfo, Option<Box<Expr>>),
/// A static method where `Self` is a struct.
StaticStruct(&'a ast::VariantData, StaticFields),
StaticStruct(&'a ast::VariantData, StaticFields<'a>),
/// A static method where `Self` is an enum.
StaticEnum(&'a ast::EnumDef),
@ -596,7 +596,7 @@ impl<'a> TraitDef<'a> {
cx: &ExtCtxt<'_>,
type_ident: Ident,
generics: &Generics,
field_tys: Vec<Box<ast::Ty>>,
field_tys: Vec<&ast::Ty>,
methods: Vec<Box<ast::AssocItem>>,
is_packed: bool,
) -> Box<ast::Item> {
@ -870,8 +870,7 @@ impl<'a> TraitDef<'a> {
from_scratch: bool,
is_packed: bool,
) -> Box<ast::Item> {
let field_tys: Vec<Box<ast::Ty>> =
struct_def.fields().iter().map(|field| field.ty.clone()).collect();
let field_tys = Vec::from_iter(struct_def.fields().iter().map(|field| &*field.ty));
let methods = self
.methods
@ -923,11 +922,13 @@ impl<'a> TraitDef<'a> {
generics: &Generics,
from_scratch: bool,
) -> Box<ast::Item> {
let mut field_tys = Vec::new();
for variant in &enum_def.variants {
field_tys.extend(variant.data.fields().iter().map(|field| field.ty.clone()));
}
let field_tys = Vec::from_iter(
enum_def
.variants
.iter()
.flat_map(|variant| variant.data.fields())
.map(|field| &*field.ty),
);
let methods = self
.methods
@ -1160,8 +1161,8 @@ impl<'a> MethodDef<'a> {
fn expand_static_struct_method_body(
&self,
cx: &ExtCtxt<'_>,
trait_: &TraitDef<'_>,
struct_def: &VariantData,
trait_: &TraitDef<'a>,
struct_def: &'a VariantData,
type_ident: Ident,
nonselflike_args: &[Box<Expr>],
) -> BlockOrExpr {
@ -1480,13 +1481,13 @@ impl<'a> MethodDef<'a> {
// general helper methods.
impl<'a> TraitDef<'a> {
fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields {
fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &'a VariantData) -> StaticFields<'a> {
let mut named_idents = Vec::new();
let mut just_spans = Vec::new();
for field in struct_def.fields() {
let sp = field.span.with_ctxt(self.span.ctxt());
match field.ident {
Some(ident) => named_idents.push((ident, sp, field.default.clone())),
Some(ident) => named_idents.push((ident, sp, field.default.as_ref())),
_ => just_spans.push(sp),
}
}