Is this a clippy bug?


#1

I have the following code sequence:

        let mut udn = try_get!(service.description, "/root/device/UDN").clone();
        if udn.starts_with("uuid:") {
            udn = String::from(&udn[5..]);
        }

and clippy gives me the following suggestion:

src/adapters/ip_camera/upnp_listener.rs:57:9: 63:10 warning: `if _ { .. } else { .. }` is an expression
src/adapters/ip_camera/upnp_listener.rs:57         let mut udn = try_get!(service.description, "/root/device/UDN").clone();
                                                   ^
src/main.rs:18:9: 18:27 note: lint level defined here
src/main.rs:18 #![warn(useless_let_if_seq)]
                       ^~~~~~~~~~~~~~~~~~
src/adapters/ip_camera/upnp_listener.rs:57:9: 63:10 help: it is more idiomatic to write
src/adapters/ip_camera/upnp_listener.rs:           let <mut> udn = if udn.starts_with("uuid:") { String::from(&udn[5..]) } else { try_get!(service.description, "/root/device/UDN").clone() };
src/adapters/ip_camera/upnp_listener.rs:57:9: 63:10 note: you might not need `mut` at all
src/adapters/ip_camera/upnp_listener.rs:57:9: 63:10 help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#useless_let_if_seq

It seems to me that the suggested improvement won’t produce correct code, since the if half needs the result from the else half.

The complete rust code is here:

So my question is: is this a bug in clippy?


#2

This is definitely a bug, the code it suggests wouldn’t compile because it calls a method on udn in the conditional before udn is defined.


#3

Thanks, I just wanted to make sure I wasn’t overlooking anything obvious.

I went and filed an issue: