Skip to content

Commit

Permalink
Fix clippy warnings (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman authored Apr 12, 2023
1 parent b3dbb48 commit 5589dad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
34 changes: 15 additions & 19 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ pub enum FileOp {

impl FileOp {
pub fn is_new_or_deleted(self) -> bool {
match self {
FileOp::New(_) => true,
FileOp::Deleted(_) => true,
_ => false,
}
matches!(self, FileOp::New(_) | FileOp::Deleted(_))
}
}

Expand Down Expand Up @@ -188,14 +184,14 @@ impl<'a> LineReader<'a> {
// NUMS_PAT = re.compile(r'^@@ -([0-9]+),?([0-9]+)? \+([0-9]+),?([0-9]+)? @@')
let iter = &mut buf.iter();
let old_start = iter
.take_while(|&&c| c >= b'0' && c <= b'9')
.take_while(|&&c| c.is_ascii_digit())
.fold(0, |r, &c| r * 10 + u32::from(c - b'0'));

let c = *iter
.next()
.ok_or_else(|| ParsepatchError::InvalidHunkHeader(self.get_line()))?;
let old_lines = if c >= b'0' && c <= b'9' {
iter.take_while(|&&c| c >= b'0' && c <= b'9')
let old_lines = if c.is_ascii_digit() {
iter.take_while(|&&c| c.is_ascii_digit())
.fold(u32::from(c - b'0'), |r, &c| r * 10 + u32::from(c - b'0'))
} else {
1
Expand All @@ -206,14 +202,14 @@ impl<'a> LineReader<'a> {
}

let new_start = iter
.take_while(|&&c| c >= b'0' && c <= b'9')
.take_while(|&&c| c.is_ascii_digit())
.fold(0, |r, &c| r * 10 + u32::from(c - b'0'));

let c = *iter
.next()
.ok_or_else(|| ParsepatchError::InvalidHunkHeader(self.get_line()))?;
let new_lines = if c >= b'0' && c <= b'9' {
iter.take_while(|&&c| c >= b'0' && c <= b'9')
let new_lines = if c.is_ascii_digit() {
iter.take_while(|&&c| c.is_ascii_digit())
.fold(u32::from(c - b'0'), |r, &c| r * 10 + u32::from(c - b'0'))
} else {
1
Expand All @@ -226,7 +222,7 @@ impl<'a> LineReader<'a> {
let mut iter = buf.iter();
let pos1 = iter
.position(|c| *c != b' ')
.ok_or_else(|| ParsepatchError::NoFilename(line))?;
.ok_or(ParsepatchError::NoFilename(line))?;
let pos2 = iter.position(|c| *c == b'\t');
let buf = if let Some(pos2) = pos2 {
unsafe { buf.get_unchecked(pos1..=pos1 + pos2) }
Expand Down Expand Up @@ -290,7 +286,7 @@ impl<'a> LineReader<'a> {
// the following number is an octal number with 6 digits (so max is 8^6 - 1)
let buf = unsafe { self.buf.get_unchecked(start.len()..) };
buf.iter()
.take_while(|&&c| c >= b'0' && c <= b'7')
.take_while(|&&c| (b'0'..=b'7').contains(&c))
.fold(0, |r, &c| r * 8 + u32::from(c - b'0'))
}
}
Expand Down Expand Up @@ -659,7 +655,7 @@ impl<'a> PatchReader<'a> {

fn parse_usize(buf: &[u8]) -> usize {
buf.iter()
.take_while(|&&c| c >= b'0' && c <= b'9')
.take_while(|&&c| c.is_ascii_digit())
.fold(0, |r, &c| r * 10 + usize::from(c - b'0'))
}

Expand All @@ -683,11 +679,11 @@ impl<'a> PatchReader<'a> {
}

fn useful(line: &LineReader) -> bool {
line.is_binary() || line.is_triple_minus() || Self::diff(&line)
line.is_binary() || line.is_triple_minus() || Self::diff(line)
}

fn starter(line: &LineReader) -> bool {
line.is_triple_minus() || Self::diff(&line)
line.is_triple_minus() || Self::diff(line)
}

fn diff(line: &LineReader) -> bool {
Expand All @@ -707,7 +703,7 @@ impl<'a> PatchReader<'a> {
}

fn hunk_change(line: &LineReader) -> bool {
if let Some(c) = line.buf.get(0) {
if let Some(c) = line.buf.first() {
let c = *c;
c == b'-' || c == b'+' || c == b' ' || line.buf.starts_with(b"\\ No newline")
} else {
Expand All @@ -731,7 +727,7 @@ mod tests {
];
for c in cases.iter() {
let buf = c.0.as_bytes();
let line = LineReader { buf: &buf, line: 1 };
let line = LineReader { buf: buf, line: 1 };
assert_eq!(line.parse_numbers().unwrap(), c.1);
}
}
Expand All @@ -757,7 +753,7 @@ mod tests {
let cases = [("+++ hello", "+++"), ("+++ hello", "++++")];
for c in cases.iter() {
let buf = c.0.as_bytes();
let line = LineReader { buf: &buf, line: 1 };
let line = LineReader { buf: buf, line: 1 };
let pat = c.1.as_bytes();
assert!(line.buf.starts_with(pat) == c.0.starts_with(c.1));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/mc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Diff for DiffImpl {
}

fn get_log(path: &str, range: &str) -> Vec<String> {
let mut client = Client::open(&path, "UTF-8", &[]).unwrap();
let mut client = Client::open(path, "UTF-8", &[]).unwrap();
let range = [range];
let (mut data, _) = runcommand!(
client,
Expand Down
6 changes: 2 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,10 @@ impl Diff for DiffImpl {
self.binary = binary_sizes.is_some();

if self.file_mode.is_none() {
self.file_mode = file_mode.and_then(|c| {
Some(FileModeChange {
self.file_mode = file_mode.map(|c| FileModeChange {
old: c.old,
new: c.new,
})
});
});
}
}

Expand Down

0 comments on commit 5589dad

Please sign in to comment.