Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
* clippy::needless_lifetimes
* clippy::unnecessary_mut_passed
* clippy::assign_op_pattern
* clippy::while_let_on_iterator
* clippy::let_and_return
* clippy::needless_range_loop
* clippy::len_zero
* clippy::single_match
* clippy::match_bool
* clippy::redundant_pattern_matching
* clippy::needless_return
* clippy::collapsible_if
* clippy::let_unit_value
* clippy::unit_arg
* clippy::duration_subsec
* clippy::redundant_closure
* clippy::new_without_default
* clippy::option_map_unit_fn
* clippy::cast_lossless
  • Loading branch information
taiki-e committed Jul 21, 2019
1 parent cd543bf commit 0cdc174
Show file tree
Hide file tree
Showing 42 changed files with 156 additions and 67 deletions.
4 changes: 2 additions & 2 deletions tokio-codec/src/bytes_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bytes::{BufMut, Bytes, BytesMut};
use std::io;

/// A simple `Codec` implementation that just ships bytes around.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct BytesCodec(());

impl BytesCodec {
Expand All @@ -19,7 +19,7 @@ impl Decoder for BytesCodec {
type Error = io::Error;

fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, io::Error> {
if buf.len() > 0 {
if !buf.is_empty() {
let len = buf.len();
Ok(Some(buf.split_to(len)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion tokio-codec/src/framed_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn framed_read2_with_buffer<T>(inner: T, mut buf: BytesMut) -> FramedRead2<T
FramedRead2 {
inner,
eof: false,
is_readable: buf.len() > 0,
is_readable: !buf.is_empty(),
buffer: buf,
}
}
Expand Down
6 changes: 6 additions & 0 deletions tokio-codec/src/lines_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ impl Encoder for LinesCodec {
}
}

impl Default for LinesCodec {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug)]
pub enum LinesCodecError {
MaxLineLengthExceeded,
Expand Down
14 changes: 10 additions & 4 deletions tokio-current-thread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<P: Park> CurrentThread<P> {
}

/// Bind `CurrentThread` instance with an execution context.
fn enter<'a>(&'a mut self) -> Entered<'a, P> {
fn enter(&mut self) -> Entered<'_, P> {
Entered { executor: self }
}

Expand Down Expand Up @@ -429,6 +429,12 @@ impl<P: Park> fmt::Debug for CurrentThread<P> {
}
}

impl<P: Park + Default> Default for CurrentThread<P> {
fn default() -> Self {
CurrentThread::new_with_park(P::default())
}
}

// ===== impl Entered =====

impl<'a, P: Park> Entered<'a, P> {
Expand Down Expand Up @@ -483,7 +489,7 @@ impl<'a, P: Park> Entered<'a, P> {

self.tick();

if let Err(_) = self.executor.park.park() {
if self.executor.park.park().is_err() {
panic!("block_on park failed");
}
}
Expand Down Expand Up @@ -550,7 +556,7 @@ impl<'a, P: Park> Entered<'a, P> {

match time {
Some((until, rem)) => {
if let Err(_) = self.executor.park.park_timeout(rem) {
if self.executor.park.park_timeout(rem).is_err() {
return Err(RunTimeoutError::new(false));
}

Expand All @@ -563,7 +569,7 @@ impl<'a, P: Park> Entered<'a, P> {
time = Some((until, until - now));
}
None => {
if let Err(_) = self.executor.park.park() {
if self.executor.park.park().is_err() {
return Err(RunTimeoutError::new(false));
}
}
Expand Down
12 changes: 5 additions & 7 deletions tokio-current-thread/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,8 @@ impl<U> Inner<U> {
let tail = *self.tail_readiness.get();
let next = (*tail).next_readiness.load(Acquire);

if tail == self.stub() {
if next.is_null() {
return false;
}
if tail == self.stub() && next.is_null() {
return false;
}

true
Expand Down Expand Up @@ -566,7 +564,7 @@ impl<U> List<U> {

self.len += 1;

return ptr;
ptr
}

/// Pop an element from the front of the list
Expand Down Expand Up @@ -618,7 +616,7 @@ impl<U> List<U> {

self.len -= 1;

return node;
node
}
}

Expand Down Expand Up @@ -773,7 +771,7 @@ impl<U> Drop for Node<U> {
fn arc2ptr<T>(ptr: Arc<T>) -> *const T {
let addr = &*ptr as *const T;
mem::forget(ptr);
return addr;
addr
}

unsafe fn ptr2arc<T>(ptr: *const T) -> Arc<T> {
Expand Down
6 changes: 6 additions & 0 deletions tokio-executor/src/park.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ impl Park for ParkThread {
}
}

impl Default for ParkThread {
fn default() -> Self {
Self::new()
}
}

// ===== impl UnparkThread =====

impl Unpark for UnparkThread {
Expand Down
6 changes: 6 additions & 0 deletions tokio-fs/src/file/open_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ impl From<StdOpenOptions> for OpenOptions {
OpenOptions(options)
}
}

impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
4 changes: 2 additions & 2 deletions tokio-io/src/async_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pub trait AsyncRead {
/// [`io::Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
/// [`poll_read_buf`]: #method.poll_read_buf
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
for i in 0..buf.len() {
buf[i] = 0;
for x in buf {
*x = 0;
}

true
Expand Down
13 changes: 5 additions & 8 deletions tokio-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
let mut runtime = RuntimeType::Multi;

for arg in args {
match arg {
syn::NestedMeta::Meta(syn::Meta::Word(ident)) => {
match ident.to_string().to_lowercase().as_str() {
"multi_thread" => runtime = RuntimeType::Multi,
"single_thread" => runtime = RuntimeType::Single,
name => panic!("Unknown attribute {} is specified", name),
}
if let syn::NestedMeta::Meta(syn::Meta::Word(ident)) = arg {
match ident.to_string().to_lowercase().as_str() {
"multi_thread" => runtime = RuntimeType::Multi,
"single_thread" => runtime = RuntimeType::Single,
name => panic!("Unknown attribute {} is specified", name),
}
_ => (),
}
}

Expand Down
2 changes: 1 addition & 1 deletion tokio-reactor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl Reactor {
"loop process - {} events, {}.{:03}s",
events,
dur.as_secs(),
dur.subsec_nanos() / 1_000_000
dur.subsec_millis()
);
}

Expand Down
8 changes: 7 additions & 1 deletion tokio-reactor/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Registration {
where
T: Evented,
{
self.register2(io, || HandlePriv::try_current())
self.register2(io, HandlePriv::try_current)
}

/// Deregister the I/O resource from the reactor it is associated with.
Expand Down Expand Up @@ -416,6 +416,12 @@ impl Registration {
}
}

impl Default for Registration {
fn default() -> Self {
Self::new()
}
}

unsafe impl Send for Registration {}
unsafe impl Sync for Registration {}

Expand Down
6 changes: 3 additions & 3 deletions tokio-signal/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl<S: Storage> Registry<S> {
/// Mark `event_id` as having been delivered, without broadcasting it to
/// any listeners.
fn record_event(&self, event_id: EventId) {
self.storage
.event_info(event_id)
.map(|event_info| event_info.pending.store(true, Ordering::SeqCst));
if let Some(event_info) = self.storage.event_info(event_id) {
event_info.pending.store(true, Ordering::SeqCst)
}
}

/// Broadcast all previously recorded events to their respective listeners.
Expand Down
2 changes: 2 additions & 0 deletions tokio-sync/src/mpsc/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl<T> Receiver<T> {
}

/// TODO: Dox
#[allow(clippy::needless_lifetimes)] // false positive: https://github.com/rust-lang/rust-clippy/issues/3988
pub async fn recv(&mut self) -> Option<T> {
use async_util::future::poll_fn;

Expand Down Expand Up @@ -204,6 +205,7 @@ impl<T> Sender<T> {
/// ```
/// unimplemented!();
/// ```
#[allow(clippy::needless_lifetimes)] // false positive: https://github.com/rust-lang/rust-clippy/issues/3988
pub async fn send(&mut self, value: T) -> Result<(), SendError> {
use async_util::future::poll_fn;

Expand Down
1 change: 1 addition & 0 deletions tokio-sync/src/mpsc/unbounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl<T> UnboundedReceiver<T> {
}

/// TODO: Dox
#[allow(clippy::needless_lifetimes)] // false positive: https://github.com/rust-lang/rust-clippy/issues/3988
pub async fn recv(&mut self) -> Option<T> {
use async_util::future::poll_fn;

Expand Down
5 changes: 3 additions & 2 deletions tokio-sync/src/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl<T> Sender<T> {
/// ```
/// unimplemented!();
/// ```
#[allow(clippy::needless_lifetimes)] // false positive: https://github.com/rust-lang/rust-clippy/issues/3988
pub async fn closed(&mut self) {
use async_util::future::poll_fn;

Expand Down Expand Up @@ -384,10 +385,10 @@ impl<T> Inner<T> {
None => Ready(Err(RecvError(()))),
}
} else {
return Pending;
Pending
}
} else {
return Pending;
Pending
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions tokio-sync/src/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ impl Permit {
}

match semaphore.poll_permit(Some((cx, self)))? {
Ready(v) => {
Ready(()) => {
self.state = PermitState::Acquired;
Ready(Ok(v))
Ready(Ok(()))
}
Pending => {
self.state = PermitState::Waiting;
Expand Down Expand Up @@ -671,6 +671,12 @@ impl Permit {
}
}

impl Default for Permit {
fn default() -> Self {
Self::new()
}
}

// ===== impl AcquireError ====

impl AcquireError {
Expand Down
2 changes: 1 addition & 1 deletion tokio-sync/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl<T> futures_sink::Sink<T> for Sender<T> {
}

fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
let _ = self.as_ref().get_ref().broadcast(item)?;
self.as_ref().get_ref().broadcast(item)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions tokio-tcp/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl TcpListener {
/// ```
/// unimplemented!();
/// ```
#[allow(clippy::needless_lifetimes)] // false positive: https://github.com/rust-lang/rust-clippy/issues/3988
pub async fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> {
use async_util::future::poll_fn;
poll_fn(|cx| self.poll_accept(cx)).await
Expand Down
6 changes: 3 additions & 3 deletions tokio-tcp/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ pub struct TcpStreamReadHalfMut<'a>(&'a TcpStream);
#[derive(Debug)]
pub struct TcpStreamWriteHalfMut<'a>(&'a TcpStream);

pub(crate) fn split_mut<'a>(
stream: &'a mut TcpStream,
) -> (TcpStreamReadHalfMut<'a>, TcpStreamWriteHalfMut<'a>) {
pub(crate) fn split_mut(
stream: &mut TcpStream,
) -> (TcpStreamReadHalfMut<'_>, TcpStreamWriteHalfMut<'_>) {
(
TcpStreamReadHalfMut(&*stream),
TcpStreamWriteHalfMut(&*stream),
Expand Down
2 changes: 1 addition & 1 deletion tokio-tcp/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ impl TcpStream {
///
/// See the module level documenation of [`split`](super::split) for more
/// details.
pub fn split_mut<'a>(&'a mut self) -> (TcpStreamReadHalfMut<'a>, TcpStreamWriteHalfMut<'a>) {
pub fn split_mut(&mut self) -> (TcpStreamReadHalfMut<'_>, TcpStreamWriteHalfMut<'_>) {
split_mut(self)
}

Expand Down
6 changes: 6 additions & 0 deletions tokio-test/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ impl MockClock {
}
}

impl Default for MockClock {
fn default() -> Self {
Self::new()
}
}

impl Handle {
pub(self) fn new(timer: Timer<MockPark>, time: MockTime) -> Self {
Handle { timer, time }
Expand Down
7 changes: 2 additions & 5 deletions tokio-test/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,8 @@ impl Inner {
return Err(io::ErrorKind::BrokenPipe.into());
}

match self.action() {
Some(&mut Action::Wait(..)) => {
return Err(io::ErrorKind::WouldBlock.into());
}
_ => {}
if let Some(&mut Action::Wait(..)) = self.action() {
return Err(io::ErrorKind::WouldBlock.into());
}

for i in 0..self.actions.len() {
Expand Down
6 changes: 6 additions & 0 deletions tokio-test/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ impl MockTask {
}
}

impl Default for MockTask {
fn default() -> Self {
Self::new()
}
}

impl ThreadWaker {
fn new() -> Self {
ThreadWaker {
Expand Down
6 changes: 6 additions & 0 deletions tokio-threadpool/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,9 @@ impl fmt::Debug for Builder {
.finish()
}
}

impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
6 changes: 6 additions & 0 deletions tokio-threadpool/src/park/default_park.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl Park for DefaultPark {
}
}

impl Default for DefaultPark {
fn default() -> Self {
Self::new()
}
}

// ===== impl DefaultUnpark =====

impl Unpark for DefaultUnpark {
Expand Down
Loading

0 comments on commit 0cdc174

Please sign in to comment.