Skip to content

Renaming the trait method also renames fields in impls that have the same name #14014

@popzxc

Description

@popzxc

rust-analyzer version: 0.3.1377-standalone (daa0138 2023-01-21)
rustc version: rustc 1.66.1 (90743e729 2023-01-10)

If you use the rename functionality on the trait method, and one of the implementations of the trait has the field with the same name, the field name would also be renamed. Moreover, field is renamed only in the structure itself, but not in places where the field is accessed.

Minimal reproducible example

Use the following code and use the rename functionality (F2 in VS code) to change the method name to something_else.

trait Foo {
    fn method(&self) -> u8;
}

struct Bar {
    method: u8,
}

impl Foo for Bar {
    fn method(&self) -> u8 {
        self.method
    }
}

Observed result

trait Foo {
    fn something_else(&self) -> u8;
}

struct Bar {
    something_else: u8, // <- Changed from `method` to `something_else.`
}

impl Foo for Bar {
    fn something_else(&self) -> u8 {
        self.method // <- Still attempts to access `method` field.
    }
}

Expected result

trait Foo {
    fn something_else(&self) -> u8;
}

struct Bar {
    method: u8, // <- not renamed
}

impl Foo for Bar {
    fn something_else(&self) -> u8 {
        self.method
    }
}

Live demo

RA_trait_method_rename_issue.mov

Metadata

Metadata

Assignees

Labels

A-idegeneral IDE featuresC-bugCategory: bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions