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

Lossy type aliases in cast_lossless lint #11285

Closed
ijijn opened this issue Aug 2, 2023 · 4 comments · Fixed by #11287
Closed

Lossy type aliases in cast_lossless lint #11285

ijijn opened this issue Aug 2, 2023 · 4 comments · Fixed by #11287
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@ijijn
Copy link
Contributor

ijijn commented Aug 2, 2023

Summary

Hello team!

I've noticed that using type aliases for built-in arithmetic types doesn't play especially well with the cast_lossless lint when it comes to casting to more capacious siblings.

I would expect the type alias to be adopted for associated warnings and fixes, rather than the underlying type itself, producing a minor semantic disconnect, and failed compilations in a somewhat confusing place should the type alias ever change. See below for a very short example.

Thanks,
Iain

Reproducer

I tried this code:

#![warn(clippy::pedantic)]

fn twelve_gets_a_promotion() {
    type Big = u64;
    let u8_twelve = 12_u8;
    let big_twelve = u8_twelve as Big;
    dbg!(big_twelve);
}

I expected to see this happen:

(warning)

casting `u8` to `Big` may become silently lossy if you later change the type

(fix)

fn twelve_gets_a_promotion() {
    type Big = u64;
    let u8_twelve = 12_u8;
    let big_twelve = Big::from(u8_twelve);
    dbg!(big_twelve);
}

Instead, this happened:

(warning)

casting `u8` to `u64` may become silently lossy if you later change the type

(fix)

fn twelve_gets_a_promotion() {
    type Big = u64;
    let u8_twelve = 12_u8;
    let big_twelve = u64::from(u8_twelve);
    dbg!(big_twelve);
}

Version

rustc 1.73.0-nightly (d12c6e947 2023-08-01)
binary: rustc
commit-hash: d12c6e947ceacf3b22c154caf9532b390d8dc88a
commit-date: 2023-08-01
host: x86_64-pc-windows-msvc
release: 1.73.0-nightly
LLVM version: 16.0.5

Additional Labels

No response

@ijijn ijijn added the C-bug Category: Clippy is not doing the correct thing label Aug 2, 2023
@Centri3
Copy link
Member

Centri3 commented Aug 2, 2023

Type aliases are unfortunately entirely stripped when going from the HIR Ty to Ty, there's sadly nothing clippy can do without upstream changes.

There has been work on making them a "real type", though: rust-lang/rust#112792

@ijijn
Copy link
Contributor Author

ijijn commented Aug 2, 2023

Ah, thank you very much, Catherine! Makes sense. Fingers crossed that we can get some improvements in that department soon. 👍🏾

@Centri3
Copy link
Member

Centri3 commented Aug 2, 2023

Hmm actually... I believe in this case we do have the HIR type, since it's an as cast, so we can probably fix this one by using that instead. I'll open a PR for that soon.

@ijijn
Copy link
Contributor Author

ijijn commented Aug 2, 2023

Oh that’s wonderful! Thanks so much 🏆

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants