Skip to content

Unboxing suggestion does not add parentheses where needed #132924

Closed
@uellenberg

Description

@uellenberg

Code

use std::sync::Arc;
fn main() {
    let x = Box::new(Some(1));

    let test: Option<i32> = x as Box<Option<i32>>;
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:5:29
  |
5 |     let test: Option<i32> = x as Box<Option<i32>>;
  |               -----------   ^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Box<Option<i32>>`
  |               |
  |               expected due to this
  |
  = note: expected enum `Option<_>`
           found struct `Box<Option<_>>`
help: consider unboxing the value
  |
5 |     let test: Option<i32> = *x as Box<Option<i32>>;
  |                             +

Desired output

error[E0308]: mismatched types
 --> src/main.rs:5:29
  |
5 |     let test: Option<i32> = x as Box<Option<i32>>;
  |               -----------   ^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Box<Option<i32>>`
  |               |
  |               expected due to this
  |
  = note: expected enum `Option<_>`
           found struct `Box<Option<_>>`
help: consider unboxing the value
  |
5 |     let test: Option<i32> = *(x as Box<Option<i32>>);
  |                             ++                     +

Rationale and extra context

The current output is incorrect, and leads to the same suggestion over and over again if we keep applying it. The relevant code is

.

See

let needs_parens = match expr.kind {
for an example of adding parentheses.

Other cases

Rust Version

rustc 1.84.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.84.0-dev
LLVM version: 19.1.3

----

Tested on commit 42b24963202f31d417a972e56e48a17e916b279b

Anything else?

No response

Metadata

Metadata

Assignees

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