rust-clippy
rust-clippy copied to clipboard
Suggest replacing str::split with empty pattern with str::chars
What it does
I was helping a friend learn Rust by going through Advent of Code 2020, and they needed to split a &str into a Vec<char>. They found the str::split method, and wrote string.split("").collect::<Vec<char>>(). While reviewing their code, this tripped me up a bit, since I would think of string.chars().collect::<Vec<char>>() instead. I think this would be a good candidate for a lint.
Advantage
- I think this is more intuitive than splitting with an empty pattern
Drawbacks
- as this playground shows, this is not a one-to-one replacement because
str::split("")has leading and trailing empty strings.
Example
"hello world".split("");
The above should trigger a suggestion of replacing with
"hello world".chars();
@rustbot claim