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

Fix some clippy warnings #948

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 examples/a-chat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>

fn main() -> Result<()> {
let mut args = std::env::args();
match (args.nth(1).as_ref().map(String::as_str), args.next()) {
match (args.nth(1).as_deref(), args.next()) {
(Some("client"), None) => client::main(),
(Some("server"), None) => server::main(),
_ => Err("Usage: a-chat [client|server]".into()),
Expand Down
3 changes: 2 additions & 1 deletion src/fs/dir_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ impl DirBuilder {
///
/// let builder = DirBuilder::new();
/// ```
pub fn new() -> DirBuilder {
#[must_use]
pub const fn new() -> DirBuilder {
Copy link
Member

Choose a reason for hiding this comment

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

What is the benefit of making this const?

Copy link
Author

Choose a reason for hiding this comment

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

The benefit of making this const is that DirBuilder::new can now be used in const declarations, as well as anything that depends on it also now has a chance to be made const.

#[cfg(not(unix))]
let builder = DirBuilder { recursive: false };

Expand Down
2 changes: 2 additions & 0 deletions src/fs/dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl DirEntry {
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn path(&self) -> PathBuf {
self.0.path().into()
}
Expand Down Expand Up @@ -147,6 +148,7 @@ impl DirEntry {
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn file_name(&self) -> OsString {
self.0.file_name()
}
Expand Down
4 changes: 3 additions & 1 deletion src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ impl LockGuard<State> {

// This function does nothing because we're not sure about `AsyncWrite::poll_close()`'s exact
// semantics nor whether it will stay in the `AsyncWrite` trait.
#[allow(clippy::unused_self)]
fn poll_close(self, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
Expand Down Expand Up @@ -899,7 +900,8 @@ mod tests {
drop(clone);
buf.len()
})
}).await;
})
.await;
assert_eq!(len as u64, file.metadata().await.unwrap().len());
});
}
Expand Down
1 change: 1 addition & 0 deletions src/fs/open_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl OpenOptions {
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn new() -> OpenOptions {
OpenOptions(std::fs::OpenOptions::new())
}
Expand Down
2 changes: 1 addition & 1 deletion src/fs/read_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ enum State {

impl ReadDir {
/// Creates an asynchronous `ReadDir` from a synchronous handle.
pub(crate) fn new(inner: std::fs::ReadDir) -> ReadDir {
pub(crate) const fn new(inner: std::fs::ReadDir) -> ReadDir {
Copy link
Member

Choose a reason for hiding this comment

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

Can this even be const? Even inner will be different, how would it generate code to account for that?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure I understand your reasoning here

ReadDir(State::Idle(Some(inner)))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/io/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<R> BufReader<R> {
/// #
/// # Ok(()) }) }
/// ```
pub fn get_ref(&self) -> &R {
pub const fn get_ref(&self) -> &R {
&self.inner
}

Expand Down
4 changes: 2 additions & 2 deletions src/io/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl<W: Write> BufWriter<W> {
self.project().inner
}

/// Consumes BufWriter, returning the underlying writer
/// Consumes `BufWriter`, returning the underlying writer
///
/// This method will not write leftover data, it will be lost.
/// For method that will attempt to write before returning the writer see [`poll_into_inner`]
Expand Down Expand Up @@ -263,7 +263,7 @@ impl<W: Write> BufWriter<W> {

/// Poll buffer flushing until completion
///
/// This is used in types that wrap around BufWrite, one such example: [`LineWriter`]
/// This is used in types that wrap around `BufWrite`, one such example: [`LineWriter`]
///
/// [`LineWriter`]: struct.LineWriter.html
fn poll_flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Expand Down
3 changes: 2 additions & 1 deletion src/io/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use crate::task::{Context, Poll};
/// #
/// # Ok(()) }) }
/// ```
pub fn empty() -> Empty {
#[must_use]
pub const fn empty() -> Empty {
Empty { _private: () }
}

Expand Down
6 changes: 3 additions & 3 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
//! of 'a type that implements the [`Read`] trait'. Much easier!
//!
//! ## Seek and BufRead
//! ## `Seek` and `BufRead`
//!
//! Beyond that, there are two important traits that are provided: [`Seek`]
//! and [`BufRead`]. Both of these build on top of a reader to control
Expand Down Expand Up @@ -70,7 +70,7 @@
//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
//! to show it off, we'll need to talk about buffers in general. Keep reading!
//!
//! ## BufReader and BufWriter
//! ## `BufReader` and `BufWriter`
//!
//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
//! making near-constant calls to the operating system. To help with this,
Expand Down Expand Up @@ -214,7 +214,7 @@
//!
//! [functions-list]: #functions-1
//!
//! ## io::Result
//! ## `io::Result`
//!
//! Last, but certainly not least, is [`io::Result`]. This type is used
//! as the return type of many `std::io` functions that can cause an error, and
Expand Down
8 changes: 4 additions & 4 deletions src/io/read/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<T, U> Chain<T, U> {
/// #
/// # Ok(()) }) }
/// ```
pub fn get_ref(&self) -> (&T, &U) {
pub const fn get_ref(&self) -> (&T, &U) {
(&self.first, &self.second)
}

Expand Down Expand Up @@ -157,10 +157,10 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {

fn consume(self: Pin<&mut Self>, amt: usize) {
let this = self.project();
if !*this.done_first {
this.first.consume(amt)
} else {
if *this.done_first {
this.second.consume(amt)
} else {
this.first.consume(amt)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/io/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ mod tests {
#[test]
fn test_read_by_ref() {
crate::task::block_on(async {
let mut f = io::Cursor::new(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8]);
let mut f = io::Cursor::new(vec![0_u8, 1, 2, 3, 4, 5, 6, 7, 8]);
Copy link
Member

Choose a reason for hiding this comment

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

This seems like churn...

Copy link
Author

Choose a reason for hiding this comment

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

Just more readable, and idiomatic/conventional to have an underscore in Rust. I agree that its not strictly necessary at all

let mut buffer = Vec::new();
let mut other_buffer = Vec::new();

Expand Down
4 changes: 2 additions & 2 deletions src/io/read/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<T> Take<T> {
/// #
/// # Ok(()) }) }
/// ```
pub fn limit(&self) -> u64 {
pub const fn limit(&self) -> u64 {
self.limit
}

Expand Down Expand Up @@ -229,7 +229,7 @@ mod tests {
let source: io::Cursor<Vec<u8>> = io::Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);

task::block_on(async move {
let mut buffer = [0u8; 5];
let mut buffer = [0_u8; 5];

// read at most five bytes
let mut handle = source.take(5);
Expand Down
3 changes: 2 additions & 1 deletion src/io/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use crate::task::{Context, Poll};
/// #
/// # Ok(()) }) }
/// ```
pub fn repeat(byte: u8) -> Repeat {
#[must_use]
pub const fn repeat(byte: u8) -> Repeat {
Repeat { byte }
}

Expand Down
3 changes: 2 additions & 1 deletion src/io/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use crate::task::{Context, Poll};
/// #
/// # Ok(()) }) }
/// ```
pub fn sink() -> Sink {
#[must_use]
pub const fn sink() -> Sink {
Sink { _private: () }
}

Expand Down
20 changes: 10 additions & 10 deletions src/io/stderr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex;
use std::future::Future;

use crate::io::{self, Write};
use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
Expand Down Expand Up @@ -30,6 +30,7 @@ use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn stderr() -> Stderr {
Stderr(Mutex::new(State::Idle(Some(Inner {
stderr: std::io::stderr(),
Expand Down Expand Up @@ -150,16 +151,15 @@ impl Write for Stderr {
// Check if the operation has completed.
if let Some(Operation::Flush(res)) = inner.last_op.take() {
return Poll::Ready(res);
} else {
let mut inner = opt.take().unwrap();

// Start the operation asynchronously.
*state = State::Busy(spawn_blocking(move || {
let res = std::io::Write::flush(&mut inner.stderr);
inner.last_op = Some(Operation::Flush(res));
State::Idle(Some(inner))
}));
}
let mut inner = opt.take().unwrap();

// Start the operation asynchronously.
*state = State::Busy(spawn_blocking(move || {
let res = std::io::Write::flush(&mut inner.stderr);
inner.last_op = Some(Operation::Flush(res));
State::Idle(Some(inner))
}));
}
// Poll the asynchronous operation the stderr is currently blocked on.
State::Busy(task) => *state = futures_core::ready!(Pin::new(task).poll(cx)),
Expand Down
20 changes: 10 additions & 10 deletions src/io/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::utils::Context as _;
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn stdin() -> Stdin {
Stdin(Mutex::new(State::Idle(Some(Inner {
stdin: std::io::stdin(),
Expand Down Expand Up @@ -125,17 +126,16 @@ impl Stdin {
// Copy the read data into the buffer and return.
buf.push_str(&inner.line);
return Poll::Ready(Ok(n));
} else {
let mut inner = opt.take().unwrap();

// Start the operation asynchronously.
*state = State::Busy(spawn_blocking(move || {
inner.line.clear();
let res = inner.stdin.read_line(&mut inner.line);
inner.last_op = Some(Operation::ReadLine(res));
State::Idle(Some(inner))
}));
}
let mut inner = opt.take().unwrap();

// Start the operation asynchronously.
*state = State::Busy(spawn_blocking(move || {
inner.line.clear();
let res = inner.stdin.read_line(&mut inner.line);
inner.last_op = Some(Operation::ReadLine(res));
State::Idle(Some(inner))
}));
}
// Poll the asynchronous operation the stdin is currently blocked on.
State::Busy(task) => *state = futures_core::ready!(Pin::new(task).poll(cx)),
Expand Down
20 changes: 10 additions & 10 deletions src/io/stdout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex;
use std::future::Future;

use crate::io::{self, Write};
use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
Expand Down Expand Up @@ -30,6 +30,7 @@ use crate::task::{spawn_blocking, Context, JoinHandle, Poll};
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn stdout() -> Stdout {
Stdout(Mutex::new(State::Idle(Some(Inner {
stdout: std::io::stdout(),
Expand Down Expand Up @@ -150,16 +151,15 @@ impl Write for Stdout {
// Check if the operation has completed.
if let Some(Operation::Flush(res)) = inner.last_op.take() {
return Poll::Ready(res);
} else {
let mut inner = opt.take().unwrap();

// Start the operation asynchronously.
*state = State::Busy(spawn_blocking(move || {
let res = std::io::Write::flush(&mut inner.stdout);
inner.last_op = Some(Operation::Flush(res));
State::Idle(Some(inner))
}));
}
let mut inner = opt.take().unwrap();

// Start the operation asynchronously.
*state = State::Busy(spawn_blocking(move || {
let res = std::io::Write::flush(&mut inner.stdout);
inner.last_op = Some(Operation::Flush(res));
State::Idle(Some(inner))
}));
}
// Poll the asynchronous operation the stdout is currently blocked on.
State::Busy(task) => *state = futures_core::ready!(Pin::new(task).poll(cx)),
Expand Down
2 changes: 1 addition & 1 deletion src/io/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
let this = self.project();
match this.future.poll(cx) {
Poll::Pending => {}
other => return other,
other @ Poll::Ready(..) => return other,
Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary, if Poll were to expand there would be issues anyways and that would almost certainly have to happen in a rust edition.

Copy link
Author

Choose a reason for hiding this comment

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

I think the goal here is to make clearer to readers that the only possibility is Poll::Ready(..), rather than being unclear about the possibly multiple cases it covers.

}

if this.timeout.poll(cx).is_ready() {
Expand Down
1 change: 1 addition & 0 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl TcpListener {
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn incoming(&self) -> Incoming<'_> {
Incoming {
incoming: Box::pin(self.watcher.incoming()),
Expand Down
1 change: 1 addition & 0 deletions src/os/unix/net/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl UnixListener {
/// #
/// # Ok(()) }) }
/// ```
#[must_use]
pub fn incoming(&self) -> Incoming<'_> {
Incoming {
incoming: Box::pin(self.watcher.incoming()),
Expand Down
2 changes: 1 addition & 1 deletion src/path/ancestors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::path::Path;
///
/// [`ancestors`]: struct.Path.html#method.ancestors
/// [`Path`]: struct.Path.html
#[derive(Copy, Clone, Debug)]
Copy link
Member

Choose a reason for hiding this comment

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

This is a breaking change. What is the reasoning?

Copy link
Author

Choose a reason for hiding this comment

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

I think this might have been a mistake, I'll revert.

#[derive(Clone, Debug)]
pub struct Ancestors<'a> {
pub(crate) next: Option<&'a Path>,
}
Expand Down
1 change: 1 addition & 0 deletions src/path/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl<'a> Components<'a> {
///
/// assert_eq!(Path::new("foo/bar.txt"), components.as_path());
/// ```
#[must_use]
pub fn as_path(&self) -> &'a Path {
self.inner.as_path().into()
}
Expand Down
1 change: 1 addition & 0 deletions src/path/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl<'a> Iter<'a> {
///
/// assert_eq!(Path::new("foo/bar.txt"), iter.as_path());
/// ```
#[must_use]
pub fn as_path(&self) -> &'a Path {
self.inner.as_path()
}
Expand Down
Loading