rust-lang / rust-lang/rust-clippy

`cast_lossless` not triggered by `char as u32`

Open
#14,469 4 comments 1 reaction 1 assignee View on GitHub

Nobody has claimed this yet.

C-bug good first issue I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.