-
Notifications
You must be signed in to change notification settings - Fork 162
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
Make Clippy happier, minor cleanups #283
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I ran `cargo clippy` and addressed some issues it raised, while leaving other issues for another time. Hopefully this will make this code a bit easier for newcomers to read. Biggest changes: * `foo.len() > 0` ➡ `!foo.is_empty()` * `return x;` ➡ `x` * `{ foo: foo }` ➡ `{ foo }` * adding a few `Default` trait implementations * some `match` statement cleanup Also, fixed two `ffi::uInt::max_value()` ➡ `ffi::uInt::MAX` due to deprecation. Biggest TODOs (please give feedback if they should be fixed too): * Clippy has complained about many `foo >> 0` -- which is a noop that might confuse (?) * some other corner cases that I don't know enough about
@alexcrichton thx for merging! Do you have any thoughts about the TODO in the comment? |
Sorry no, I can perhaps offer my thoughts on a case-by-case basis but I don't run clippy myself. |
nyurik
added a commit
to nyurik/flate2-rs
that referenced
this pull request
Dec 13, 2021
Following up from rust-lang#283 * Address all remaining clippy issues * Switch `assert!(a==b)` to `assert_eq!(a, b)` * `MAX` instead of `max_value()` > warning: this seems like a manual implementation of the non-exhaustive pattern > --> src/mem.rs:46:1 > | > 46 | pub enum FlushCompress { > | ^--------------------- > | | > | _help: add the attribute: `#[non_exhaustive] pub enum FlushCompress` > | | > 47 | | /// A typical parameter for passing to compression/decompression functions, > 48 | | /// this indicates that the underlying stream to decide how much data to > 49 | | /// accumulate before producing output in order to maximize compression. > ... | > 85 | | _Nonexhaustive, > 86 | | } > | |_^ > | > = note: `#[warn(clippy::manual_non_exhaustive)]` on by default > help: remove this variant > --> src/mem.rs:85:5 > | > 85 | _Nonexhaustive, > | ^^^^^^^^^^^^^^ > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive > > warning: this seems like a manual implementation of the non-exhaustive pattern > --> src/mem.rs:91:1 > | > 91 | pub enum FlushDecompress { > | ^----------------------- > | | > | _help: add the attribute: `#[non_exhaustive] pub enum FlushDecompress` > | | > 92 | | /// A typical parameter for passing to compression/decompression functions, > 93 | | /// this indicates that the underlying stream to decide how much data to > 94 | | /// accumulate before producing output in order to maximize compression. > ... | > 113 | | _Nonexhaustive, > 114 | | } > | |_^ > | > help: remove this variant > --> src/mem.rs:113:5 > | > 113 | _Nonexhaustive, > | ^^^^^^^^^^^^^^ > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive > > warning: the operation is ineffective. Consider reducing it to `(header[4] as u32)` > --> src/gz/bufread.rs:58:39 > | > 58 | r.part.header.mtime = ((header[4] as u32) << 0) > | ^^^^^^^^^^^^^^^^^^^^^^^^^ > | > = note: `#[warn(clippy::identity_op)]` on by default > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: `ref` on an entire `let` pattern is discouraged, take a reference with `&` instead > --> src/gz/bufread.rs:197:13 > | > 197 | let ref arr = [ > | ^^^^^^^ > | > = note: `#[warn(clippy::toplevel_ref_arg)]` on by default > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#toplevel_ref_arg > help: try > | > 197 ~ let arr = &[ > 198 + (crc.sum() >> 0) as u8, > 199 + (crc.sum() >> 8) as u8, > 200 + (crc.sum() >> 16) as u8, > 201 + (crc.sum() >> 24) as u8, > 202 + (crc.amount() >> 0) as u8, > ... > > warning: the operation is ineffective. Consider reducing it to `crc.sum()` > --> src/gz/bufread.rs:198:13 > | > 198 | (crc.sum() >> 0) as u8, > | ^^^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `crc.amount()` > --> src/gz/bufread.rs:202:13 > | > 202 | (crc.amount() >> 0) as u8, > | ^^^^^^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `(buf[0] as u32)` > --> src/gz/bufread.rs:233:15 > | > 233 | let crc = ((buf[0] as u32) << 0) > | ^^^^^^^^^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `(buf[4] as u32)` > --> src/gz/bufread.rs:237:15 > | > 237 | let amt = ((buf[4] as u32) << 0) > | ^^^^^^^^^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `sum` > --> src/gz/write.rs:102:17 > | > 102 | (sum >> 0) as u8, > | ^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `amt` > --> src/gz/write.rs:106:17 > | > 106 | (amt >> 0) as u8, > | ^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `(self.crc_bytes[0] as u32)` > --> src/gz/write.rs:307:19 > | > 307 | let crc = ((self.crc_bytes[0] as u32) << 0) > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `(self.crc_bytes[4] as u32)` > --> src/gz/write.rs:311:19 > | > 311 | let amt = ((self.crc_bytes[4] as u32) << 0) > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: the operation is ineffective. Consider reducing it to `v.len()` > --> src/gz/mod.rs:215:25 > | > 215 | header.push((v.len() >> 0) as u8); > | ^^^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: you are using an explicit closure for copying elements > --> src/gz/mod.rs:221:27 > | > 221 | header.extend(filename.as_bytes_with_nul().iter().map(|x| *x)); > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `filename.as_bytes_with_nul().iter().copied()` > | > = note: `#[warn(clippy::map_clone)]` on by default > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone > > warning: you are using an explicit closure for copying elements > --> src/gz/mod.rs:225:27 > | > 225 | header.extend(comment.as_bytes_with_nul().iter().map(|x| *x)); > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `copied` method: `comment.as_bytes_with_nul().iter().copied()` > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone > > warning: the operation is ineffective. Consider reducing it to `mtime` > --> src/gz/mod.rs:231:21 > | > 231 | header[4] = (mtime >> 0) as u8; > | ^^^^^^^^^^^^ > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op > > warning: use of `offset` with a `usize` casted to an `isize` > --> src/mem.rs:365:27 > | > 365 | let ptr = output.as_mut_ptr().offset(len as isize); > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `output.as_mut_ptr().add(len)` > | > = note: `#[warn(clippy::ptr_offset_with_cast)]` on by default > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast > > warning: use of `offset` with a `usize` casted to an `isize` > --> src/mem.rs:506:27 > | > 506 | let ptr = output.as_mut_ptr().offset(len as isize); > | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `output.as_mut_ptr().add(len)` > | > = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast > > warning: `flate2` (lib) generated 18 warnings > Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I ran
cargo clippy
and addressed some issues it raised, while leaving other issues for another time. Hopefully this will make this code a bit easier for newcomers to read.Biggest changes:
foo.len() > 0
➡!foo.is_empty()
return x;
➡x
{ foo: foo }
➡{ foo }
Default
trait implementationsmatch
statement cleanupAlso, fixed two
ffi::uInt::max_value()
➡ffi::uInt::MAX
due to deprecation.Biggest TODOs (please give feedback if they should be fixed too):
foo >> 0
-- which is a noop that might confuse (?)