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

New lint: calling trim() then split_whitespace() #8521

Closed
ghost opened this issue Mar 11, 2022 · 0 comments · Fixed by #8575
Closed

New lint: calling trim() then split_whitespace() #8521

ghost opened this issue Mar 11, 2022 · 0 comments · Fixed by #8575
Labels
A-lint Area: New lints

Comments

@ghost
Copy link

ghost commented Mar 11, 2022

What it does

Warns about calling str::trim (and variants) before str::split_whitespace (and variants). split_whitespace already ignores leading and trailing whitespace.

These two lines produce the same output:

    println!("{:?}", " A B C ".trim().split_whitespace().collect::<Vec<_>>());
    println!("{:?}", " A B C ".split_whitespace().collect::<Vec<_>>());

which is

["A", "B", "C"]
["A", "B", "C"]

playground

Lint Name

trim_split_whitespace

Category

pedantic

Advantage

  • The trim call is needless
  • The developer might not realize that split_whitespace works this way

Drawbacks

No response

Example

" A B C ".trim().split_whitespace()

Could be written as:

" A B C ".split_whitespace()
@ghost ghost added the A-lint Area: New lints label Mar 11, 2022
@bors bors closed this as completed in c7a705a May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

0 participants