Rust-analyzer: If applicable, use `Self` when filling match arms

Created on 29 Oct 2020  路  3Comments  路  Source: rust-analyzer/rust-analyzer

Given an enum and a match expression with a scrutinee of type Self inside of a method,
the assist fill_match_arms currently does not use Self but the actual name of the enum as
the first segment of paths referring to the variants.

I am pretty sure this is not idiomatic Rust code. However, I do not have any source at hand that backs this up.
Still, this is comparable to the general preference of Self in constructors.
Consider: fn new() -> Self { Self { /* .. */ } }.

For example presently, code after applying the assist might look like the following:

impl Choice {
    fn dummy(self) {
        match self {
            Choice::Yes => {}
            Choice::No => {}
        }
    }
}

But ideally, it should look like:

impl Choice {
    fn dummy(self) {
        match self {
            Self::Yes => {}
            Self::No => {}
        }
    }
}

Of course, this should apply recursively and even if a scrutinee is not syntactically Self like in fn dummy(self, other: Choice).

S-unactionable

Most helpful comment

1.37 is more than a year ago. I would propose to make this default and allow people config option to opt-out.

All 3 comments

I don't think either of that is more idiomatic, given that referring to enum variants through type aliases(Self in this case) was only stabilized in 1.37. It would maybe make sense to make this a configuration though?

Well, the fact alone that it was added to the language recently might indicate its idiomaticness but sure, it might do as a config option, too.

1.37 is more than a year ago. I would propose to make this default and allow people config option to opt-out.

Was this page helpful?
0 / 5 - 0 ratings