Skip to content
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

Manual char string search pattern #12490

Open
kornelski opened this issue Mar 15, 2024 · 5 comments
Open

Manual char string search pattern #12490

kornelski opened this issue Mar 15, 2024 · 5 comments
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@kornelski
Copy link
Contributor

kornelski commented Mar 15, 2024

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 kornelski added the A-lint Area: New lints label Mar 15, 2024
@Alexendoo Alexendoo added the good-first-issue These issues are a good way to get started with Clippy label Mar 15, 2024
@CBSpeir
Copy link
Contributor

CBSpeir commented Mar 16, 2024

@rustbot claim

@Vrajs16
Copy link

Vrajs16 commented Apr 18, 2024

@rustbot claim

@rustbot rustbot assigned Vrajs16 and unassigned CBSpeir Apr 18, 2024
@AurelienFT
Copy link
Contributor

Hello @Vrajs16,
Are you still working on this ?
I would love to start contributing to clippy and this is an issue that interest me :)

@Vrajs16
Copy link

Vrajs16 commented May 23, 2024

@AurelienFT you can claim it

@AurelienFT
Copy link
Contributor

@rustbot claim

@rustbot rustbot assigned AurelienFT and unassigned Vrajs16 May 23, 2024
bors added a commit that referenced this issue Jun 11, 2024
…lexendoo

Add lint to check manual pattern char comparison

This PR adds a new lint asked in #12490

This lint catches manual char comparison in pattern of string functions and propose to use `char` or array of `char` instead.

As it's my first contribution i'm not feeling very safe about not matching too much or missing some cases.

Thanks in advance for taking time to review and propose feedback !
bors added a commit that referenced this issue Jun 11, 2024
…lexendoo

Add lint to check manual pattern char comparison

This PR adds a new lint asked in #12490

This lint catches manual char comparison in pattern of string functions and propose to use `char` or array of `char` instead.

As it's my first contribution i'm not feeling very safe about not matching too much or missing some cases.

Thanks in advance for taking time to review and propose feedback !

changelog: new lint [`manual_pattern_char_comparison`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

5 participants