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 manual_ignore_cast_cmp lint #13334

Merged
merged 1 commit into from
Oct 13, 2024
Merged

Add manual_ignore_cast_cmp lint #13334

merged 1 commit into from
Oct 13, 2024

Conversation

nyurik
Copy link
Contributor

@nyurik nyurik commented Sep 2, 2024

// bad
fn compare(a: &str, b: &str) -> bool {
    a.to_ascii_lowercase() == b.to_ascii_lowercase()
    || a.to_ascii_lowercase() == "abc"
}

// good
fn compare(a: &str, b: &str) -> bool {
   a.eq_ignore_ascii_case(b)
   || a.eq_ignore_ascii_case("abc")
}
  • Followed [lint naming conventions][lint_naming]
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

changelog: New lint: [manual_ignore_case_cmp] perf
#13334

Closes #13204

@rustbot
Copy link
Collaborator

rustbot commented Sep 2, 2024

r? @xFrednet

rustbot has assigned @xFrednet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 2, 2024
@bors
Copy link
Collaborator

bors commented Sep 3, 2024

☔ The latest upstream changes (presumably #13247) made this pull request unmergeable. Please resolve the merge conflicts.

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, let me know if anything is unclear :D

clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
Comment on lines 43 to 44
let ty = ty_raw.peel_refs();
if needs_ref_to_cmp(cx, ty)
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 explain these lines?

They feel a bit weird. The peel_refs removes all potential references from the type, which means that some ascii types will fail the needs_ref_to_cmp function even if ty_raw would pass it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xFrednet what's the better way to do this comparison?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i was thinking some more on this, and tried to come up with a unit test that would fail, but was not able to do that... are you certain this is an issue?

Copy link
Member

Choose a reason for hiding this comment

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

I think the naming tripped me up. When using this function you should make sure that the raw type is still a reference. (ty_raw.is_ref() && needs_ref_to_cmp(cx, ty)) should do the trick

Copy link
Contributor Author

@nyurik nyurik Sep 24, 2024

Choose a reason for hiding this comment

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

@xFrednet I tried your suggestion, but it starts to miss half of the test cases like these:

fn string(a: String, b: String) {
    a.to_ascii_lowercase() == b.to_ascii_lowercase();
    a.to_ascii_lowercase() == "a";
    "a" == b.to_ascii_lowercase();
    a.to_ascii_lowercase() == "a";
    "a" == b.to_ascii_lowercase();
}

fn ref_string(a: String, b: &String) {
    a.to_ascii_lowercase() == b.to_ascii_lowercase();
    a.to_ascii_lowercase() == "a";

    b.to_ascii_lowercase() == a.to_ascii_lowercase();
    "a" == a.to_ascii_lowercase();
}

clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
tests/ui/manual_ignore_case_cmp.rs Show resolved Hide resolved
tests/ui/manual_ignore_case_cmp.rs Show resolved Hide resolved
clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Sep 5, 2024

☔ The latest upstream changes (presumably #12987) made this pull request unmergeable. Please resolve the merge conflicts.

@nyurik nyurik force-pushed the ascii-str-eq branch 7 times, most recently from f1d859e to c4f6a20 Compare September 20, 2024 13:27
@bors
Copy link
Collaborator

bors commented Sep 22, 2024

☔ The latest upstream changes (presumably #13322) made this pull request unmergeable. Please resolve the merge conflicts.

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.

Three small nits, but the rest looks good to me :D

clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@nyurik nyurik left a comment

Choose a reason for hiding this comment

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

thx, updated

clippy_lints/src/manual_ignore_case_cmp.rs Outdated Show resolved Hide resolved
@nyurik
Copy link
Contributor Author

nyurik commented Sep 24, 2024

CI is also having some download issues, seems unrelated

@nyurik
Copy link
Contributor Author

nyurik commented Oct 1, 2024

@xFrednet hi, anything else left on this one? thx!

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.

This version looks good to me! I've also checked and we don't need an MSRV check, since eq_ignore_ascii_case and to_ascii_lowercase have the same MSRV

I created an FCP: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/FCP.20.60manual_ignore_cast_cmp.60/near/474019623

@xFrednet xFrednet added S-final-comment-period Status: final comment period it will be merged unless new objections are raised (~1 week) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 4, 2024
@xFrednet
Copy link
Member

It looks like the FCP passed without comments :D

Thank you for the implementation:


Roses are red,
Violets are blue,
Final comments are done,
there were none

@bors
Copy link
Collaborator

bors commented Oct 13, 2024

📌 Commit d1dec19 has been approved by xFrednet

It is now in the queue for this repository.

@xFrednet xFrednet removed the S-final-comment-period Status: final comment period it will be merged unless new objections are raised (~1 week) label Oct 13, 2024
@bors
Copy link
Collaborator

bors commented Oct 13, 2024

⌛ Testing commit d1dec19 with merge c71f0be...

@bors
Copy link
Collaborator

bors commented Oct 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest using eq_ignore_ascii_case on non case-sensitive string comparision
4 participants