rust-lang / rust-lang/rust-clippy

`let-and-return` suggests invalid cast

Open
#16,135 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Using the following flags

--force-warn clippy::let-and-return

this code:

fn bindings() -> *const u8 {
    let x = 0;
    let x = &x;
    x
}

pub fn main() {}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.LTN5gk3lY4Yf/icemaker_clippyfix_tempdir.VkBZzezo6BF0/_a)
warning: returning the result of a `let` binding from a block
 --> src/main.rs:4:5
  |
3 |     let x = &x;
  |     ----------- unnecessary `let` binding
4 |     x
  |     ^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
  = note: requested on the command line with `--force-warn clippy::let-and-return`
help: return the expression directly
  |
3 ~     
4 ~     (&x) as _
  |

warning: `_a` (bin "_a") generated 1 warning (run `cargo clippy --fix --bin "_a" -p _a` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s

However after applying these diagnostics, the resulting code:

fn bindings() -> *const u8 {
    let x = 0;
    
    (&x) as _
}

pub fn main() {}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.LTN5gk3lY4Yf/icemaker_clippyfix_tempdir.VkBZzezo6BF0/_a)
error[E0606]: casting `&i32` as `*const u8` is invalid
 --> src/main.rs:4:5
  |
4 |     (&x) as _
  |     ^^^^^^^^^

For more information about this error, try `rustc --explain E0606`.
error: could not compile `_a` (bin "_a") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a" test) due to 1 previous error

Version:

rustc 1.93.0-nightly (94b49fd99 2025-11-22)
binary: rustc
commit-hash: 94b49fd998d6723e0a9240a7cff5f9df37b84dd8
commit-date: 2025-11-22
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5

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 reproducing the provided let-and-return example with cargo clippy --fix and confirm that the suggested cast fails to compile. Trace the lint's suggestion generation and add a regression case showing that the fix must preserve compilation for this pointer-returning function.

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
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.