Skip to content

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

Closed
rust-lang/rust
#112490
@dtolnay

Description

@dtolnay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions