rust-lang / rust-lang/rust-clippy

unit_arg breaks code

Open
#10,016 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

.

Lint Name

unit_arg

Reproducer

I tried this code:

#![allow(dead_code)]
#![allow(unused_assignments)]
#![allow(unused_variables)]
#![allow(unreachable_code)]

trait UnitDefault {
    fn default() -> Self;
}

impl UnitDefault for () {
    fn default() -> () {
        panic!()
    }
}

fn match_arm() {
    let _x = match Ok(UnitDefault::default()) {
        Ok(v) => v,
        Err(()) => return,
    };
}

fn match_arm_rev() {
    let _x = match Ok(UnitDefault::default()) {
        Err(()) => return,
        Ok(v) => v,
    };
}

fn main() {}

I saw this happen:

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

  * src/main.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:
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
  --> src/main.rs:18:9
   |
7  |     fn default() -> Self;
   |     --------------------- `UnitDefault::default` defined here
...
18 |         UnitDefault::default();
   |         ^^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait

error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
  --> src/main.rs:28:9
   |
7  |     fn default() -> Self;
   |     --------------------- `UnitDefault::default` defined here
...
28 |         UnitDefault::default();
   |         ^^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait

error: aborting due to 2 previous errors

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

warning: passing a unit value to a function
  --> src/main.rs:17:20
   |
17 |     let _x = match Ok(UnitDefault::default()) {
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
   = note: requested on the command line with `-W clippy::unit-arg`
help: move the expression in front of the call and replace it with the unit literal `()`
   |
17 ~     let _x = match {
18 +         UnitDefault::default();
19 +         Ok(())
20 ~     } {
   |

warning: passing a unit value to a function
  --> src/main.rs:24:20
   |
24 |     let _x = match Ok(UnitDefault::default()) {
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
help: move the expression in front of the call and replace it with the unit literal `()`
   |
24 ~     let _x = match {
25 +         UnitDefault::default();
26 +         Ok(())
27 ~     } {
   |
Version
rustc 1.67.0-nightly (c97b539e4 2022-11-30)
binary: rustc
commit-hash: c97b539e408ea353f4fde2f9251d598291fec421
commit-date: 2022-11-30
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the unit_arg lint issue using the Rust code in the report and observe the compiler errors in src/main.rs after automatic fixes are applied. Inspect the unit_arg lint's suggestion behavior; done when applying its suggestion no longer produces E0790 errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.