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

unnecessary_cast false positives in macros #3981

Closed
gnzlbg opened this issue Apr 17, 2019 · 7 comments · Fixed by #4026
Closed

unnecessary_cast false positives in macros #3981

gnzlbg opened this issue Apr 17, 2019 · 7 comments · Fixed by #4026
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions T-macros Type: Issues with macros and macro expansion

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 17, 2019

Playground:

#![deny(clippy::unnecessary_cast)]

macro_rules! foo {
    ($a:ident, $b:ident) => {
        pub fn $a() -> $b {
            1 as $b
        }
    };
}

foo!(a, i32);
foo!(b, f32);
foo!(c, f64);

produces

error: casting integer literal to f32 is unnecessary
  --> src/lib.rs:6:13
   |
6  |             1 as $b
   |             ^^^^^^^ help: try: `1_f32`
...
12 | foo!(b, f32);
   | ------------- in this macro invocation
   |
note: lint level defined here
  --> src/lib.rs:1:9
   |
1  | #![deny(clippy::unnecessary_cast)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

error: casting integer literal to f64 is unnecessary
  --> src/lib.rs:6:13
   |
6  |             1 as $b
   |             ^^^^^^^ help: try: `1_f64`
...
13 | foo!(c, f64);
   | ------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

error: aborting due to 2 previous errors
@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-bug Category: Clippy is not doing the correct thing T-macros Type: Issues with macros and macro expansion L-suggestion Lint: Improving, adding or fixing lint suggestions labels Apr 17, 2019
@cemiloten
Copy link
Contributor

Hi, I would like to try to solve this, but I am very new to Rust, is that ok ?
If somebody could lead me towards a good way to getting started on it. Thanks :)

@cemiloten
Copy link
Contributor

I see the same errors as you in the playground, but I cannot reproduce it in my own machine.

cargo new test_project
cd test_project/
mv src/main.rs src/lib.rs

... copy paste your code in src/lib.rs and save

cargo clippy

results in

Cemils-MBP:test_project cemil$ cargo clippy
    Checking test_project v0.1.0 (/Users/cemil/dev/rust/test_project)
warning: casting i32 to f64 may become silently lossy if types change
  --> src/lib.rs:6:13
   |
6  |             1 as $b
   |             ^^^^^^^ help: try: `f64::from(1)`
...
13 | foo!(c, f64);
   | ------------- in this macro invocation
   |
   = note: #[warn(clippy::cast_lossless)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless

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

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Apr 22, 2019

Can you reproduce with cargo clippy -- -D clippy::pedantic ?

@cemiloten
Copy link
Contributor

Here is what I got (--verbose)

Cemils-MBP:test_project cemil$ cargo clippy --verbose -- -D clippy::pedantic
    Checking test_project v0.1.0 (/Users/cemil/dev/rust/test_project)
     Running `/Users/cemil/.rustup/toolchains/stable-x86_64-apple-darwin/bin/clippy-driver rustc --edition=2018 --crate-name test_project src/lib.rs --col
or always --crate-type lib --emit=dep-info,metadata -C debuginfo=2 -C metadata=c7c978c2d835e2e8 -C extra-filename=-c7c978c2d835e2e8 --out-dir /Users/cemil
/dev/rust/test_project/target/debug/deps -C incremental=/Users/cemil/dev/rust/test_project/target/debug/incremental -L dependency=/Users/cemil/dev/rust/te
st_project/target/debug/deps`
error: casting i32 to f32 causes a loss of precision (i32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
  --> src/lib.rs:6:13
   |
6  |             1 as $b
   |             ^^^^^^^
...
12 | foo!(b, f32);
   | ------------- in this macro invocation
   |
   = note: `-D clippy::cast-precision-loss` implied by `-D clippy::pedantic`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_precision_loss

warning: casting i32 to f64 may become silently lossy if types change
  --> src/lib.rs:6:13
   |
6  |             1 as $b
   |             ^^^^^^^ help: try: `f64::from(1)`
...
13 | foo!(c, f64);
   | ------------- in this macro invocation
   |
   = note: #[warn(clippy::cast_lossless)] on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless

error: aborting due to previous error

error: Could not compile `test_project`.

Caused by:
  process didn't exit successfully: `/Users/cemil/.rustup/toolchains/stable-x86_64-apple-darwin/bin/clippy-driver rustc --edition=2018 --crate-name test_p
roject src/lib.rs --color always --crate-type lib --emit=dep-info,metadata -C debuginfo=2 -C metadata=c7c978c2d835e2e8 -C extra-filename=-c7c978c2d835e2e8
 --out-dir /Users/cemil/dev/rust/test_project/target/debug/deps -C incremental=/Users/cemil/dev/rust/test_project/target/debug/incremental -L dependency=/
Users/cemil/dev/rust/test_project/target/debug/deps` (exit code: 1)

Still not getting these errors, are you?

@flip1995
Copy link
Member

Oh, what version of clippy do you have? (cargo clippy --version)
Try

$ rustup update nightly
($ rustup component add clippy --toolchain=nightly) # only necessary if you haven't installed Clippy in nightly before
$ cargo +nightly clippy

@cemiloten
Copy link
Contributor

Ok with nightly I am seeing these errors now. How would you suggest getting started on this ? Is there a way to use a debugger when running clippy from a build of the project ? (I am using VSCode) In any case I'm not sure how to check my work. Thanks :)

@flip1995
Copy link
Member

You can use the example code @gnzlbg provided and add it somewhere here:

// Test cast_unnecessary

After that you can test it with

$ TESTNAME=ui/cast cargo +master uitest

(You can install the master toolchain with RTIM or you can try if the nightly toolchain works by swapping master with nightly)

To fix this issue you need to add a macro check with the utils::in_macro function. You can grep the clippy_lints codebase to find examples how to do that.

After that the above command should run without errors. (Maybe some lines in the *.stderr file need to be adapted)

Also have a look at https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md. It's a really good documentation on how to work on Clippy.

bors added a commit that referenced this issue Apr 25, 2019
Attempt to fix #3981

Fixes #3981

Hi, hopefully this is correct, happy to have feedback.

changelog: Don't trigger `unnecessary_cast` inside a macro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants