Skip to content

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

Closed
@shepmaster

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)

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