Skip to content

Bad span for ? removal suggestion in parentheses #114392

Closed
@ehuss

Description

@ehuss

Code

fn foo() -> Option<()> {
    let x = Some(());
    (x?)
}

Current output

warning: unnecessary parentheses around block return value
 --> src/lib.rs:3:5
  |
3 |     (x?)
  |     ^  ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
3 -     (x?)
3 +     x?
  |

error[E0308]: `?` operator has incompatible types
 --> src/lib.rs:3:6
  |
3 |     (x?)
  |      ^^ expected `Option<()>`, found `()`
  |
  = note: `?` operator cannot convert from `()` to `Option<()>`
  = note:   expected enum `Option<()>`
          found unit type `()`
help: try removing this `?`
  |
3 -     (x?)
3 +     (x?
  |
help: try wrapping the expression in `Some`
  |
3 |     (Some(x?))
  |      +++++  +

For more information about this error, try `rustc --explain E0308`.

Desired output

...
help: try removing this `?`
  |
3 -     (x?)
3 +     (x)

Rationale and extra context

The suggestion span for removing the ? is off-by-one, and suggests removing the parenthesis instead of the ?.

Other cases

No response

Anything else?

rustc 1.73.0-nightly (7bd81ee 2023-07-13)
binary: rustc
commit-hash: 7bd81ee
commit-date: 2023-07-13
host: aarch64-apple-darwin
release: 1.73.0-nightly
LLVM version: 16.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions