Skip to content

Commit 56be9c3

Browse files
fix: let-and-return suggests invalid cast
1 parent 432dad4 commit 56be9c3

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,12 @@ fn issue15987() -> i32 {
271271
r
272272
}
273273

274+
// https://github.com/rust-lang/rust-clippy/issues/16135
275+
fn issue16135() -> *const u8 {
276+
let boxed_value = Box::new(42u8);
277+
278+
Box::into_raw(boxed_value)
279+
//~^ let_and_return
280+
}
281+
274282
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:278:5
153+
|
154+
LL | let ptr = Box::into_raw(boxed_value);
155+
| ------------------------------------- unnecessary `let` binding
156+
LL | ptr
157+
| ^^^
158+
|
159+
help: return the expression directly
160+
|
161+
LL ~
162+
LL ~ Box::into_raw(boxed_value)
163+
|
164+
165+
error: aborting due to 11 previous errors
152166

tests/ui/let_and_return.edition2024.fixed

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

274+
// https://github.com/rust-lang/rust-clippy/issues/16135
275+
fn issue16135() -> *const u8 {
276+
let boxed_value = Box::new(42u8);
277+
278+
Box::into_raw(boxed_value)
279+
//~^ let_and_return
280+
}
281+
274282
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:278:5
229+
|
230+
LL | let ptr = Box::into_raw(boxed_value);
231+
| ------------------------------------- unnecessary `let` binding
232+
LL | ptr
233+
| ^^^
234+
|
235+
help: return the expression directly
236+
|
237+
LL ~
238+
LL ~ Box::into_raw(boxed_value)
239+
|
240+
241+
error: aborting due to 16 previous errors
228242

tests/ui/let_and_return.rs

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

274+
// https://github.com/rust-lang/rust-clippy/issues/16135
275+
fn issue16135() -> *const u8 {
276+
let boxed_value = Box::new(42u8);
277+
let ptr = Box::into_raw(boxed_value);
278+
ptr
279+
//~^ let_and_return
280+
}
281+
274282
fn main() {}

0 commit comments

Comments
 (0)