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

Fix enum_variants FP on prefixes that are not camel-case #8127

Merged
merged 8 commits into from
Dec 28, 2021

Conversation

dswij
Copy link
Member

@dswij dswij commented Dec 15, 2021

closes #8090

Fix FP on enum_variants when prefixes are only a substring of a camel-case word. Also adds some util helpers on str_utils to help parsing camel-case strings.

This changes how the lint behaves:

  1. previously if the Prefix is only a length of 1, it's going to get ignored, i.e. these were previously ignored and now is warned
enum Foo {
    cFoo,
    cBar,
    cBaz,
}

enum Something {
    CCall,
    CCreate,
    CCryogenize,
}
  1. non-ascii characters that doesn't have casing will not be split,
enum NonCaps {
    PrefixXXX,
    PrefixTea,
    PrefixCake,
}

will be considered as PrefixXXX, Prefix, Prefix, so this won't lint as opposed to fired previously.

changelog: [enum_variant_names] Fix FP when first prefix are only a substring of a camel-case word.


(Edited by @xFrednet removed some non ascii characters)

@rust-highfive
Copy link

r? @llogiq

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 15, 2021
@dswij dswij marked this pull request as ready for review December 23, 2021 05:39
@dswij dswij changed the title [WIP] Fix enum_variants FP on prefixes that are not camel-case Fix enum_variants FP on prefixes that are not camel-case Dec 23, 2021
@xFrednet
Copy link
Member

I would like to review this, I hope it's okay to steal the assignment ^^

r? @xFrednet

(It's on my todo list later today 🙃 )

@rust-highfive rust-highfive assigned xFrednet and unassigned llogiq Dec 23, 2021
@llogiq
Copy link
Contributor

llogiq commented Dec 23, 2021

Sure! It's all yours, @xFrednet !

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.

Thank you for the changes, overall, they look good to me and the code is also more readable now. I found some things which can probably be improved, but nothing major. I've also been a bit more nit picky than usual in str_utils 😅. Afterwards, it should be ready to be merged 🙃

clippy_lints/src/enum_variants.rs Outdated Show resolved Hide resolved
clippy_utils/src/str_utils.rs Outdated Show resolved Hide resolved
clippy_utils/src/str_utils.rs Outdated Show resolved Hide resolved
clippy_utils/src/str_utils.rs Outdated Show resolved Hide resolved
/// StrIndex::new(6, 6)]);
/// assert_eq!(camel_case_indexes("abcDef"), vec![StrIndex::new(3, 3), StrIndex::new(6, 6)]);
/// ```
pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> {
pub fn camel_case_indices(s: &str) -> Vec<StrIndex> {

Same for the documentation

Comment on lines 149 to 162
let offsets = camel_case_indexes(s);
let mut idxs_iter = offsets.iter().map(|str_idx| str_idx.byte_index).peekable();
let idxs: Vec<usize> = if let Some(&idx) = idxs_iter.peek() {
if idx == 0 {
idxs_iter.collect()
} else {
Vec::<usize>::from([0]).into_iter().chain(idxs_iter).collect()
}
} else {
return vec![s];
};
let split_points: Vec<(&usize, &usize)> = idxs[..idxs.len() - 1].iter().zip(&idxs[1..]).collect();

split_points.iter().map(|(&start, &stop)| &s[start..stop]).collect()
Copy link
Member

Choose a reason for hiding this comment

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

All of these collects seem a bit much to me. I would suggest the following rewrite 🙃

    let offsets = camel_case_indexes(s);

    if offsets[0] != 0 {
        offsets.insert(0, 0);
    }

    offsets.as_slice().windows(2).map(|win| {
        let start = win[0].byte_index;
        let end = win[1].byte_index;
        &s[start..end]
    }).collect()

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this seems much better, I was trying to be too fancy there 😅

Copy link
Member

Choose a reason for hiding this comment

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

This also often happens to me, it's just more fun to go fancy ^^. BTW, there is also a std::iter::once() function to create a one time iterator, that's more efficient than Vec::<usize>::from([0]).into_iter() as it doesn't allocate any memory. 🙃

clippy_lints/src/enum_variants.rs Outdated Show resolved Hide resolved
tests/ui/enum_variants.stderr Show resolved Hide resolved
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.

LGTM. One last NIT, Rust and Clippy have a no-merge policy, meaning that no merge-commits should be added in PRs. Could you please remove the merge commit from this branch and rebase on master. I'm guessing the easiest way is to drop the commit first with git rebase -i HEAD~9 (This opens the rebase interface) and afterwards using git rebase upstream/master or something similar depending on your setup. Let me know if you need help with that.

Then I think it's ready to be merged 🙃

`enum_variant_names` will consider characters with no case to be a part
of prefixes/suffixes substring that are compared. This means `Foo1` and
`Foo2` has different prefixes (`Foo1` and `Foo2` prefix respeectively).
This applies to all non-ascii characters with no casing.
@dswij
Copy link
Member Author

dswij commented Dec 27, 2021

@xFrednet BTW, this is rebased and ready to be re-reviewed 🙂

@xFrednet
Copy link
Member

Awesome thank you very much! This looks good to me 👍

@bors r+

(Bors seems to have some problems since yesterday. Let's hope that it runs now 🙃 )

@bors
Copy link
Contributor

bors commented Dec 27, 2021

📌 Commit b82c9ce has been approved by xFrednet

@bors
Copy link
Contributor

bors commented Dec 27, 2021

⌛ Testing commit b82c9ce with merge ab66862...

bors added a commit that referenced this pull request Dec 27, 2021
Fix `enum_variants` FP on prefixes that are not camel-case

closes #8090

Fix FP on `enum_variants` when prefixes are only a substring of a camel-case word. Also adds some util helpers on `str_utils` to help parsing camel-case strings.

This changes how the lint behaves:

1. previously if the Prefix is only a length of 1, it's going to get ignored, i.e. these were previously ignored and now is warned
```rust
enum Foo {
    cFoo,
    cBar,
    cBaz,
}

enum Something {
    CCall,
    CCreate,
    CCryogenize,
}
```

2. non-ascii characters that doesn't have casing will not be split,
```rust
enum NonCaps {
    Prefix的,
    PrefixTea,
    PrefixCake,
}
```
will be considered as `Prefix的`, `Prefix`, `Prefix`, so this won't lint as opposed to fired previously.

changelog: [`enum_variant_names`] Fix FP when first prefix are only a substring of a camel-case word.
@bors
Copy link
Contributor

bors commented Dec 27, 2021

💔 Test failed - checks-action_test

@xFrednet
Copy link
Member

@bors retry

@bors
Copy link
Contributor

bors commented Dec 27, 2021

⌛ Testing commit b82c9ce with merge e5293c5...

bors added a commit that referenced this pull request Dec 27, 2021
Fix `enum_variants` FP on prefixes that are not camel-case

closes #8090

Fix FP on `enum_variants` when prefixes are only a substring of a camel-case word. Also adds some util helpers on `str_utils` to help parsing camel-case strings.

This changes how the lint behaves:

1. previously if the Prefix is only a length of 1, it's going to get ignored, i.e. these were previously ignored and now is warned
```rust
enum Foo {
    cFoo,
    cBar,
    cBaz,
}

enum Something {
    CCall,
    CCreate,
    CCryogenize,
}
```

2. non-ascii characters that doesn't have casing will not be split,
```rust
enum NonCaps {
    PrefixXXX,
    PrefixTea,
    PrefixCake,
}
```
will be considered as `PrefixXXX`, `Prefix`, `Prefix`, so this won't lint as opposed to fired previously.

changelog: [`enum_variant_names`] Fix FP when first prefix are only a substring of a camel-case word.

---

 (Edited by `@xFrednet` removed some non ascii characters)
@bors
Copy link
Contributor

bors commented Dec 27, 2021

💔 Test failed - checks-action_dev_test

@xFrednet
Copy link
Member

@bors retry (restarting the failed job worked on the normal CI, let's see it now)

@bors
Copy link
Contributor

bors commented Dec 27, 2021

⌛ Testing commit b82c9ce with merge 3f2bb30...

bors added a commit that referenced this pull request Dec 27, 2021
Fix `enum_variants` FP on prefixes that are not camel-case

closes #8090

Fix FP on `enum_variants` when prefixes are only a substring of a camel-case word. Also adds some util helpers on `str_utils` to help parsing camel-case strings.

This changes how the lint behaves:

1. previously if the Prefix is only a length of 1, it's going to get ignored, i.e. these were previously ignored and now is warned
```rust
enum Foo {
    cFoo,
    cBar,
    cBaz,
}

enum Something {
    CCall,
    CCreate,
    CCryogenize,
}
```

2. non-ascii characters that doesn't have casing will not be split,
```rust
enum NonCaps {
    PrefixXXX,
    PrefixTea,
    PrefixCake,
}
```
will be considered as `PrefixXXX`, `Prefix`, `Prefix`, so this won't lint as opposed to fired previously.

changelog: [`enum_variant_names`] Fix FP when first prefix are only a substring of a camel-case word.

---

 (Edited by `@xFrednet` removed some non ascii characters)
@bors
Copy link
Contributor

bors commented Dec 27, 2021

💔 Test failed - checks-action_test

@dswij
Copy link
Member Author

dswij commented Dec 27, 2021

Lots of failed downloads on GH action, I suppose we can wait for a while 🙂

 Warning: Failed to download action 'https://api.github.com/repos/actions/download-artifact/tarball/18f0f591fbc635562c815484d73b6e8e3980482e'. Error: The operation was canceled.
Warning: Back off 12.276 seconds before retry.
Warning: Failed to download action 'https://api.github.com/repos/actions/download-artifact/tarball/18f0f591fbc635562c815484d73b6e8e3980482e'. Error: The operation was canceled.
Warning: Back off 15.908 seconds before retry.
Error: The operation was canceled.

@xFrednet
Copy link
Member

GitHub had some outages, not everything seems to work again. let's try it one more time

@bors retry

@bors
Copy link
Contributor

bors commented Dec 28, 2021

⌛ Testing commit b82c9ce with merge 56ccd30...

@bors
Copy link
Contributor

bors commented Dec 28, 2021

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

@bors bors merged commit 56ccd30 into rust-lang:master Dec 28, 2021
@bors bors mentioned this pull request Dec 28, 2021
@dswij dswij deleted the 8090 branch January 20, 2022 12:27
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.

enum_variant_names false positive when first word has common prefix as part of word
5 participants