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

FP empty_structs_with_brackets #9887

Closed
matthiaskrgr opened this issue Nov 19, 2022 · 1 comment · Fixed by #10141
Closed

FP empty_structs_with_brackets #9887

matthiaskrgr opened this issue Nov 19, 2022 · 1 comment · Fixed by #10141
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Nov 19, 2022

Summary

.

Lint Name

empty_structs_with_brackets.

Reproducer

I tried this code:

#![feature(box_syntax, fn_traits, unboxed_closures)]

struct GreetStruct2();

impl FnOnce<(&str,)> for GreetStruct2 {
    type Output = ();

    extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
        println!("hello {}", who);
    }
}


fn main() {
    GreetStruct2()("world");
}

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::empty_structs_with_brackets

    Checking a v0.1.0 (/tmp/a)
warning: failed to automatically apply fixes suggested by rustc to crate `a`

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[E0057]: this struct takes 1 argument but 0 arguments were supplied
  --> src/main.rs:15:5
   |
15 |     GreetStruct2()("world");
   |     ^^^^^^^^^^^^-- an argument of type `&str` is missing
   |
help: provide the argument
   |
15 |     GreetStruct2(/* &str */)("world");
   |                 ~~~~~~~~~~~~

error[E0618]: expected function, found `()`
  --> src/main.rs:15:5
   |
3  | struct GreetStruct2;
   | ------------------- `GreetStruct2` defined here returns `()`
...
15 |     GreetStruct2()("world");
   |     ^^^^^^^^^^^^^^---------
   |     |
   |     call expression requires function

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0057, E0618.
For more information about an error, try `rustc --explain E0057`.
Original diagnostics will follow.

warning: found empty brackets on struct declaration
 --> src/main.rs:3:20
  |
3 | struct GreetStruct2();
  |                    ^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_structs_with_brackets
  = note: requested on the command line with `-W clippy::empty-structs-with-brackets`
  = help: remove the brackets

warning: `a` (bin "a") generated 1 warning (run `cargo fix --bin "a"` to apply 1 suggestion)
warning: `a` (bin "a" test) generated 1 warning (1 duplicate)

Version

rustc 1.67.0-nightly (b833ad56f 2022-11-18)
binary: rustc
commit-hash: b833ad56f46a0bbe0e8729512812a161e7dae28a
commit-date: 2022-11-18
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Nov 19, 2022
@FoseFx
Copy link
Contributor

FoseFx commented Jan 1, 2023

This is not a false positive.

But indeed, the fix breaks the code, as soon as the struct is used. I don't know how it never occurred to me when writing this lint, but when the fix changes the struct, all usages of it must be changed as well.

Here is a minimal reproducer.
#![warn(clippy::empty_structs_with_brackets)]
struct S();

fn main() {
    let _ = S();
}

$ RUST_LOG=trace cargo +nightly clippy --fix

    Checking someproj v0.1.0 (/tmp/someproj)
warning: failed to automatically apply fixes suggested by rustc to crate `someproj`

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[E0618]: expected function, found struct `S`
 --> src/main.rs:5:13
  |
2 | struct S;
  | -------- struct `S` defined here
...
5 |     let _ = S();
  |             ^--
  |             |
  |             call expression requires function
  |
help: `S` is a unit struct, and does not take parentheses to be constructed
  |
5 -     let _ = S();
5 +     let _ = S;
  |

error: aborting due to previous error

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

warning: found empty brackets on struct declaration
 --> src/main.rs:2:9
  |
2 | struct S();
  |         ^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::empty_structs_with_brackets)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_structs_with_brackets
  = help: remove the brackets

warning: `someproj` (bin "someproj") generated 1 warning (1 duplicate)
warning: `someproj` (bin "someproj" test) generated 1 warning

While the actual replacement of these usages is probably quite trivial, I suspect tracking them down in the first place is not. Please let me know if there is any other lint that does changes like this for me to look at, but for now I think it's best to simply downgrade the suggestion from MachineApplicable to Unspecified and let the user figure it out.

@rustbot claim

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 I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants