Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clippy_lints/src/returns/let_and_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/let_and_return.edition2021.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
16 changes: 15 additions & 1 deletion tests/ui/let_and_return.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

8 changes: 8 additions & 0 deletions tests/ui/let_and_return.edition2024.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
16 changes: 15 additions & 1 deletion tests/ui/let_and_return.edition2024.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

8 changes: 8 additions & 0 deletions tests/ui/let_and_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}