rust-lang / rust-lang/rust-clippy

New lint: `split(" ")` where `split_whitespace()` was likely meant

Open
#17,693 4 comments 1 reaction 1 assignee View on GitHub

@Szizoid is already working on this.

Since Sep 7, 2026.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Warns when str::split(" ") (or split(' ')) is used in a way that suggests the author wanted to split on whitespace generally, not on a literal single space. split_whitespace() handles multiple consecutive spaces, tabs, and leading/trailing whitespace correctly, while split(" ") produces empty strings between repeated spaces.

Example:

    println!("{:?}", "a b".split(" ").collect::<Vec<_>>());
    println!("{:?}", "a b".split_whitespace().collect::<Vec<_>>());
    println!("{:?}", "a  b".split(" ").collect::<Vec<_>>());

Output:

["a", "b"]
["a", "b"]
["a", "", "b"]

Playground

Advantage
  • Catches a bug that's easy to miss, split(" ") ignores extra spaces
  • Points people toward the method that actually does what is probably meant
Drawbacks

split(" ") and split_whitespace() don't do the same thing, so this can't be auto fixed, that would break anything relying on the empty strings, needs some thought on when it should trigger, like when the result is immediately filtered with .filter(|p| !p.is_empty()) or warn without auto fixing.

Example
let parts: Vec<&str> = input.split(" ").filter(|p| !p.is_empty()).collect();

Could be written as:

let parts: Vec<&str> = input.split_whitespace().collect();
Comparison with existing lints

Similar to trim_split_whitespace which flags the unnecessary .trim() before .split_whitespace(), but this is a different case, since it's about .trim(), not .split(" ").

There's also non_portable_line_iteration which suggested .lines() instead of split("\n") or .split("\r\n").

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.