Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofix for needless_return with unsafe block produces invalid code #12157

Open
harrysarson-signaloid opened this issue Jan 16, 2024 · 0 comments · May be fixed by #14094
Open

Autofix for needless_return with unsafe block produces invalid code #12157

harrysarson-signaloid opened this issue Jan 16, 2024 · 0 comments · May be fixed by #14094
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@harrysarson-signaloid
Copy link

Summary

Running clippy on the below code suggests

warning: unneeded `return` statement
 --> src/main.rs:9:5
  |
9 |     return unsafe {f1()} as *const i32;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
  = note: `#[warn(clippy::needless_return)]` on by default
help: remove `return`
  |
9 -     return unsafe {f1()} as *const i32;
9 +     unsafe {f1()} as *const i32
  |

but when doing autofixing I get:

after fixes were automatically applied the compiler reported errors within these files:

  * xxx

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:

error: expected expression, found `as`
  --> xxx
   |
42 |     unsafe {f1()} as *const i32
   |                   ^^ expected expression
   |
help: parentheses are required to parse this as an expression
   |
42 |     (unsafe {f1()}) as *const i32
   |     +

Reproducer

I tried this code:

unsafe fn f1() -> *const u8 {
    todo!()
}

pub unsafe fn f2() -> *const i32 {
    return unsafe {f1()} as *const i32;
}

fn main() {
    println!("Hello, world!");
}

I expected to see this happen: clippy --fix work

Instead, this happened: Error

Version

rustc 1.77.0-nightly (714b29a17 2024-01-15)
binary: rustc
commit-hash: 714b29a17ff5fa727c794bbb60bfd335f8e75d42
commit-date: 2024-01-15
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

Additional Labels

No response

@harrysarson-signaloid harrysarson-signaloid added the C-bug Category: Clippy is not doing the correct thing label Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant