Is there a way to replace lambda in rust-analyzer structural search and replace?

I want to change this code but I don't know how to replace the lambda.

fn main() {
    let a: Result<(), ()> = Ok(());
    let b: Result<(), ()> = a.map_err(|e| e.into());
}

to

fn main() {
    let a: Result<(), ()> = Ok(());
    let b: Result<(), ()> = a.map_err(Into::into);
}
Result::map_err($a, |$b|  Into::<()>::into($c) ) ==>> $a.map_err(Into::into)

This snippet returns the following error.

Is such replacement possible with ssr?

rust-analyzer version: 84be2eaf9 2022-05-23 stable

When using the methods of a trait, the trait must be in scope.

Give this a try:

fn main() {
    use std::convert::Into;

    let a: Result<(), ()> = Ok(());
    let b: Result<(), ()> = a.map_err(Into::into);
}

If that version works, you're likely still on an old Rust edition eg 2018.
That can be fixed by changing it in the Cargo.toml of the project:

[package]
edition = "2021"

Into has been in the prelude since 1.0, it's TryInto which was added in 2021, so that can't be the reason.

1 Like

Then something else entirely has to be wrong. The 2nd OP example compiles without modification on play.rust-lang.org.

$a.map_err(|$b| $c.into()) ==>> $a.map_err(Into::<()>::into)

Eventually it worked, but not quite what I expected.

fn main() {
    let a: Result<(), ()> = Ok(());
    let b: Result<(), ()> = a.map_err(Into::<()>::into);
    // let b: Result<(), ()> = b.map_err(Into::into); // expected
}

This one works too, but strangely.

$a.map_err(|$b| $c.into()) ==>> $a.map_err(Into::<>::into)
1 Like

I don't know about this one in particular, but Clippy can fix some trivial cases automatically, and e.g. changes map(|x| foo(x)) to map(foo) where possible.

cargo clippy --fix

It sounds like OP is trying to add such a fix to rust-analyzer.