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 type error #9960

Closed
matthiaskrgr opened this issue Nov 26, 2022 · 0 comments · Fixed by #9980
Closed

unnecessary_cast type error #9960

matthiaskrgr opened this issue Nov 26, 2022 · 0 comments · Fixed by #9980
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Lint Name

unnecessary_cast

Reproducer

I tried this code:

#![allow(dead_code)]

enum T {
    A(isize),
    B(usize)
}

macro_rules! test {
    ($id:ident, $e:expr) => (
        fn foo(t: T) -> isize {
            match t {
                T::A($id) => $e,
                T::B($id) => $e
            }
        }
    )
}

test!(y, 10 + (y as isize));

pub fn main() {
    foo(T::A(20));
}

I saw this happen:
cargo clippy --fix

The following errors were reported:
error[E0308]: `match` arms have incompatible types
  --> src/main.rs:19:10
   |
11 | /             match t {
12 | |                 T::A($id) => $e,
13 | |                 T::B($id) => $e
14 | |             }
   | |_____________- `match` arms have incompatible types
...
19 |   test!(y, 10 + y);
   |            ^^^^^^
   |            |
   |            expected `isize`, found `usize`
   |            this is found to be of type `isize`
   |
help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit
   |
19 | test!(y, (10 + y).try_into().unwrap());
   |          +      +++++++++++++++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: casting to the same type is unnecessary (`isize` -> `isize`)
  --> src/main.rs:19:15
   |
19 | test!(y, 10 + (y as isize));
   |               ^^^^^^^^^^^^ help: try: `y`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
   = note: `#[warn(clippy::unnecessary_cast)]` on by default


Version

rustc 1.67.0-nightly (b3bc6bf31 2022-11-24)
binary: rustc
commit-hash: b3bc6bf31265ac10946a0832092dbcedf9b26805
commit-date: 2022-11-24
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 26, 2022
@bors bors closed this as completed in e9a8b8c Nov 29, 2022
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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant