Skip to content

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

Closed
@ghost

Description

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions