Skip to content

E0308 .as_deref() suggestion is asymmetric #114087

Open
@aticu

Description

@aticu

Code

fn main() {
    let a = Some(String::from("foo"));
    let b = Some("foo");
    if a == b {
        println!("do stuff");
    }
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:4:13
  |
4 |     if a == b {
  |             ^ expected `Option<String>`, found `Option<&str>`
  |
  = note: expected enum `Option<String>`
             found enum `Option<&str>`

Desired output

error[E0308]: mismatched types
 --> src/main.rs:4:13
  |
4 |     if a == b {
  |             ^ expected `Option<String>`, found `Option<&str>`
  |
  = note: expected enum `Option<String>`
             found enum `Option<&str>`
help: try using `.as_deref()` to convert `Option<String>` to `Option<&str>`
  |
4 |     if a.as_deref() == b {
  |         +++++++++++

Rationale and extra context

This is an obvious suggestion that is already implemented, but only checked for when the order of a and b is swapped.

Other cases

.as_deref() is suggested correctly when the order of a and b in the comparison is swapped.

Anything else?

A fix for this would likely also fix #102063, as this issue is closely related.

@clubby789 maybe you're interested in working on this? It's related to #114050, which you fixed yesterday (thanks for that by the way).

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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