Open
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