Skip to content

is digits instead of digits range contains #9933

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

Closed
leonardo-m opened this issue Nov 23, 2022 · 10 comments
Closed

is digits instead of digits range contains #9933

leonardo-m opened this issue Nov 23, 2022 · 10 comments
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@leonardo-m
Copy link

leonardo-m commented Nov 23, 2022

What it does

It suggests to replace a coding pattern of using range + "contains" with a more specific function.

Lint Name

is_ascii_digit_instead_contains

Category

style

Advantage

Less bug-prone, shorter

Drawbacks

No response

Example

fn foo1(d: u8) -> bool {
    (b'0' ..= b'9').contains(&d)
}

Could be written as:

fn foo2(d: u8) -> bool {
    d.is_ascii_digit()
}
@leonardo-m leonardo-m added the A-lint Area: New lints label Nov 23, 2022
@koka831
Copy link
Contributor

koka831 commented Nov 23, 2022

@leonardo-m
Copy link
Author

Are you sure that lint fires for that foo1 function? I've tried with this and still I don't see anything.

#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

@koka831
Copy link
Contributor

koka831 commented Nov 23, 2022

Ah, I'm sorry I misunderstood it. The lint only supports for matches! macro and does not for contains currently.

@xFrednet
Copy link
Member

This can be an addition to the lint logic. :)

@rustbot label +good-first-issue

@rustbot rustbot added the good-first-issue These issues are a good way to get started with Clippy label Nov 23, 2022
@naosense
Copy link
Contributor

@rustbot claim

Any hints where I can start?😀

@koka831
Copy link
Contributor

koka831 commented Nov 25, 2022

Hi @naosense, you can add check for contains method in is_manual_ascii_check lint.
Currently it looks for matches! macro only.

let Some(macro_call) = root_macro_call(expr.span) else { return };
if is_matches_macro(cx, macro_call.def_id) {

You may find further informations in the book https://doc.rust-lang.org/nightly/clippy/development/index.html :)

@xFrednet
Copy link
Member

xFrednet commented Nov 25, 2022

Hey @naosense you can start, by checking for <expr-1>.contains(<expr-2>) calls. If <expr-2> is a character you can then check if <expr-1> is a specific range.

When starting on a new lint I often first look at the HIR with the #[clippy::dump] attribute. Try running Clippy on this playground. Then you "just" need to write the logic to match the printed HIR tree.

As always you're welcome to reach out if you want more guidance. @koka831 already correctly suggested, adding this in the rust-clippy/clippy_lints/src/manual_is_ascii_check.rs file. Mostly to keep all lint logic in one place

@naosense
Copy link
Contributor

naosense commented Dec 7, 2022

Does anyone use intellj, I configured it as instructed, but it didn't look good, see #9969. Anything wrong?

bors added a commit that referenced this issue Dec 14, 2022
improve `manual_is_ascii_check ` check

Sorry, not familiar the api, i can only check the method name of expression `<expr-1>.contains(<expr-2>)` after read clippy book and hints from #9933 . i dont know how to check
1.  if <expr-1> is a specific range
2. <expr-2> is a character

r? `@xFrednet` could you please provide some more hints? 😝️

---

changelog: Enhancement: [`manual_is_ascii_check`]: Now detects ranges with `.contains()` calls
[#10053](#10053)
<!-- changelog_checked -->
@Niki4tap
Copy link
Contributor

@xFrednet this now can be closed since #10053 landed?

@llogiq
Copy link
Contributor

llogiq commented Dec 24, 2022

Agreed. Let's close this.

@llogiq llogiq closed this as completed Dec 24, 2022
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

7 participants