Skip to content
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

clippy fixes #405

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/iface/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
}
}

fn has_neighbor<'a>(&self, addr: &'a IpAddress, timestamp: Instant) -> bool {
fn has_neighbor(&self, addr: &IpAddress, timestamp: Instant) -> bool {
match self.route(addr, timestamp) {
Ok(routed_addr) => {
self.neighbor_cache
Expand Down
6 changes: 3 additions & 3 deletions src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ impl<'a> Parser<'a> {

fn accept_digit(&mut self, hex: bool) -> Result<u8> {
let digit = self.advance()?;
if digit >= b'0' && digit <= b'9' {
if (b'0'..=b'9').contains(&digit) {

Choose a reason for hiding this comment

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

This might not be such a big deal, but does this generate code that is equally efficient?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not quite.I just tried the following patch in my project and it resulted in a small decrease in size:

diff --git a/src/parsers.rs b/src/parsers.rs
index bf87cdc..36bad29 100644
--- a/src/parsers.rs
+++ b/src/parsers.rs
@@ -88,11 +88,11 @@ impl<'a> Parser<'a> {

     fn accept_digit(&mut self, hex: bool) -> Result<u8> {
         let digit = self.advance()?;
-        if (b'0'..=b'9').contains(&digit) {
+        if digit >= b'0' && digit <= b'9' {
             Ok(digit - b'0')
-        } else if hex && (b'a'..=b'f').contains(&digit) {
+        } else if hex && digit >= b'a' && digit <= b'f' {
             Ok(digit - b'a' + 10)
-        } else if hex && (b'A'..=b'F').contains(&digit) {
+        } else if hex && digit >= b'A' && digit <= b'F' {
             Ok(digit - b'A' + 10)
         } else {
             Err(())

Before this change, the size was as follows:

section                 size        addr
.vector_table            336           0
.text                  41528         336
.rodata                 8496       41864

And after:

section                 size        addr
.vector_table            336           0
.text                  41404         336
.rodata                 8512       41740

Ok(digit - b'0')
} else if hex && digit >= b'a' && digit <= b'f' {
} else if hex && (b'a'..=b'f').contains(&digit) {
Ok(digit - b'a' + 10)
} else if hex && digit >= b'A' && digit <= b'F' {
} else if hex && (b'A'..=b'F').contains(&digit) {
Ok(digit - b'A' + 10)
} else {
Err(())
Expand Down
2 changes: 1 addition & 1 deletion src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ impl<'a> TcpSocket<'a> {
payload_len, payload_offset);
self.rx_buffer.write_unallocated(payload_offset, repr.payload);
}
Err(()) => {
Err(_) => {
net_debug!("{}:{}:{}: assembler: too many holes to add {} octets at offset {}",
self.meta.handle, self.local_endpoint, self.remote_endpoint,
payload_len, payload_offset);
Expand Down
11 changes: 7 additions & 4 deletions src/storage/assembler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use core::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TooManyHolesError;

/// A contiguous chunk of absent data, followed by a contiguous chunk of present data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Contig {
Expand Down Expand Up @@ -148,10 +151,10 @@ impl Assembler {
}

/// Add a contig at the given index, and return a pointer to it.
fn add_contig_at(&mut self, at: usize) -> Result<&mut Contig, ()> {
fn add_contig_at(&mut self, at: usize) -> Result<&mut Contig, TooManyHolesError> {
debug_assert!(!self.contigs[at].is_empty());

if !self.back().is_empty() { return Err(()) }
if !self.back().is_empty() { return Err(TooManyHolesError) }

for i in (at + 1..self.contigs.len()).rev() {
self.contigs[i] = self.contigs[i - 1];
Expand All @@ -163,7 +166,7 @@ impl Assembler {

/// Add a new contiguous range to the assembler, and return `Ok(())`,
/// or return `Err(())` if too many discontiguities are already recorded.
pub fn add(&mut self, mut offset: usize, mut size: usize) -> Result<(), ()> {
pub fn add(&mut self, mut offset: usize, mut size: usize) -> Result<(), TooManyHolesError> {
let mut index = 0;
while index != self.contigs.len() && size != 0 {
let contig = self.contigs[index];
Expand Down Expand Up @@ -413,7 +416,7 @@ mod test {
}
// Maximum of allowed holes is reached
let assr_before = assr.clone();
assert_eq!(assr.add(1, 3), Err(()));
assert_eq!(assr.add(1, 3), Err(TooManyHolesError));
assert_eq!(assr_before, assr);
}

Expand Down
2 changes: 1 addition & 1 deletion src/storage/ring_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
/// or return `Err(Error::Exhausted)` if the buffer is full.
///
/// This function is a shortcut for `ring_buf.enqueue_one_with(Ok)`.
pub fn enqueue_one<'b>(&'b mut self) -> Result<&'b mut T> {
pub fn enqueue_one(&mut self) -> Result<&mut T> {
self.enqueue_one_with(Ok)
}

Expand Down
14 changes: 4 additions & 10 deletions src/wire/ipv6option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,6 @@ impl<'a> Ipv6OptionsIterator<'a> {
length, data
}
}

/// Helper function to return an error in the implementation
/// of `Iterator`.
#[inline]
fn return_err(&mut self, err: Error) -> Option<Result<Repr<'a>>> {
self.hit_error = true;
Some(Err(err))
}
}

impl<'a> Iterator for Ipv6OptionsIterator<'a> {
Expand All @@ -332,12 +324,14 @@ impl<'a> Iterator for Ipv6OptionsIterator<'a> {
Some(Ok(repr))
}
Err(e) => {
self.return_err(e)
self.hit_error = true;
Some(Err(e))
}
}
}
Err(e) => {
self.return_err(e)
self.hit_error = true;
Some(Err(e))
}
}
} else {
Expand Down