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

False positive unnecessary_cast on type alias to cfg-dependent type alias #8093

Closed
dtolnay opened this issue Dec 8, 2021 · 3 comments · Fixed by rust-lang/rust#112490
Closed
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

@dtolnay
Copy link
Member

dtolnay commented Dec 8, 2021

Summary

Closely related to #6331:

#[cfg(unix)]
type CfgDependent = u8;

#[cfg(not(unix))]
type CfgDependent = i8;

type AlsoCfgDependent = CfgDependent;

fn main() {
    let _ = 0 as CfgDependent;
    let _ = 0 as AlsoCfgDependent;
}
$ cargo clippy
warning: casting integer literal to `u8` is unnecessary
  --> src/main.rs:11:13
   |
11 |     let _ = 0 as AlsoCfgDependent;
   |             ^^^^^^^^^^^^^^^^^^^^^ help: try: `0_u8`
   |
   = note: `#[warn(clippy::unnecessary_cast)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

Following the fix of #6331, Clippy is correctly no longer triggering unnecessary_cast on 0 as CfgDependent. However the same lint is still triggered on 0 as AlsoCfgDependent in which Clippy's suggested replacement is incorrect for exactly the same reason. It would be better to transitively look through type aliases when looking for the presence of a cfg attribute.

Lint Name

unnecessary_cast

Version

rustc 1.59.0-nightly (0b6f079e4 2021-12-07)
binary: rustc
commit-hash: 0b6f079e4987ded15c13a15b734e7cfb8176839f
commit-date: 2021-12-07
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 8, 2021
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 8, 2021
@kraktus
Copy link
Contributor

kraktus commented Oct 2, 2022

since #8596 unnecessary_cast does not lint type aliases at all anymore, so I guess this can be closed

@necrashter
Copy link

since #8596 unnecessary_cast does not lint type aliases at all anymore, so I guess this can be closed

I have rustc version 1.66.0, and clippy does complain about unnecessary casts from a type alias (not config dependent) to a primitive. IMHO this lint shouldn't apply to type aliases at all because declaring a type alias expresses the intention to possibly change the type or make it config dependent in the future.

@lygstate
Copy link

lygstate commented May 8, 2023

Can this be closed now?

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 22, 2023
Remove `#[cfg(all())]` workarounds from `c_char`

Casts to type aliases are now ignored by Clippy rust-lang/rust-clippy#8596

Closes rust-lang/rust-clippy#8093
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 22, 2023
Remove `#[cfg(all())]` workarounds from `c_char`

Casts to type aliases are now ignored by Clippy rust-lang/rust-clippy#8596

Closes rust-lang/rust-clippy#8093
thomcc pushed a commit to tcdi/postgrestd that referenced this issue Oct 17, 2023
Remove `#[cfg(all())]` workarounds from `c_char`

Casts to type aliases are now ignored by Clippy rust-lang/rust-clippy#8596

Closes rust-lang/rust-clippy#8093
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.

5 participants