splitn or rsplitn called with a limit of 0 or 1 never splits

ID

rust.correctness.splitn_limit

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Api Misuse

Language

Rust

Description

Detects splitn and rsplitn called with a literal limit of 0 or 1, including suffixed spellings such as 1usize.

Rationale

The limit counts the pieces produced, not the separators consumed: splitn(1, ..) yields the whole input as a single piece and splitn(0, ..) yields nothing at all, so the separator is never honoured. The call compiles and returns an iterator, so the caller reads an unsplit string as if it had been parsed.

The following code illustrates the pattern detected by this rule:

pub fn parse_header(line: &str) -> Vec<&str> {
    // FLAGGED: splitn or rsplitn called with a limit of 0 or 1 never splits
    line.splitn(1, ':').collect()
}

Remediation

Pass the number of pieces wanted — splitn(2, ..) to cut a key=value line once — or use split when every occurrence of the separator should be honoured. Reach for split_once when exactly one split is needed and both halves are required.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved