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
11 changes: 11 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,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() {}
11 changes: 11 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,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() {}
11 changes: 11 additions & 0 deletions tests/ui/let_and_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
}
}
Comment on lines +275 to +283
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't test the lint. The function body should be:

let x = Clone::clone(&x);
x

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I'll try to get to this asap.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that doesn't work.

struct S;
trait T {}
impl T for S {}

fn f(x: *const S) -> *const dyn T {
    Clone::clone(&x) as _
}

Doesn't actually create an error, so the lint was valid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That not cause a lint warning is why you need to change the test. Right now the test contains what the code should be after rustfix runs, but the test needs to make sure we don't make a broken suggestion. Hence you need to change the function's body to what I said earlier so that it will actually trigger let_and_return.

Copy link
Author

@ErinvanderVeen ErinvanderVeen Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That, I understand, but I'd love to be able to reproduce the error from the original issue in the test. This test doesn't have that error.


fn main() {}