rust-lang / rust-lang/rust-clippy

`unnecessary_cast` false negative: parameter vs. local

Open
#13,495 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

unnecessary_cast appears to trigger for a parameter, but not for a local.

Lint Name

unnecessary_cast

Reproducer

I tried this code:

fn g() -> i16 { 42 }

pub fn f1() -> i32 {
    let x = g();
    x as i32 as i32
}

pub fn f2(x: i16) -> i32 {
    x as i32 as i32
}

I expected to see this happen: two lints, one on each function.

Instead, this happened: one lint, only on f2.

Version

rustc 1.83.0-nightly (18b1161ec 2024-10-02)
binary: rustc
commit-hash: 18b1161ec9eeab8927f91405bca0ddf59a4a26c9
commit-date: 2024-10-02
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

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 by running the provided Rust reproducer with the unnecessary_cast lint enabled and compare the diagnostics for f1 and f2. Trace the lint's handling of the local and parameter expressions, then ensure the reproducer produces two lint warnings without changing the intended lint behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.