Skip to content

Improve manual_strip fix suggestion #14183

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

Closed
nyurik opened this issue Feb 9, 2025 · 0 comments · Fixed by #14188
Closed

Improve manual_strip fix suggestion #14183

nyurik opened this issue Feb 9, 2025 · 0 comments · Fixed by #14188

Comments

@nyurik
Copy link
Contributor

nyurik commented Feb 9, 2025

fn main() {
    let token = "-lfoo";
    if token.starts_with("-l") {
        let libname = &token[2..];
        println!("Library name: {libname}");
    }
}

The above code generates this warning with a suggestion:

Image

Note how the new code uses <stripped> instead of a real variable name. This might be a bit confusing, especially to the new users. I propose to add a bit more extra logic for this common case:

  • if the result is assigned to a new variable, use that binding's name directly in the Some expression

This will modify the suggestion from

if let Some(<stripped>) = token.strip_prefix("-l") {
   let libname = <stripped>;

to this

if let Some(libname) = token.strip_prefix("-l") {
github-merge-queue bot pushed a commit that referenced this issue Feb 26, 2025
When the manually stripped entity receives a name as the first use
through a simple `let` statement, this name can be used in the generated
`if let Some(…)` expression instead of a placeholder.

Fix #14183

changelog: [`manual_strip`]: reuse existing identifier in suggestion
when possible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant