Skip to content

cast_lossless not triggered by char as u32 #14469

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

Open
y86-dev opened this issue Mar 25, 2025 · 3 comments · May be fixed by #14470
Open

cast_lossless not triggered by char as u32 #14469

y86-dev opened this issue Mar 25, 2025 · 3 comments · May be fixed by #14470
Assignees
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 I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@y86-dev
Copy link

y86-dev commented Mar 25, 2025

Summary

Casting a char to a u32 is lossless, but the cast_lossless lint doesn't cover it.

Lint Name

cast_lossless

Reproducer

I tried this code:

#![deny(clippy::cast_lossless)]

fn main() {
    let _ = '|' as u32;
}

Playground

I expected to see this happen: Compiler error

error: casts from `char` to `u32` can be expressed infallibly using `From`
 --> src/main.rs:4:13
  |
4 |     let _ = '|' as u32;
  |             ^^^^^^^^^^
  |
  = help: an `as` cast can become silently lossy if the types change in the future
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(clippy::cast_lossless)]
  |         ^^^^^^^^^^^^^^^^^^^^^
help: use `u32::from` instead
  |
4 |     let _ = u32::from('|');
  |             ~~~~~~~~~~~~~~

Instead, this happened:

The code compiled without any issues.

Version

rustc 1.87.0-nightly (1aeb99d24 2025-03-19)
binary: rustc
commit-hash: 1aeb99d248e1b0069110cb03c6f1dcc7b36fd7f3
commit-date: 2025-03-19
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

(also on stable via playground)

This issue has been assigned to @lapla-cogito via this comment.

@y86-dev y86-dev added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Mar 25, 2025
@samueltardieu samueltardieu added the good-first-issue These issues are a good way to get started with Clippy label Mar 25, 2025
@zen-zap
Copy link

zen-zap commented Apr 14, 2025

@rustbot claim

@samueltardieu
Copy link
Contributor

@zen-zap It looks like @lapla-cogito has already submitted a pull request to fix the issue but forgot to assign themselves.

@rustbot assign @lapla-cogito

@rustbot rustbot assigned rustbot and unassigned zen-zap Apr 14, 2025
@zen-zap
Copy link

zen-zap commented Apr 14, 2025

oh that's fine .. thanks

@samueltardieu samueltardieu assigned rustbot and unassigned rustbot Apr 14, 2025
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 I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants