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..6412d4884a8a 100644 --- a/tests/ui/let_and_return.edition2021.fixed +++ b/tests/ui/let_and_return.edition2021.fixed @@ -271,4 +271,15 @@ fn issue15987() -> i32 { r } +// https://github.com/rust-lang/rust-clippy/issues/16135 +mod issue16135 { + struct S; + trait T {} + impl T for S {} + + fn f(x: *const S) -> *const dyn T { + Clone::clone(&x) as _ + } +} + fn main() {} diff --git a/tests/ui/let_and_return.edition2024.fixed b/tests/ui/let_and_return.edition2024.fixed index d2c76673ca03..c96d7fc3bad4 100644 --- a/tests/ui/let_and_return.edition2024.fixed +++ b/tests/ui/let_and_return.edition2024.fixed @@ -271,4 +271,15 @@ fn issue15987() -> i32 { r } +// https://github.com/rust-lang/rust-clippy/issues/16135 +mod issue16135 { + struct S; + trait T {} + impl T for S {} + + fn f(x: *const S) -> *const dyn T { + Clone::clone(&x) as _ + } +} + fn main() {} diff --git a/tests/ui/let_and_return.rs b/tests/ui/let_and_return.rs index 1af5f8ba5c16..a7d67aa48e3e 100644 --- a/tests/ui/let_and_return.rs +++ b/tests/ui/let_and_return.rs @@ -271,4 +271,15 @@ fn issue15987() -> i32 { r } +// https://github.com/rust-lang/rust-clippy/issues/16135 +mod issue16135 { + struct S; + trait T {} + impl T for S {} + + fn f(x: *const S) -> *const dyn T { + Clone::clone(&x) as _ + } +} + fn main() {}