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

Add needless_character_iteration lint #12815

Merged

Conversation

GuillaumeGomez
Copy link
Member

Fixes #4817.

r? @xFrednet

changelog: Add needless_character_iteration lint

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 17, 2024
@GuillaumeGomez GuillaumeGomez force-pushed the add-needless_character_iteration branch 2 times, most recently from 1fa6ea2 to 076e3bc Compare May 18, 2024 13:25
@GuillaumeGomez
Copy link
Member Author

Fixed dogfood errors.

#![allow(clippy::unnecessary_operation)]

fn main() {
"foo".chars().all(|c| c.is_ascii());
Copy link
Member

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())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

@xFrednet xFrednet left a 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

tests/ui/needless_character_iteration.rs Show resolved Hide resolved
Comment on lines 40 to 60
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);
}
},
Copy link
Member

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.

Copy link
Member Author

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. ;)

@GuillaumeGomez GuillaumeGomez force-pushed the add-needless_character_iteration branch from 076e3bc to 566dfd9 Compare May 27, 2024 12:16
@GuillaumeGomez
Copy link
Member Author

Fixed the corner cases and added more tests.

@xFrednet
Copy link
Member

xFrednet commented Jun 3, 2024

Roses are red,
ascii strings are checked,
this is now added,
to the merge roulette.

@bors
Copy link
Collaborator

bors commented Jun 3, 2024

📌 Commit 566dfd9 has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 3, 2024

⌛ Testing commit 566dfd9 with merge 61d3e14...

@bors
Copy link
Collaborator

bors commented Jun 3, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing 61d3e14 to master...

@bors bors merged commit 61d3e14 into rust-lang:master Jun 3, 2024
8 checks passed
@GuillaumeGomez GuillaumeGomez deleted the add-needless_character_iteration branch June 3, 2024 08:32
--> tests/ui/needless_character_iteration.rs:24:5
|
LL | "foo".chars().all(|c| !char::is_ascii(&c));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!"foo".is_ascii()`
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: detect needless character iteration
5 participants