From 56be9c3ad544e8a0c2ade99a6b0060230f8b43ad Mon Sep 17 00:00:00 2001 From: Erin van der Veen Date: Wed, 26 Nov 2025 16:19:54 +0100 Subject: [PATCH] fix: `let-and-return` suggests invalid cast --- clippy_lints/src/returns/let_and_return.rs | 5 ++++- tests/ui/let_and_return.edition2021.fixed | 8 ++++++++ tests/ui/let_and_return.edition2021.stderr | 16 +++++++++++++++- tests/ui/let_and_return.edition2024.fixed | 8 ++++++++ tests/ui/let_and_return.edition2024.stderr | 16 +++++++++++++++- tests/ui/let_and_return.rs | 8 ++++++++ 6 files changed, 58 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/returns/let_and_return.rs b/clippy_lints/src/returns/let_and_return.rs index 0a00981e15be..7029c22b7e57 100644 --- a/clippy_lints/src/returns/let_and_return.rs +++ b/clippy_lints/src/returns/let_and_return.rs @@ -45,7 +45,10 @@ pub(super) fn check_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'_>) } else { format!("({src})") } - } else if !cx.typeck_results().expr_adjustments(retexpr).is_empty() { + } else if !cx.typeck_results().expr_adjustments(retexpr).is_empty() + // Do not suggest 'as _' for raw pointers; it's an invalid cast + && !cx.typeck_results().expr_ty_adjusted(retexpr).is_raw_ptr() + { if has_enclosing_paren(&src) { format!("{src} as _") } else { diff --git a/tests/ui/let_and_return.edition2021.fixed b/tests/ui/let_and_return.edition2021.fixed index e89e4476bf82..00effc5015ff 100644 --- a/tests/ui/let_and_return.edition2021.fixed +++ b/tests/ui/let_and_return.edition2021.fixed @@ -271,4 +271,12 @@ fn issue15987() -> i32 { r } +// https://github.com/rust-lang/rust-clippy/issues/16135 +fn issue16135() -> *const u8 { + let boxed_value = Box::new(42u8); + + Box::into_raw(boxed_value) + //~^ let_and_return +} + fn main() {} diff --git a/tests/ui/let_and_return.edition2021.stderr b/tests/ui/let_and_return.edition2021.stderr index f9536d1b5477..6253a5f8b12f 100644 --- a/tests/ui/let_and_return.edition2021.stderr +++ b/tests/ui/let_and_return.edition2021.stderr @@ -148,5 +148,19 @@ LL ~ LL ~ ({ true } || { false } && { 2 <= 3 }) | -error: aborting due to 10 previous errors +error: returning the result of a `let` binding from a block + --> tests/ui/let_and_return.rs:278:5 + | +LL | let ptr = Box::into_raw(boxed_value); + | ------------------------------------- unnecessary `let` binding +LL | ptr + | ^^^ + | +help: return the expression directly + | +LL ~ +LL ~ Box::into_raw(boxed_value) + | + +error: aborting due to 11 previous errors diff --git a/tests/ui/let_and_return.edition2024.fixed b/tests/ui/let_and_return.edition2024.fixed index d2c76673ca03..e50ba0f1ba16 100644 --- a/tests/ui/let_and_return.edition2024.fixed +++ b/tests/ui/let_and_return.edition2024.fixed @@ -271,4 +271,12 @@ fn issue15987() -> i32 { r } +// https://github.com/rust-lang/rust-clippy/issues/16135 +fn issue16135() -> *const u8 { + let boxed_value = Box::new(42u8); + + Box::into_raw(boxed_value) + //~^ let_and_return +} + fn main() {} diff --git a/tests/ui/let_and_return.edition2024.stderr b/tests/ui/let_and_return.edition2024.stderr index ca378fa43232..c669a17bc689 100644 --- a/tests/ui/let_and_return.edition2024.stderr +++ b/tests/ui/let_and_return.edition2024.stderr @@ -224,5 +224,19 @@ LL + None => Ok(Ok(0)), LL + }? | -error: aborting due to 15 previous errors +error: returning the result of a `let` binding from a block + --> tests/ui/let_and_return.rs:278:5 + | +LL | let ptr = Box::into_raw(boxed_value); + | ------------------------------------- unnecessary `let` binding +LL | ptr + | ^^^ + | +help: return the expression directly + | +LL ~ +LL ~ Box::into_raw(boxed_value) + | + +error: aborting due to 16 previous errors diff --git a/tests/ui/let_and_return.rs b/tests/ui/let_and_return.rs index 1af5f8ba5c16..be07b7d3b0e7 100644 --- a/tests/ui/let_and_return.rs +++ b/tests/ui/let_and_return.rs @@ -271,4 +271,12 @@ fn issue15987() -> i32 { r } +// https://github.com/rust-lang/rust-clippy/issues/16135 +fn issue16135() -> *const u8 { + let boxed_value = Box::new(42u8); + let ptr = Box::into_raw(boxed_value); + ptr + //~^ let_and_return +} + fn main() {}