Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

identity_conversion false positive (macro + ?) #4098

Closed
realcr opened this issue May 14, 2019 · 2 comments
Closed

identity_conversion false positive (macro + ?) #4098

realcr opened this issue May 14, 2019 · 2 comments

Comments

@realcr
Copy link

realcr commented May 14, 2019

Hi, I think I'm having a false positive with the identity_conversion lint. Minimal example:

pub trait SafeSignedArithmetic: Copy {
    type Unsigned;

    fn checked_add_unsigned(self, u: Self::Unsigned) -> Option<Self>;
}

macro_rules! impl_safe_signed_arithmetic {
    ( $i:ty, $u:ty ) => {
        impl SafeSignedArithmetic for $i {
            type Unsigned = $u;

            fn checked_add_unsigned(self, u: $u) -> Option<$i> {
                let u_half = (u / 2) as $i;
                let u_rem = (u % 2) as $i;

                self.checked_add(u_half)?
                    .checked_add(u_half)?
                    .checked_add(u_rem)
            }
        }
    };
}

impl_safe_signed_arithmetic!(i8, u8);

fn main() {
    println!("Hello, world!");
}

Versions:

$ cargo clippy -V
clippy 0.0.212 (3710ec5 2019-05-11)

$ rustc --version
rustc 1.36.0-nightly (af98304b9 2019-05-11)

Clippy's output:

$ cargo clippy
    Checking check_identical_conversion v0.1.0 (/home/real/temp/check_identical_conversion)
warning: identical conversion
  --> src/main.rs:17:41
   |
17 |                 self.checked_add(u_half)?
   |                                         ^
...
25 | impl_safe_signed_arithmetic!(i8, u8);
   | ------------------------------------- in this macro invocation
   |
   = note: #[warn(clippy::identity_conversion)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_conversion

warning: identical conversion
  --> src/main.rs:18:41
   |
18 |                     .checked_add(u_half)?
   |                                         ^
...
25 | impl_safe_signed_arithmetic!(i8, u8);
   | ------------------------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_conversion

    Finished dev [unoptimized + debuginfo] target(s) in 0.20s

Interestingly enough, if I try to implement this code without the macro, the clippy warnings disappear.

Thank you for developing clippy!

@flip1995
Copy link
Member

This is already fixed in the latest release of Clippy: #4082 #4089

Playground

Try running rustup update nightly and try it again. If the issue still persists feel free to reopen.

@Manishearth
Copy link
Member

Beta is coming soon, we should consider backports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants