Skip to content

Incomplete suggestion combines poorly with other tools (rust-analyzer) #8739

Closed
@Crzyrndm

Description

@Crzyrndm

Summary

Summary:

  • clippy::while_let_loop suggests a replacement where the body of the if is not included. Note that cargo clippy --fix doesn't update anything
  • rust-analyzer sees the suggestion and ends up removing the body of the if when user doesn't fully comprehend the suggested action

Unknown if while_let_loop is the only clippy lint to include this behaviour (incomplete suggested replacement)

rust-lang/rust-analyzer#12069

Some clippy replacements are incomplete. They provide a template for the suggested output, but direct copy paste will not give equivalent/improved behaviour. Some sections are left to the user to fill in

A simple example is the clippy::while_let_loop lint (I feel like it's not the only one, but perhaps I just keep running into the same issue)
image
clippy correctly identifies this as an opportunity to suggest a while-let loop
image
Note that the internals of the loop is left to the the user in the suggested replacement. This is where things become problematic

RA picks up the suggested replacement
image
And now any code that was in the body of the if is gone
image

Reproducer

Using vscode + rust-analyzer with clippy suggestions
I tried this code:

pub fn loop_lint(x: Option<i32>) {
    loop {
        if let Some(_i) = x {
            // many lines of work
        } else {
            break;
        }
    }
}

I expected to see this happen:

pub fn loop_lint(x: Option<i32>) {
    while let Some(_i) = x {
        // many lines of work
    }
}

Instead, this happened:

pub fn loop_lint(x: Option<i32>) {
    while let Some(_i) = x { .. }
}

Version

rustc --version
rustc 1.60.0 (7737e0b5c 2022-04-04)

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions