-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add needless_character_iteration
lint
#12815
Add needless_character_iteration
lint
#12815
Conversation
1fa6ea2
to
076e3bc
Compare
Fixed dogfood errors. |
#![allow(clippy::unnecessary_operation)] | ||
|
||
fn main() { | ||
"foo".chars().all(|c| c.is_ascii()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test where there is a different method between chars()
and all()
? Also some test where the all call tests something else as well, like .all(|c| c.is_ascii() && c.is_alphabetic())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid start, I'd like to see a few more tests and have a few questions, then we can merge this :D
ExprKind::Block(block, _) => { | ||
if let Some(block_expr) = block.expr | ||
// First we ensure that this is a "binding chain" (each statement is a binding | ||
// of the previous one) and that it is a binding of the closure argument. | ||
&& let Some(last_chain_binding_id) = | ||
get_last_chain_binding_hir_id(first_param, block.stmts) | ||
{ | ||
handle_expr(cx, block_expr, last_chain_binding_id, span, before_chars, revert); | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this part complicates the lint unnecessary. If people write weird code where they chain these things, I'd say it's okay to not lint it, but we can keep it if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's convenient and easy enough to check so if you don't mind, I'd prefer to keep it. ;)
076e3bc
to
566dfd9
Compare
Fixed the corner cases and added more tests. |
Roses are red, |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
--> tests/ui/needless_character_iteration.rs:24:5 | ||
| | ||
LL | "foo".chars().all(|c| !char::is_ascii(&c)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!"foo".is_ascii()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This transformation isn't correct:
fn main() {
let s = "Beyoncé";
// EVERY character isn't ascii
dbg!(s.chars().all(|c| !char::is_ascii(&c)));
// ANY character isn't ascii
dbg!(!s.is_ascii());
}
[src/main.rs:3:5] s.chars().all(|c| !char::is_ascii(&c)) = false
[src/main.rs:4:5] !s.is_ascii() = true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! Interested into send a fix? Otherwise please open an issue tagging me on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't have time to work on this before it becomes a problem for nightly users. Filled #12879
Fixes #4817.
r? @xFrednet
changelog: Add
needless_character_iteration
lint