Skip to content

cargo clippy --fix generated broken code #9583

@rbtcollins

Description

@rbtcollins

Output from clippy.

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

  * examples/threads.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[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
  --> examples/threads.rs:61:32
   |
60 |       rt.block_on(async {
   |  _______________________-
61 | |         client.register().await?
   | |                                ^ cannot use the `?` operator in an async block that returns `()`
62 | |     })?;
   | |_____- this function should return `Result` or `Option` to accept `?`
   |
   = help: the trait `std::ops::FromResidual<std::result::Result<std::convert::Infallible, std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>>>` is not implemented for `()`

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
  --> examples/threads.rs:60:5
   |
60 | /     rt.block_on(async {
61 | |         client.register().await?
62 | |     })?;
   | |_______^ the `?` operator cannot be applied to type `()`
   |
   = help: the trait `std::ops::Try` is not implemented for `()`

error: aborting due to 2 previous errors

I would be happy if either the lint didn't fire, or if clippy fixed things with code that works.

Meta

The same error occurs with nightly.

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
rustc 1.66.0-nightly (57f097ea2 2022-10-01)

Activity

added
C-bugCategory: Clippy is not doing the correct thing
on Oct 3, 2022
transferred this issue fromrust-lang/ruston Oct 3, 2022
Alexendoo

Alexendoo commented on Oct 3, 2022

@Alexendoo
Member

Could you share the output of running cargo clippy without --fix, or the code this occurred on?

rbtcollins

rbtcollins commented on Oct 3, 2022

@rbtcollins
Author

The allow was added after this.

    rt.block_on(async {
        if let Err(e) = client.register().await {
            Err(e)
        } else {
            Ok(())
        }
    })?;

for permanence

added
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Oct 5, 2022
BenjaminBrienen

BenjaminBrienen commented on Jun 17, 2025

@BenjaminBrienen

I also get this.

warning: failed to automatically apply fixes suggested by rustc to crate `wgsl_analyzer`

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

  * crates/wgsl-analyzer/src/lsp/capabilities.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-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[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
   --> crates/wgsl-analyzer/src/lsp/capabilities.rs:142:34
    |
138 |     fn code_action_capabilities(&self) -> CodeActionProviderCapability {
    |     ------------------------------------------------------------------ this function should return `Result` or `Option` to accept `?`
...
142 |                         .as_ref()?;
    |                                  ^ cannot use the `?` operator in a method that returns `lsp_types::CodeActionProviderCapability`

error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
   --> crates/wgsl-analyzer/src/lsp/capabilities.rs:459:34
    |
455 |     pub fn inlay_hint_resolve_support_properties(&self) -> FxHashSet<&str> {
    |     ---------------------------------------------------------------------- this function should return `Result` or `Option` to accept `?`
...
459 |                         .as_ref()?;
    |                                  ^ cannot use the `?` operator in a method that returns `std::collections::HashSet<&str, rustc_hash::FxBuildHasher>`

error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `std::ops::FromResidual`)
   --> crates/wgsl-analyzer/src/lsp/capabilities.rs:474:34
    |
470 |     pub fn completion_resolve_support_properties(&self) -> FxHashSet<&str> {
    |     ---------------------------------------------------------------------- this function should return `Result` or `Option` to accept `?`
...
474 |                         .as_ref()?;
    |                                  ^ cannot use the `?` operator in a method that returns `std::collections::HashSet<&str, rustc_hash::FxBuildHasher>`

error: aborting due to 3 previous errors
blyxyas

blyxyas commented on Jun 19, 2025

@blyxyas
Member

This issue is currently not happening anymore in the repo's current state. Could you post an example of the problematic code @BenjaminBrienen? =^w^=

blyxyas

blyxyas commented on Jun 20, 2025

@blyxyas
Member

Minimized example of this behaviour:

fn a() -> bool {
    (|| {
        Some(true)
    })() == Some(true)
}

Which rustfix tries to change into:

fn a() -> bool {
    {
        Some(true)
    } == Some(true)
}

which produces:

error: expected expression, found `==`
 --> src/lib.rs:4:7
  |
4 |     } == Some(true)
  |       ^^ expected expression

error[E0308]: mismatched types
 --> src/lib.rs:3:9
  |
3 |         Some(true)
  |         ^^^^^^^^^^ expected `()`, found `Option<bool>`
  |
  = note: expected unit type `()`
                  found enum `std::option::Option<bool>`
odysa

odysa commented on Jun 26, 2025

@odysa
Contributor

@rustbot claim

odysa

odysa commented on Jun 26, 2025

@odysa
Contributor

I think the expected behavior is

(|| { Some(true) })() == Some(true)
// suggested as
Some(true) == Some(true)
odysa

odysa commented on Jun 26, 2025

@odysa
Contributor

Possible solution:
If the closure body is a block, and the block contains no statements but has a final expression, we can suggest replacing the entire closure with just that expression.
For example, (|| { Some(true) })() can be simplified to Some(true).

if let ExprKind::Block(block, _) = body.kind
     && block.stmts.is_empty()
     && let Some(expr) = block.expr {
		// suggest inner expr
}
added a commit that references this issue on Jul 6, 2025
2713c50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @rbtcollins@Alexendoo@odysa@BenjaminBrienen@dswij

    Issue actions

      cargo clippy --fix generated broken code · Issue #9583 · rust-lang/rust-clippy