Skip to content

Commit

Permalink
chore: happy clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades authored Jul 28, 2021
1 parent 0a185f8 commit 33b9b42
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 56 deletions.
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.51"
17 changes: 17 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,28 @@ jobs:
profile: minimal
toolchain: nightly
components: rustfmt
override: true
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

lint:
name: Clippy Linting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: clippy
override: true
- uses: actions-rs/cargo@v1
with:
command: clippy
args: --all

docs:
name: Check docs
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use_field_init_shorthand = true
17 changes: 8 additions & 9 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<R: Read + Unpin> Archive<R> {
preserve_permissions: false,
preserve_mtime: true,
ignore_zeros: false,
obj: obj,
obj,
pos: 0,
})),
}
Expand Down Expand Up @@ -268,7 +268,7 @@ impl<R: Read + Unpin> Stream for Entries<R> {
let archive = self.archive.clone();
let (next, current_header, current_header_pos, _) = &mut self.current;
let entry = ready_opt_err!(poll_next_raw(
archive,
&archive,
next,
current_header,
current_header_pos,
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<R: Read + Unpin> Stream for Entries<R> {
let archive = self.archive.clone();
let (next, _, current_pos, current_ext) = &mut self.current;
ready_err!(poll_parse_sparse_header(
archive,
&archive,
next,
current_ext,
current_pos,
Expand All @@ -350,12 +350,12 @@ impl<R: Read + Unpin> Stream for RawEntries<R> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let archive = self.archive.clone();
let (next, current_header, current_header_pos) = &mut self.current;
poll_next_raw(archive, next, current_header, current_header_pos, cx)
poll_next_raw(&archive, next, current_header, current_header_pos, cx)
}
}

fn poll_next_raw<R: Read + Unpin>(
archive: Archive<R>,
archive: &Archive<R>,
next: &mut u64,
current_header: &mut Option<Header>,
current_header_pos: &mut usize,
Expand Down Expand Up @@ -424,7 +424,6 @@ fn poll_next_raw<R: Read + Unpin>(
let size = header.entry_size()?;

let data = EntryIo::Data(archive.clone().take(size));
drop(header);

let header = current_header.take().unwrap();

Expand Down Expand Up @@ -459,7 +458,7 @@ fn poll_next_raw<R: Read + Unpin>(
}

fn poll_parse_sparse_header<R: Read + Unpin>(
archive: Archive<R>,
archive: &Archive<R>,
next: &mut u64,
current_ext: &mut Option<GnuExtSparseHeader>,
current_ext_pos: &mut usize,
Expand Down Expand Up @@ -535,7 +534,7 @@ fn poll_parse_sparse_header<R: Read + Unpin>(
data.push(EntryIo::Data(reader.clone().take(len)));
Ok(())
};
for block in gnu.sparse.iter() {
for block in &gnu.sparse {
add_block(block)?
}
if gnu.is_extended() {
Expand All @@ -561,7 +560,7 @@ fn poll_parse_sparse_header<R: Read + Unpin>(
}

*next += 512;
for block in ext.sparse.iter() {
for block in &ext.sparse {
add_block(block)?;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<W: Write + Unpin + Send + Sync> Builder<W> {
) -> io::Result<()> {
prepare_header_path(self.get_mut(), header, path.as_ref()).await?;
header.set_cksum();
self.append(&header, data).await?;
self.append(header, data).await?;

Ok(())
}
Expand Down Expand Up @@ -524,7 +524,7 @@ async fn prepare_header_path(
// long name extension by emitting an entry which indicates that it's the
// filename.
if let Err(e) = header.set_path(path) {
let data = path2bytes(&path)?;
let data = path2bytes(path)?;
let max = header.as_old().name.len();
// Since e isn't specific enough to let us know the path is indeed too
// long, verify it first before using the extension.
Expand All @@ -550,7 +550,7 @@ async fn prepare_header_link(
) -> io::Result<()> {
// Same as previous function but for linkname
if let Err(e) = header.set_link_name(&link_name) {
let data = path2bytes(&link_name)?;
let data = path2bytes(link_name)?;
if data.len() < header.as_old().linkname.len() {
return Err(e);
}
Expand Down
48 changes: 22 additions & 26 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,23 @@ impl<R: Read + Unpin> EntryFields<R> {
}

fn path_bytes(&self) -> Cow<[u8]> {
match self.long_pathname {
Some(ref bytes) => {
if let Some(&0) = bytes.last() {
Cow::Borrowed(&bytes[..bytes.len() - 1])
} else {
Cow::Borrowed(bytes)
}
if let Some(ref bytes) = self.long_pathname {
if let Some(&0) = bytes.last() {
Cow::Borrowed(&bytes[..bytes.len() - 1])
} else {
Cow::Borrowed(bytes)
}
None => {
if let Some(ref pax) = self.pax_extensions {
let pax = pax_extensions(pax)
.filter_map(|f| f.ok())
.find(|f| f.key_bytes() == b"path")
.map(|f| f.value_bytes());
if let Some(field) = pax {
return Cow::Borrowed(field);
}
} else {
if let Some(ref pax) = self.pax_extensions {
let pax = pax_extensions(pax)
.filter_map(Result::ok)
.find(|f| f.key_bytes() == b"path")
.map(|f| f.value_bytes());
if let Some(field) = pax {
return Cow::Borrowed(field);
}
self.header.path_bytes()
}
self.header.path_bytes()
}
}

Expand Down Expand Up @@ -479,7 +476,7 @@ impl<R: Read + Unpin> EntryFields<R> {
})?;
}

let canon_target = self.validate_inside_dst(&dst, parent).await?;
let canon_target = self.validate_inside_dst(dst, parent).await?;

self.unpack(Some(&canon_target), &file_dst)
.await
Expand Down Expand Up @@ -549,7 +546,7 @@ impl<R: Read + Unpin> EntryFields<R> {
// use canonicalization to ensure this guarantee. For hard
// links though they're canonicalized to their existing path
// so we need to validate at this time.
Some(ref p) => {
Some(p) => {
let link_src = p.join(src);
self.validate_inside_dst(p, &link_src).await?;
link_src
Expand Down Expand Up @@ -638,14 +635,14 @@ impl<R: Read + Unpin> EntryFields<R> {
let mut f = match open(dst).await {
Ok(f) => Ok(f),
Err(err) => {
if err.kind() != ErrorKind::AlreadyExists {
Err(err)
} else {
if err.kind() == ErrorKind::AlreadyExists {
match fs::remove_file(dst).await {
Ok(()) => open(dst).await,
Err(ref e) if e.kind() == io::ErrorKind::NotFound => open(dst).await,
Err(e) => Err(e),
}
} else {
Err(err)
}
}
}?;
Expand Down Expand Up @@ -779,7 +776,7 @@ impl<R: Read + Unpin> EntryFields<R> {
_ => return Ok(()),
};
let exts = exts
.filter_map(|e| e.ok())
.filter_map(Result::ok)
.filter_map(|e| {
let key = e.key_bytes();
let prefix = b"SCHILY.xattr.";
Expand Down Expand Up @@ -888,10 +885,9 @@ impl<R: Read + Unpin> Read for EntryFields<R> {
return Poll::Pending;
}
}
} else {
// Unable to pull another value from `data`, so we are done.
return Poll::Ready(Ok(0));
}
// Unable to pull another value from `data`, so we are done.
return Poll::Ready(Ok(0));
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Header {
if self.entry_type().is_gnu_sparse() {
self.as_gnu()
.ok_or_else(|| other("sparse header was not a gnu header"))
.and_then(|h| h.real_size())
.and_then(GnuHeader::real_size)
} else {
self.entry_size()
}
Expand Down Expand Up @@ -397,10 +397,10 @@ impl Header {
/// separators.
pub fn link_name_bytes(&self) -> Option<Cow<[u8]>> {
let old = self.as_old();
if old.linkname[0] != 0 {
Some(Cow::Borrowed(truncate(&old.linkname)))
} else {
if old.linkname[0] == 0 {
None
} else {
Some(Cow::Borrowed(truncate(&old.linkname)))
}
}

Expand Down Expand Up @@ -1103,8 +1103,8 @@ impl GnuHeader {
fn fullname_lossy(&self) -> String {
format!(
"{}:{}",
String::from_utf8_lossy(&self.groupname_bytes()),
String::from_utf8_lossy(&self.username_bytes()),
String::from_utf8_lossy(self.groupname_bytes()),
String::from_utf8_lossy(self.username_bytes()),
)
}

Expand Down Expand Up @@ -1406,10 +1406,10 @@ fn num_field_wrapper_into(dst: &mut [u8], src: u64) {
// Wrapper to figure out if we should read the header field in binary (numeric
// extension) or octal (standard encoding).
fn num_field_wrapper_from(src: &[u8]) -> io::Result<u64> {
if src[0] & 0x80 != 0 {
Ok(numeric_extended_from(src))
} else {
if src[0] & 0x80 == 0 {
octal_from(src)
} else {
Ok(numeric_extended_from(src))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// all just super thin wrappers (e.g. easy to codegen).

#![deny(missing_docs)]
#![deny(clippy::all)]

use std::io::{Error, ErrorKind};

Expand Down
10 changes: 5 additions & 5 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ async fn large_filename() {
let path = td.path().join("test");
t!(t!(File::create(&path).await).write_all(b"test").await);

let filename = repeat("abcd/").take(50).collect::<String>();
let filename = "abcd/".repeat(50);
let mut header = Header::new_ustar();
header.set_path(&filename).unwrap();
header.set_metadata(&t!(fs::metadata(&path).await));
header.set_cksum();
t!(ar.append(&header, &b"test"[..]).await);
let too_long = repeat("abcd").take(200).collect::<String>();
let too_long = "abcd".repeat(200);
t!(ar
.append_file(&too_long, &mut t!(File::open(&path).await))
.await);
Expand Down Expand Up @@ -808,7 +808,7 @@ async fn encoded_long_name_has_trailing_nul() {
t!(t!(File::create(&path).await).write_all(b"test").await);

let mut b = Builder::new(Vec::<u8>::new());
let long = repeat("abcd").take(200).collect::<String>();
let long = "abcd".repeat(200);

t!(b.append_file(&long, &mut t!(File::open(&path).await)).await);

Expand Down Expand Up @@ -978,8 +978,8 @@ async fn append_path_symlink() {
ar.follow_symlinks(false);
let td = t!(TempBuilder::new().prefix("async-tar").tempdir());

let long_linkname = repeat("abcd").take(30).collect::<String>();
let long_pathname = repeat("dcba").take(30).collect::<String>();
let long_linkname = "abcd".repeat(30);
let long_pathname = "dcba".repeat(30);
t!(env::set_current_dir(td.path()));
// "short" path name / short link name
t!(symlink("testdest", "test"));
Expand Down
8 changes: 4 additions & 4 deletions tests/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
fs::{self, File},
io::{self, Write},
iter, mem,
mem,
path::Path,
thread, time,
};
Expand Down Expand Up @@ -144,9 +144,9 @@ fn set_path() {
assert_eq!(t!(h.path()).to_str(), Some("foo\\bar"));
}

let long_name = iter::repeat("foo").take(100).collect::<String>();
let medium1 = iter::repeat("foo").take(52).collect::<String>();
let medium2 = iter::repeat("fo/").take(52).collect::<String>();
let long_name = "foo".repeat(100);
let medium1 = "foo".repeat(52);
let medium2 = "fo/".repeat(52);

assert!(h.set_path(&long_name).is_err());
assert!(h.set_path(&medium1).is_err());
Expand Down

0 comments on commit 33b9b42

Please sign in to comment.