rust-clippy
rust-clippy copied to clipboard
Manual char string search pattern
What it does
Uses of Pattern (in methods like str.split(), str.replace(), str.trim_matches()) where the pattern is a closure matching chars:
str.split(|c: char| c == '\n' || c == 'X') // or
str.split(|c: char| matches!(c, '\n' | 'X'))
can be expressed more succinctly as:
str.split(['\n', 'X'])
and a single-char comparison can be simplified further.
Advantage
It makes the code much shorter, and the character set much easier to read.
They all have about the same performance, except a single-char pattern is much faster than the other forms.
Drawbacks
No response
Example
sentence.trim_end_matches(|c: char| c == '.' || c == '!' || c == '?')
Could be written as:
sentence.trim_end_matches(['.', '!', '?'])
@rustbot claim
@rustbot claim
Hello @Vrajs16, Are you still working on this ? I would love to start contributing to clippy and this is an issue that interest me :)
@AurelienFT you can claim it
@rustbot claim