Tim Diekmann
Tim Diekmann

Reputation: 8476

What does #[automatically_derived] mean?

I found #[automatically_derived] in the serde-derive crate when generating the implementations for the derived type:

quote! {
    #[automatically_derived]
    impl #impl_generics _serde::Serialize for #ident #ty_generics #where_clause {
        fn serialize<__S>(&self, __serializer: __S) -> _serde::export::Result<__S::Ok, __S::Error>
        where
            __S: _serde::Serializer,
        {
            #body
        }
    }
}

What does this mean? When I should use this?

I also found this in several expanded macros, but I couldn't find any description about this line.

Upvotes: 13

Views: 3723

Answers (1)

Shepmaster
Shepmaster

Reputation: 431479

It is an indication to the compiler that the marked code should not be reported as unused, among other things:

// Don't run unused pass for #[derive()]
if let FnKind::Method(..) = fk {
    let parent = ir.tcx.hir().get_parent_item(id);
    if let Some(Node::Item(i)) = ir.tcx.hir().find(parent) {
        if i.attrs.iter().any(|a| a.check_name(sym::automatically_derived)) {
            return;
        }
    }
}

It also applies to

Upvotes: 14

Related Questions