Repository: bevyengine/bevy
File: crates/bevy_ecs/macros/src/component.rs (Rust)

Code before:
        on_insert: None,
        on_replace: None,
        on_remove: None,
        on_despawn: None,
        requires: None,
        relationship: None,
        relationship_target: None,
        immutable: false,
        clone_behavior: None,
        map_entities: None,
    };

    let mut require_paths = HashSet::new();
    for attr in ast.attrs.iter() {
        if attr.path().is_ident(COMPONENT) {
            attr.parse_nested_meta(|nested| {
                if nested.path.is_ident(STORAGE) {
                    attrs.storage = match nested.value()?.parse::<LitStr>()?.value() {
                        s if s == TABLE => StorageTy::Table,
                        s if s == SPARSE_SET => StorageTy::SparseSet,
                        s => {
                            return Err(nested.error(format!(
                                "Invalid storage type `{s}`, expected '{TABLE}' or '{SPARSE_SET}'.",
                            )));
                        }
                    };
                    Ok(())
                } else if nested.path.is_ident(ON_ADD) {
                    attrs.on_add = Some(HookAttributeKind::parse(
                        nested.input,
                        parse_quote! { Self::on_add },
                    )?);
                    Ok(())
                } else if nested.path.is_ident(ON_INSERT) {
                    attrs.on_insert = Some(HookAttributeKind::parse(
                        nested.input,
                        parse_quote! { Self::on_insert },
                    )?);
                    Ok(())
                } else if nested.path.is_ident(ON_REPLACE) {
                    attrs.on_replace = Some(HookAttributeKind::parse(
                        nested.input,
                        parse_quote! { Self::on_replace },
                    )?);
                    Ok(())
                } else if nested.path.is_ident(ON_REMOVE) {
                    attrs.on_remove = Some(HookAttributeKind::parse(
                        nested.input,
                        parse_quote! { Self::on_remove },
                    )?);
                    Ok(())

Review comment (suggestion):
you could do 
``` rust 
attrs.on_add = Some(nested.value()?.parse::<HookAttributeKind>().unwrap_or_else( parse_quote! { Self::on_add }));
```
and then the parse function stays the same as the old one. Might be more clear.

Code after:
    let mut attrs = Attrs {
        storage: StorageTy::Table,
        on_add: None,
        on_insert: None,
        on_replace: None,
        on_remove: None,
        on_despawn: None,
        requires: None,
        relationship: None,
        relationship_target: None,
        immutable: false,
        clone_behavior: None,
        map_entities: None,
    };

    let mut require_paths = HashSet::new();
    for attr in ast.attrs.iter() {
        if attr.path().is_ident(COMPONENT) {
            attr.parse_nested_meta(|nested| {
                if nested.path.is_ident(STORAGE) {
                    attrs.storage = match nested.value()?.parse::<LitStr>()?.value() {
                        s if s == TABLE => StorageTy::Table,
                        s if s == SPARSE_SET => StorageTy::SparseSet,
                        s => {
                            return Err(nested.error(format!(
                                "Invalid storage type `{s}`, expected '{TABLE}' or '{SPARSE_SET}'.",
                            )));
                        }
                    };
                    Ok(())
                } else if nested.path.is_ident(ON_ADD) {
                    attrs.on_add = Some(HookAttributeKind::parse(nested.input, || {
                        parse_quote! { Self::on_add }
                    })?);
                    Ok(())
                } else if nested.path.is_ident(ON_INSERT) {
                    attrs.on_insert = Some(HookAttributeKind::parse(nested.input, || {
                        parse_quote! { Self::on_insert }
                    })?);
                    Ok(())
                } else if nested.path.is_ident(ON_REPLACE) {
                    attrs.on_replace = Some(HookAttributeKind::parse(nested.input, || {
                        parse_quote! { Self::on_replace }
                    })?);
                    Ok(())
                } else if nested.path.is_ident(ON_REMOVE) {
                    attrs.on_remove = Some(HookAttributeKind::parse(nested.input, || {
                        parse_quote! { Self::on_remove }
                    })?);
                    Ok(())
                } else if nested.path.is_ident(ON_DESPAWN) {