Skip to content

False positive in cmp_owned lint #4909

@tesuji

Description

@tesuji

Consider this (crappy code)

macro_rules! foo {
    ($($i:ident: $ty:ty, $def:expr);+ $(;)*) => {
        pub fn bar(key: &str) -> bool {
            match key {
                $(
                    stringify!($i) => {
                        let res = <$ty>::default() == $def;
                        let _i: $ty = $def;
                        return res;
                    }
                )+
                _ => false,
            }
        }
    };
}

foo! {
    max_width: usize, 100;
    required_version: String, env!("HOME").to_string();
}

Clippy outputs this warnings:

warning: this creates an owned instance just for comparison
  --> src/lib.rs:20:31
   |
20 |     required_version: String, env!("HOME").to_string();
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `env!("HOME")`
   |
   = note: `#[warn(clippy::cmp_owned)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned

Which is wrong.


cargo clippy --version: clippy 0.0.212 (69f99e7 2019-12-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions