bodil / vgtk

A declarative desktop UI framework for Rust built on GTK and Gtk-rs
http://bodil.lol/vgtk/
Other
1.05k stars 37 forks source link

gtk! macro produces code flagged by clippy::field_reassign_with_default #75

Open meteficha opened 3 years ago

meteficha commented 3 years ago

The test case is already committed, clippy will complain about examples/todomvc/src/app.rs:

error: field assignment outside of initializer for an instance created with Default::default()
Error:    --> examples/todomvc/src/app.rs:66:9
    |
66  | /         gtk! {
67  | |             <Box spacing=10 orientation=Orientation::Vertical>
68  | |                 <Box spacing=10 orientation=Orientation::Horizontal Box::expand=false>
69  | |                     <Button image="edit-select-all" relief=ReliefStyle::Half
...   |
99  | |             </Box>
100 | |         }
    | |_________^
    |
    = note: `-D clippy::field-reassign-with-default` implied by `-D warnings`
note: consider initializing the variable with `radio::Radio<app::Filter> { active: #[proc_macro_hack(support_nested)]
pub use vgtk_macros::gtk, ..Default::default() }` and removing relevant reassignments
   --> examples/todomvc/src/app.rs:66:9
    |
66  | /         gtk! {
67  | |             <Box spacing=10 orientation=Orientation::Vertical>
68  | |                 <Box spacing=10 orientation=Orientation::Horizontal Box::expand=false>
69  | |                     <Button image="edit-select-all" relief=ReliefStyle::Half
...   |
99  | |             </Box>
100 | |         }
    | |_________^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#field_reassign_with_default
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

This was found by an unrelated PR (#73). One can silence the warning at the call site (https://github.com/bodil/vgtk/pull/73/commits/3054df9d7f7fca9ad1f602e87039281a70c104f2), but that's not great. @meteficha couldn't find how to make the macro generate a silencing pragma that clippy would respect.

meteficha commented 3 years ago

The problem lies in macros/src/gtk.rs, function expand_component. It's written as:

pub fn expand_component(gtk: &GtkComponent) -> TokenStream {
    let name = to_stream(&gtk.name);
    let mut out = quote!(
        use vgtk::{Component, vnode::VComponent, vnode::PropTransform};
        let mut vcomp = VComponent::new::<#name>();
        let mut props = <#name as Component>::Properties::default();
    );

    ...
    // Add lines modifying `props` to `out`. 
    ...

    quote!({
        #out
        vcomp.set_props::<#name>(props);
        VNode::Component(vcomp)
    })
}

Clippy doesn't like that we have a mut props set to default then changed. It's a silly complaint because it's much harder to generate code that doesn't use default here. I've tried adding #![allow(clippy::field_reassign_with_default)] to the generated code in a few ways, but clippy seemed to always ignore it.