Skip to content

Suggest using string.lines() when string.split("\n"), or string.split("\r\n") is seen #8733

@cowlicks

Description

@cowlicks

What it does

Suggest replacing string.split("\n"), string.split('\n'), and string.split("\r\n") with string.lines().

Note that clippy throws a single_char_split warning for string.split("\n") which should be updated to this.

Lint Name

non_portable_line_iteration

Category

suspicious, style

Advantage

Makes string line iteration portable across Unix and Windows.

Drawbacks

If you need to differentiate between platform specific newlines this will be a false positive.
Also, if the string has a final terminating newline, the result of a .split will differ from .lines.
This could be avoiding by having the lint only trigger on .trim().split(sep).

Example

for line in string.split("\n") { ... }
// or
for line in string.split("\r\n") { ... }
// or
for line in string.split('\n') { ... }

Could be written as:

for line in string.lines() { ... }

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions