Skip to content

Fix clippy warnings #376

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

Merged
merged 3 commits into from
Jun 2, 2017
Merged

Fix clippy warnings #376

merged 3 commits into from
Jun 2, 2017

Conversation

lukaslueg
Copy link
Contributor

This fixes all clippy warnings, mostly lifetime elisions and useless borrows. Clippy now passes on nightly; on stable/without clippy, no lint warnings are given. All tests still pass.

I've not moved any code around, which is the reason for allowing string_lit_as_bytes in so many places.

Up for review

src/re_bytes.rs Outdated
@@ -785,6 +785,7 @@ pub struct Captures<'t> {
named_groups: Arc<HashMap<String, usize>>,
}

#[allow(len_without_is_empty)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's probably better to

fn is_empty(&self) -> bool {
    debug_assert!(self.len() > 0);
    false
}

as this will trigger const-folding and warnings should anyone expect captures to be empty

Copy link
Member

Choose a reason for hiding this comment

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

I don't agree with adding a useless method to the public API.

Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

@lukaslueg Thanks so much for doing this. I realize it's a chore to do this, and I apologize in advance for being so picky about this stuff, but my comments probably show why I haven't really gotten on board the Clippy train.

At a high level, I agree with a lot of your changes. The amount of superfluous & sigils is amusing, and I wonder if I can blame that on a bygone era when those were actually required.

But there are a number of things I flat out disagree with, and I think some of the transformations you made make the code less readable. On a different note, your use of the {debug_}?assert_ne! macros requires a minimum Rust version bump, which I do not want to do at this time. We need to stick with Rust 1.12.

It seems like the way to express disagreement with Clippy is to pepper the code base with cfg'd attributes, and I simply do not want to deal with that. In principle, I'd be fine if it were rare, but this PR contains a lot of them. They have to go. If there is a way to add a config file to globally disable some lints, then I think I would be OK with the loss of precision.

@@ -9,6 +9,8 @@ macro_rules! regex {
}}
}

#[cfg_attr(not(feature = "cargo-clippy"), allow(unknown_lints))]
#[allow(regex_macro)]
Copy link
Member

Choose a reason for hiding this comment

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

I do not want to litter the examples directory with a bunch of clippy stuff. We need to find a way to remove this (similarly for the rest of the examples).

src/compile.rs Outdated
@@ -1049,13 +1055,6 @@ impl ByteClassSet {
}
}

fn u32_to_usize(n: u32) -> usize {
Copy link
Member

Choose a reason for hiding this comment

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

Why did you get rid of this function? Please put it back. Without it, you've turned a hard error into a silent error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think this is the only really useful warning by clippy for the whole codebase: The comparison always evaluates to false, making the check superfluous.

Why is it there anyway? if usize is 32bit, n can't be larger. If usize is 64bit, n can't be larger as well.

Copy link
Member

Choose a reason for hiding this comment

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

It's a guard against the case when usize is less than 32 bits. I'm not sure what the state of 16 bit usizes is in rustc, but IIRC people were interested in making it happen. It's certainly possible, so it seems worthwhile to be conservative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shall I leave a comment about std::convert::TryFrom ?

Copy link
Member

Choose a reason for hiding this comment

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

Sure.

src/compile.rs Outdated
@@ -713,6 +713,12 @@ impl Compiler {
}
}

