rust-lang / rust-lang/rust-clippy
Clippy might suggest code that can resolve to multiple alternatives
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Clippy might suggest code that won't compile because the proposed solution leads to ambiguity.
Reproducer
I tried this code:
trait FromU8ToOption: Sized {
fn from(a: u8) -> Option<Self>;
}
impl FromU8ToOption for u16 {
fn from(a: u8) -> Option<Self> { Some(a as Self) }
}
fn main() {
let _: u16 = 0u8 as u16;
}
The cast_lossless lint suggested replacing the conversion by u16::from(0u8) which is perfectly reasonable but does not compile because of the overloaded u16::from().
This is not an hypothetical case, as this can be found when autofixing the num-traits crate. Granted, this is a very special case, and defining traits with some well-known names used in the prelude is risky, but it would be nice if Clippy was able to check if the suggestion will resolve unambiguously.
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
Additional Labels
@rustbot label +I-suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the cast_lossless lint and reproduce the reported u16::from(0u8) suggestion using the example in the issue. Investigate whether the suggested replacement resolves unambiguously, and verify that the resulting suggestion compiles without ambiguity while preserving the lint's intended behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100