Skip to content

"consider dereferencing here" help doesn't account for operator precedence of the deref operator #105429

Closed

Description

@shepmaster

Given the following code:

struct SourceStruct;
struct TargetStruct;

impl From<SourceStruct> for TargetStruct {
    fn from(unchecked: SourceStruct) -> Self {
        TargetStruct
    }
}

fn main() {
    let a = &SourceStruct;
    let b: TargetStruct = a.into();
}

The current output is:

error[E0277]: the trait bound `TargetStruct: From<&SourceStruct>` is not satisfied
  --> src/main.rs:12:27
   |
12 |     let b: TargetStruct = a.into();
   |                           ^ ---- required by a bound introduced by this call
   |                           |
   |                           the trait `From<&SourceStruct>` is not implemented for `TargetStruct`
   |
   = note: required for `&SourceStruct` to implement `Into<TargetStruct>`
help: consider dereferencing here
   |
12 |     let b: TargetStruct = *a.into();
   |                           +

Ideally the output should look like:

help: consider dereferencing here
   |
12 |     let b: TargetStruct = (*a).into();
   |                           ++ +

The current suggestion will apply the dereference after the method call to .into(), which is not what is needed.

Really, that suggestion wouldn't even be suggested because the result also doesn't work, but that might be a bigger change.

Seen in 1.67.0-nightly (2022-12-06 b28d30e)

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 7, 2022
  2. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    and removed
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Dec 7, 2022
  3. self-assigned this
    on Dec 12, 2022
  4. added a commit that references this issue on Dec 14, 2022
    939880a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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