Skip to content
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

Improve automatic name selection for "Extract into variable" on from() calls #17631

Open
kpreid opened this issue Jul 18, 2024 · 6 comments
Open
Labels
A-assists C-feature Category: feature request E-easy

Comments

@kpreid
Copy link
Contributor

kpreid commented Jul 18, 2024

// Stubs for demo
struct Foo {}
struct Bar {}
impl From<Foo> for Bar {
    fn from(_: Foo) -> Self {
        Bar {}
    }
}
fn do_something(_: Bar) {}

// Code of interest
fn main() {
    let foo = Foo {};
    do_something(Bar::from(foo));
    //           ^^^^^^^^^^^^^^ select this and extract into variable
}

The result of extraction is:

    let from = Bar::from(foo);
    do_something(from);

A better result would be:

    let bar = Bar::from(foo);
    do_something(bar);

.into() calls also currently produce variables named into, but that might be more trouble to fix; I don't know.

rust-analyzer version: 0.3.2037-standalone (e9afba5 2024-07-14)

@kpreid kpreid added the C-feature Category: feature request label Jul 18, 2024
@lnicola
Copy link
Member

lnicola commented Jul 19, 2024

@roife roife added the E-easy label Jul 19, 2024
@Lampese
Copy link

Lampese commented Jul 19, 2024

@rustbot claim

@Lampese
Copy link

Lampese commented Jul 19, 2024

@kpreid How do you think it would be better to handle this situation?

use std::slice::Iter;

struct Foo;

impl From<Foo> for fn(&Iter<'_, i32>) -> *mut i32 {
    fn from(_: Foo) -> Self {
        todo!()
    }
}

@Fancyflame
Copy link

@Lampese I guess what he wants is that if Self is a path type, the variable name is the type name, otherwise fall back to from.
But personally, I think it is not conducive to unified management.

@kpreid
Copy link
Contributor Author

kpreid commented Jul 19, 2024

Yes. Things like this can never be perfect; they can only handle common cases better.

Speaking of common cases, here's another one not using the From trait at all:

struct Foo {}
struct Bar {}
impl Bar {
    pub fn from_foo(_: Foo) -> Self {
        Self {}
    }
}
fn do_something(_: Bar) {}

fn main() {
    let foo = Foo {};
    do_something(Bar::from_foo(foo));
    //           ^^^^^^^^^^^^^^^^^^ select this and extract into variable
}

This generates the variable name from_foo, but, similarly, bar would usually be better. Since from_* constructor functions are part of Rust idiom, I think it would be good if rust-analyzer recognized those too, in the same way. That is, if there is a call of the syntactic form *::$1::from_*(*) or *::$1::from(*), and $1 is a type name, then $1 should be (snake-cased and) used as the suggested variable name. No need to care whether the From trait is involved at all.

@Lampese Lampese removed their assignment Jul 20, 2024
@joshka
Copy link
Contributor

joshka commented Jul 28, 2024

with_ seems like a reasonable addition to this to be handled in the same way as from_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-feature Category: feature request E-easy
Projects
None yet
Development

No branches or pull requests

6 participants