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