Skip to content

let-and-return suggests invalid cast #16135

@matthiaskrgr

Description

@matthiaskrgr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions