Skip to content

E0308 .as_deref() suggestion is asymmetric #114087

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

Open
aticu opened this issue Jul 26, 2023 · 1 comment
Open

E0308 .as_deref() suggestion is asymmetric #114087

aticu opened this issue Jul 26, 2023 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@aticu
Copy link
Contributor

aticu commented Jul 26, 2023

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).

@aticu aticu added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 26, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 26, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 26, 2023
@mucinoab
Copy link
Contributor

mucinoab commented Aug 2, 2023

@rustbot claim
I will try to look into this over the weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants