Skip to content

Commit ba90993

Browse files
ErinvanderVeenErin van der Veen
authored andcommitted
fix: let-and-return suggests invalid cast
1 parent 432dad4 commit ba90993

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

clippy_lints/src/returns/let_and_return.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ pub(super) fn check_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'_>)
4545
} else {
4646
format!("({src})")
4747
}
48-
} else if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
48+
} else if !cx.typeck_results().expr_adjustments(retexpr).is_empty()
49+
// Do not suggest 'as _' for raw pointers; it's an invalid cast
50+
&& !cx.typeck_results().expr_ty_adjusted(retexpr).is_raw_ptr()
51+
{
4952
if has_enclosing_paren(&src) {
5053
format!("{src} as _")
5154
} else {

tests/ui/let_and_return.edition2021.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,15 @@ fn issue15987() -> i32 {
271271
r
272272
}
273273

274+
// https://github.com/rust-lang/rust-clippy/issues/16135
275+
mod issue16135 {
276+
struct S;
277+
trait T {}
278+
impl T for S {}
279+
280+
fn f(x: *const S) -> *const dyn T {
281+
Clone::clone(&x) as _
282+
}
283+
}
284+
274285
fn main() {}

tests/ui/let_and_return.edition2024.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,15 @@ fn issue15987() -> i32 {
271271
r
272272
}
273273

274+
// https://github.com/rust-lang/rust-clippy/issues/16135
275+
mod issue16135 {
276+
struct S;
277+
trait T {}
278+
impl T for S {}
279+
280+
fn f(x: *const S) -> *const dyn T {
281+
Clone::clone(&x) as _
282+
}
283+
}
284+
274285
fn main() {}

tests/ui/let_and_return.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,15 @@ fn issue15987() -> i32 {
271271
r
272272
}
273273

274+
// https://github.com/rust-lang/rust-clippy/issues/16135
275+
mod issue16135 {
276+
struct S;
277+
trait T {}
278+
impl T for S {}
279+
280+
fn f(x: *const S) -> *const dyn T {
281+
Clone::clone(&x) as _
282+
}
283+
}
284+
274285
fn main() {}

0 commit comments

Comments
 (0)