-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Suggest using string.lines() when string.split("\n"), or string.split("\r\n") is seen #8733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's not equivalent. fn main() {
let text = "foo\r\nbar\n\nbaz\n";
println!("{:?}", text.split("\n").collect::<Vec<_>>());
println!("{:?}", text.split("\r\n").collect::<Vec<_>>());
println!("{:?}", text.lines().collect::<Vec<_>>());
} gives
Even when the line endings aren't mixed, |
@rustbot claim |
@mikerite so what are you suggesting? Maybe this lint can go into the "pedantic" category, with an appropriate message stating that they're not equivalent, but maybe you can rewrite a bit in order to use the |
Also, if this lint is still not taken I'd like to claim it 🙂 |
I think this lint shouldn't exist or it should go into restriction. The line endings and final blank element won't matter 99% of the time. When it does, the suggestion introduce bugs. |
@rustbot claim |
Indeed it is not. Good point about the final Perhaps a good middle ground would be to only suggest this lint whenever we see |
@rustbot claim |
Just for my own records, right now the GitHub search finds
|
What it does
Suggest replacing
string.split("\n")
,string.split('\n')
, andstring.split("\r\n")
withstring.lines()
.Note that clippy throws a
single_char_split
warning forstring.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
Could be written as:
The text was updated successfully, but these errors were encountered: