rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Manual char string search pattern

Open kornelski opened this issue 1 year ago • 2 comments

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(['.', '!', '?'])

kornelski avatar Mar 15 '24 01:03 kornelski

@rustbot claim

CBSpeir avatar Mar 16 '24 16:03 CBSpeir

@rustbot claim

Vrajs16 avatar Apr 18 '24 02:04 Vrajs16

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 avatar May 23 '24 13:05 AurelienFT

@AurelienFT you can claim it

Vrajs16 avatar May 23 '24 14:05 Vrajs16

@rustbot claim

AurelienFT avatar May 23 '24 18:05 AurelienFT