-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
r? @llogiq (rust-highfive has picked a reviewer for you, use r? to override) |
enum_variants
FP on prefixes that are not camel-caseenum_variants
FP on prefixes that are not camel-case
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 🙃 ) |
Sure! It's all yours, @xFrednet ! |
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.
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_utils/src/str_utils.rs
Outdated
/// 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> { |
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.
pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> { | |
pub fn camel_case_indices(s: &str) -> Vec<StrIndex> { |
Same for the documentation
clippy_utils/src/str_utils.rs
Outdated
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() |
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.
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()
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.
Yeah, this seems much better, I was trying to be too fancy there 😅
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 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. 🙃
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.
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.
@xFrednet BTW, this is rebased and ready to be re-reviewed 🙂 |
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 🙃 ) |
📌 Commit b82c9ce has been approved by |
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.
💔 Test failed - checks-action_test |
@bors retry |
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)
💔 Test failed - checks-action_dev_test |
@bors retry (restarting the failed job worked on the normal CI, let's see it now) |
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)
💔 Test failed - checks-action_test |
Lots of failed downloads on GH action, I suppose we can wait for a while 🙂
|
GitHub had some outages, not everything seems to work again. let's try it one more time @bors retry |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
closes #8090
Fix FP on
enum_variants
when prefixes are only a substring of a camel-case word. Also adds some util helpers onstr_utils
to help parsing camel-case strings.This changes how the lint behaves:
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)