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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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: [`manual_ignore_case_cmp`]: replace `to_ascii_lowercase`-style comparisons with `eq_ignore_ascii_case`

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?

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 +20 to +34
fn unsupported(a: char, b: char) {
// TODO:: these are rare, and might not be worth supporting
a.to_ascii_lowercase() == char::to_ascii_lowercase(&b);
char::to_ascii_lowercase(&a) == b.to_ascii_lowercase();
char::to_ascii_lowercase(&a) == char::to_ascii_lowercase(&b);
Copy link
Member

Choose a reason for hiding this comment

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

Not really worth supporting. I would expect them to still trigger the lint though 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oki, will keep them as part of tests to track progress if they ever change, but won't fix them in the lint

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.

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.

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