impl Default for Compiler {
Copy link
Member

Choose a reason for hiding this comment

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

Is this actually used anywhere? If not, I don't see any reason to add it.

src/dfa.rs Outdated
@@ -271,7 +271,7 @@ struct State{
data: Box<[u8]>,
}

/// InstPtr is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
/// `InstPt`r is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the ending backtick is in the wrong spot.

(I really don't see why every single identifier needs to be put in backticks. These comments aren't public, so they'll never be rendered anyway.)

src/dfa.rs Outdated
@@ -482,7 +484,7 @@ impl<'a> Fsm<'a> {
Some(STATE_DEAD) => return Result::NoMatch(at),
Some(si) => si,
};
debug_assert!(dfa.start != STATE_UNKNOWN);
debug_assert_ne!(dfa.start, STATE_UNKNOWN);
Copy link
Member

Choose a reason for hiding this comment

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

debug_assert_ne! was added in Rust 1.13. regex 0.2's minimum support Rust version is 1.12. You'll need to revert all uses of debug_assert_ne! and assert_ne!.

src/re_bytes.rs Outdated
@@ -785,6 +785,7 @@ pub struct Captures<'t> {
named_groups: Arc<HashMap<String, usize>>,
}

#[allow(len_without_is_empty)]
Copy link
Member

Choose a reason for hiding this comment

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

I don't agree with adding a useless method to the public API.

src/re_bytes.rs Outdated
@@ -986,7 +987,7 @@ pub trait Replacer {
/// be beneficial to avoid finding sub-captures.
///
/// In general, this is called once for every call to `replacen`.
fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> {
fn no_expansion(&mut self) -> Option<Cow<[u8]>> {
Copy link
Member

Choose a reason for hiding this comment

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

Please put the lifetimes back. I don't mind elision in simple cases, but I'd rather the lifetime parameter be explicit here, especially in the trait definition.

src/re_trait.rs Outdated
self,
text: &'t Self::Text,
) -> CaptureMatches<'t, Self> {
text: & Self::Text,
Copy link
Member

Choose a reason for hiding this comment

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

There's an extra space here.

@@ -1198,7 +1199,7 @@ pub trait Replacer {
/// be beneficial to avoid finding sub-captures.
///
/// In general, this is called once for every call to `replacen`.
fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, str>> {
fn no_expansion(&mut self) -> Option<Cow<str>> {
Copy link
Member

Choose a reason for hiding this comment

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

As with above, please put the lifetime back. Leaving it off the implementations is fine.

tests/api.rs Outdated
@@ -1,36 +1,48 @@
#[test]
#[cfg_attr(not(feature = "cargo-clippy"), allow(unknown_lints))]
#[allow(trivial_regex, string_lit_as_bytes)]
Copy link
Member

Choose a reason for hiding this comment

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

No, I'm sorry, I cannot abide peppering attributes everywhere for Clippy. These have to go. If that means we don't get clean Clippy output, then we'll just have to live with that.

@lukaslueg
Copy link
Contributor Author

Don't hesitate to let me know of needed changes, it's just a few hunk-jumps :-)

Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

@lukaslueg Thanks so much! This is looking much better. :-) Unfortunately, I spotted one more nit that I didn't see last time. (I suspect it was accidental.)

src/lib.rs Outdated
@@ -432,9 +432,9 @@ These classes are based on the definitions provided in
<pre class="rust">
\d digit (\p{Nd})
\D not digit
\s whitespace (\p{White_Space})
\s whitespace (\p{`White_Space`})
Copy link
Member

Choose a reason for hiding this comment

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

Oh hmm, I don't think you should be using back ticks here.

src/lib.rs Outdated
\S not whitespace
\w word character (\p{Alphabetic} + \p{M} + \d + \p{Pc} + \p{Join_Control})
\w word character (\p{Alphabetic} + \p{M} + \d + \p{Pc} + \p{`Join_Control`})
Copy link
Member

Choose a reason for hiding this comment

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

Or here. :-)

@BurntSushi
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 2, 2017

📌 Commit fbb2ba7 has been approved by BurntSushi

@bors
Copy link
Contributor

bors commented Jun 2, 2017

⌛ Testing commit fbb2ba7 with merge 8ffd85d...

bors added a commit that referenced this pull request Jun 2, 2017
Fix clippy warnings

This fixes all clippy warnings, mostly lifetime elisions and useless borrows. Clippy now passes on nightly; on stable/without clippy, no lint warnings are given. All tests still pass.

I've not moved any code around, which is the reason for allowing `string_lit_as_bytes` in so many places.

Up for review
@bors
Copy link
Contributor

bors commented Jun 2, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: BurntSushi
Pushing 8ffd85d to master...

@bors bors merged commit fbb2ba7 into rust-lang:master Jun 2, 2017
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.

3 participants