Skip to content

Commit 8357a98

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

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,17 @@ 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: &S) -> Box<dyn T + '_> {
281+
282+
(Box::new(Clone::clone(&x))) as _
283+
//~^ let_and_return
284+
}
285+
}
286+
274287
fn main() {}

tests/ui/let_and_return.edition2021.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,19 @@ LL ~
148148
LL ~ ({ true } || { false } && { 2 <= 3 })
149149
|
150150

151-
error: aborting due to 10 previous errors
151+
error: returning the result of a `let` binding from a block
152+
--> tests/ui/let_and_return.rs:282:9
153+
|
154+
LL | let x = Box::new(Clone::clone(&x));
155+
| ----------------------------------- unnecessary `let` binding
156+
LL | x
157+
| ^
158+
|
159+
help: return the expression directly
160+
|
161+
LL ~
162+
LL ~ (Box::new(Clone::clone(&x))) as _
163+
|
164+
165+
error: aborting due to 11 previous errors
152166

tests/ui/let_and_return.edition2024.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,17 @@ 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: &S) -> Box<dyn T + '_> {
281+
282+
(Box::new(Clone::clone(&x))) as _
283+
//~^ let_and_return
284+
}
285+
}
286+
274287
fn main() {}

tests/ui/let_and_return.edition2024.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,19 @@ LL + None => Ok(Ok(0)),
224224
LL + }?
225225
|
226226

227-
error: aborting due to 15 previous errors
227+
error: returning the result of a `let` binding from a block
228+
--> tests/ui/let_and_return.rs:282:9
229+
|
230+
LL | let x = Box::new(Clone::clone(&x));
231+
| ----------------------------------- unnecessary `let` binding
232+
LL | x
233+
| ^
234+
|
235+
help: return the expression directly
236+
|
237+
LL ~
238+
LL ~ (Box::new(Clone::clone(&x))) as _
239+
|
240+
241+
error: aborting due to 16 previous errors
228242

tests/ui/let_and_return.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,17 @@ 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: &S) -> Box<dyn T + '_> {
281+
let x = Box::new(Clone::clone(&x));
282+
x
283+
//~^ let_and_return
284+
}
285+
}
286+
274287
fn main() {}

0 commit comments

Comments
 (0)