rust-lang / rust-lang/rust-clippy

Clippy might suggest code that can resolve to multiple alternatives

Open
#14,498 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error P-low
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

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.