rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.38k stars 1.54k forks source link

`needless_return` false positive in match arm #10390

Open Thechi2000 opened 1 year ago

Thechi2000 commented 1 year ago

I ran cargo clippy --fix on my project, and it failed with the following output:

   Compiling uzurion-launcher v0.0.0 (/home/ludovic/Desktop/prog/react-uzurion-launcher/src-tauri)
warning: failed to automatically apply fixes suggested by rustc to crate `uzurion_launcher`

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

  * src/event.rs
  * src/login/microsoft/mod.rs

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/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:
warning: `crate` references the macro call's crate
  --> src/event.rs:29:13
   |
29 |             crate::consts::events::ERROR,
   |             ^^^^^ help: to reference the macro definition's crate, use: `$crate`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#crate_in_macro_def
   = note: `#[warn(clippy::crate_in_macro_def)]` on by default

error[E0308]: `match` arms have incompatible types
   --> src/event.rs:12:9
    |
12  | /         {
13  | |             let __event = $event;
14  | |             let __payload = $payload;
15  | |
...   |
19  | |             }
20  | |         }
    | |_________^ expected struct `tauri::Window`, found `()`
    |
   ::: src/login/microsoft/mod.rs:110:5
    |
110 | /     match WindowBuilder::new(&app, "microsoft-login", WindowUrl::External(url))
111 | |         .focused(true)
112 | |         .title("Microsoft login")
113 | |         .build()
    | |________________- `match` arms have incompatible types
114 |       {
115 |           Ok(w) => w,
    |                    - this is found to be of type `tauri::Window`
...
118 |               send_error!(app, "Cannot create window", e);
    |               ------------------------------------------- in this macro invocation
    |
    = note: expected struct `tauri::Window`
            found unit type `()`
    = note: this error originates in the macro `$crate::send_event` which comes from the expansion of the macro `send_error` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: `crate` references the macro call's crate
  --> src/event.rs:27:9
   |
27 |         crate::send_event!{
   |         ^^^^^ help: to reference the macro definition's crate, use: `$crate`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#crate_in_macro_def
   = note: `#[warn(clippy::crate_in_macro_def)]` on by default

warning: useless use of `format!`
  --> src/login/microsoft/requests.rs:52:47
   |
52 |     let content_type = HeaderValue::from_str(&format!("{}", "application/x-www-form-urlencoded")).expect("Header value creation bug");
   |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"application/x-www-form-urlencoded".to_string()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
   = note: `#[warn(clippy::useless_format)]` on by default

warning: unneeded `return` statement
  --> src/login/microsoft/mod.rs:63:25
   |
63 |                         return;
   |                         ^^^^^^
   |
   = 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`

warning: unneeded `return` statement
  --> src/login/microsoft/mod.rs:67:25
   |
67 |                         return;
   |                         ^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
   = help: remove `return`

warning: unneeded `return` statement
   --> src/login/microsoft/mod.rs:119:13
    |
119 |             return;
    |             ^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = help: remove `return`

warning: unneeded `return` statement
   --> src/login/microsoft/mod.rs:178:13
    |
178 |             return;
    |             ^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
    = help: remove `return`

warning: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
  --> src/routing/microsoft.rs:22:5
   |
22 |        app.get_window(windows::MICROSOFT_LOGIN).map(|w| {
   |   _____^
   |  |_____|
   | ||
23 | ||         if let Err(e) = w.close() {
24 | ||             error!("Could not close microsoft-login window: {:?}", e);
25 | ||         }
26 | ||     });
   | ||______^- help: try this: `if let Some(w) = app.get_window(windows::MICROSOFT_LOGIN) { ... }`
   | |_______|
   | 
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
   = note: `#[warn(clippy::option_map_unit_fn)]` on by default

warning: `uzurion-launcher` (bin "uzurion-launcher" test) generated 7 warnings (1 duplicate)
warning: `uzurion-launcher` (bin "uzurion-launcher") generated 7 warnings (6 duplicates)
    Finished dev [unoptimized + debuginfo] target(s) in 6.03s

Reproduction:

I was working on this project, on the commit e5087b1591032157c90d28e7925e5282df8456eb

Versions: rustup: 1.25.1 rustc: 1.66.0 cargo: 1.66.0 clippy: 0.1.66

Issue

The problem is that clippy considers that the return at src-tauri/src/login/microsoft/mod.rs:119 is unneeded, but it actually makes the function return at the end of the match arm, thus the arm does not need to return a Window<Wry>. When clippy removes it, the second arms return (), thus it is incompatible with the first one (which returns Window<Wry>).

kadiwa4 commented 1 year ago

Shrunk example:

fn func(input: u8) {
    match input {
        3 => 4,
        _ => {
            // side effect ...
            return;
        },
    };
}

Produces:

warning: unneeded `return` statement
 --> src/lib.rs:5:27
  |
5 |               // side effect ...
  |  _______________________________^
6 | |             return;
  | |__________________^
  |
  = 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`