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 cleanup #1107

Merged
merged 6 commits into from
Aug 30, 2019
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - ReleaseDate
### Added

- Implemented `Default` for `FdSet`
([#1107](https://github.com/nix-rust/nix/pull/1107))

- Added `NixPath::is_empty`.
([#1107](https://github.com/nix-rust/nix/pull/1107))

### Changed
- Changed `readlink` and `readlinkat` to return `OsString`
([#1109](https://github.com/nix-rust/nix/pull/1109))
Expand All @@ -25,6 +32,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Minimum supported Rust version is now 1.36.0.
([#1108](https://github.com/nix-rust/nix/pull/1108))

- `Ipv4Addr::octets`, `Ipv4Addr::to_std`, `Error::as_errno`,
`ForkResult::is_child`, `ForkResult::is_parent`, `Gid::as_raw`,
`Uid::is_root`, `Uid::as_raw`, `Pid::as_raw`, and `PollFd::revents` now take
`self` by value.
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the motivation for taking self by value? Doesn't that either consume the original value or require Copy? This change was part of the "Clippy cleanup" commit, which doesn't provide any other context.

([#1107](https://github.com/nix-rust/nix/pull/1107))

### Fixed
### Removed

Expand Down
6 changes: 3 additions & 3 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ impl<'d> Iterator for Iter<'d> {
if let Err(e) = Errno::result(readdir_r((self.0).0.as_ptr(), &mut ent.0, &mut result)) {
return Some(Err(e));
}
if result == ptr::null_mut() {
if result.is_null() {
return None;
}
assert_eq!(result, &mut ent.0 as *mut dirent);
return Some(Ok(ent));
Some(Ok(ent))
}
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Entry {
target_os = "macos",
target_os = "solaris")))]
pub fn ino(&self) -> u64 {
self.0.d_fileno as u64
u64::from(self.0.d_fileno)
}

/// Returns the bare file name of this directory entry without any other leading path component.
Expand Down
4 changes: 2 additions & 2 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cfg_if! {
}

/// Sets the platform-specific errno to no-error
unsafe fn clear() -> () {
unsafe fn clear() {
*errno_location() = 0;
}

Expand All @@ -70,7 +70,7 @@ impl Errno {
from_i32(err)
}

pub unsafe fn clear() -> () {
pub unsafe fn clear() {
clear()
}

Expand Down
10 changes: 6 additions & 4 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,17 @@ libc_bitflags!(

pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = path.with_nix_path(|cstr| {
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
let modebits = c_uint::from(mode.bits());
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), modebits) }
})?;

Errno::result(fd)
}

pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = path.with_nix_path(|cstr| {
unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
let modebits = c_uint::from(mode.bits());
unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), modebits) }
})?;
Errno::result(fd)
}
Expand All @@ -187,7 +189,7 @@ fn wrap_readlink_result(v: &mut Vec<u8>, res: ssize_t) -> Result<OsString> {
}
}

pub fn readlink<'a, P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
pub fn readlink<P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
let res = path.with_nix_path(|cstr| {
unsafe { libc::readlink(cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.capacity() as size_t) }
Expand All @@ -197,7 +199,7 @@ pub fn readlink<'a, P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
}


pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
pub fn readlinkat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
let res = path.with_nix_path(|cstr| {
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.capacity() as size_t) }
Expand Down
4 changes: 2 additions & 2 deletions src/ifaddrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl InterfaceAddress {
let mut addr = InterfaceAddress {
interface_name: ifname.to_string_lossy().to_string(),
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
address: address,
netmask: netmask,
address,
netmask,
broadcast: None,
destination: None,
};
Expand Down
36 changes: 33 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ impl Error {
/// let e = Error::from(Errno::EPERM);
/// assert_eq!(Some(Errno::EPERM), e.as_errno());
/// ```
pub fn as_errno(&self) -> Option<Errno> {
if let &Error::Sys(ref e) = self {
Some(*e)
pub fn as_errno(self) -> Option<Errno> {
if let Error::Sys(e) = self {
Some(e)
} else {
None
}
Expand Down Expand Up @@ -177,13 +177,19 @@ impl fmt::Display for Error {
}

pub trait NixPath {
fn is_empty(&self) -> bool;

fn len(&self) -> usize;

fn with_nix_path<T, F>(&self, f: F) -> Result<T>
where F: FnOnce(&CStr) -> T;
}

impl NixPath for str {
fn is_empty(&self) -> bool {
NixPath::is_empty(OsStr::new(self))
}

fn len(&self) -> usize {
NixPath::len(OsStr::new(self))
}
Expand All @@ -195,6 +201,10 @@ impl NixPath for str {
}

impl NixPath for OsStr {
fn is_empty(&self) -> bool {
self.as_bytes().is_empty()
}

fn len(&self) -> usize {
self.as_bytes().len()
}
Expand All @@ -206,6 +216,10 @@ impl NixPath for OsStr {
}

impl NixPath for CStr {
fn is_empty(&self) -> bool {
self.to_bytes().is_empty()
}

fn len(&self) -> usize {
self.to_bytes().len()
}
Expand All @@ -222,6 +236,10 @@ impl NixPath for CStr {
}

impl NixPath for [u8] {
fn is_empty(&self) -> bool {
self.is_empty()
}

fn len(&self) -> usize {
self.len()
}
Expand Down Expand Up @@ -249,6 +267,10 @@ impl NixPath for [u8] {
}

impl NixPath for Path {
fn is_empty(&self) -> bool {
NixPath::is_empty(self.as_os_str())
}

fn len(&self) -> usize {
NixPath::len(self.as_os_str())
}
Expand All @@ -259,6 +281,10 @@ impl NixPath for Path {
}

impl NixPath for PathBuf {
fn is_empty(&self) -> bool {
NixPath::is_empty(self.as_os_str())
}

fn len(&self) -> usize {
NixPath::len(self.as_os_str())
}
Expand All @@ -270,6 +296,10 @@ impl NixPath for PathBuf {

/// Treats `None` as an empty string.
impl<'a, NP: ?Sized + NixPath> NixPath for Option<&'a NP> {
fn is_empty(&self) -> bool {
self.map_or(true, NixPath::is_empty)
}

fn len(&self) -> usize {
self.map_or(0, NixPath::len)
}
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,6 @@ macro_rules! libc_enum {
/// offset of `field` within struct `ty`
macro_rules! offset_of {
($ty:ty, $field:ident) => {
&(*(0 as *const $ty)).$field as *const _ as usize
&(*(ptr::null() as *const $ty)).$field as *const _ as usize
}
}
4 changes: 3 additions & 1 deletion src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl MqAttr {
/// Open a message queue
///
/// See also [`mq_open(2)`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html)
// The mode.bits cast is only lossless on some OSes
#[allow(clippy::cast_lossless)]
pub fn mq_open(name: &CString,
oflag: MQ_OFlag,
mode: Mode,
Expand Down Expand Up @@ -142,7 +144,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
/// Returns the old attributes
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
let oldattr = mq_getattr(mqd)?;
let newattr = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as c_long,
let newattr = MqAttr::new(c_long::from(MQ_OFlag::O_NONBLOCK.bits()),
oldattr.mq_attr.mq_maxmsg,
oldattr.mq_attr.mq_msgsize,
oldattr.mq_attr.mq_curmsgs);
Expand Down
4 changes: 2 additions & 2 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ impl PollFd {
pub fn new(fd: RawFd, events: PollFlags) -> PollFd {
PollFd {
pollfd: libc::pollfd {
fd: fd,
fd,
events: events.bits(),
revents: PollFlags::empty().bits(),
},
}
}

/// Returns the events that occured in the last call to `poll` or `ppoll`.
pub fn revents(&self) -> Option<PollFlags> {
pub fn revents(self) -> Option<PollFlags> {
PollFlags::from_bits(self.pollfd.revents)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ pub fn openpty<'a, 'b, T: Into<Option<&'a Winsize>>, U: Into<Option<&'b Termios>
Errno::result(ret)?;

Ok(OpenptyResult {
master: master,
slave: slave,
master,
slave,
})
}

Expand Down Expand Up @@ -319,8 +319,8 @@ pub fn forkpty<'a, 'b, T: Into<Option<&'a Winsize>>, U: Into<Option<&'b Termios>
})?;

Ok(ForkptyResult {
master: master,
fork_result: fork_result,
master,
fork_result,
})
}

1 change: 0 additions & 1 deletion src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,6 @@ impl<'a> LioCb<'a> {
},
Err(Error::Sys(Errno::EINPROGRESS)) => {
// aiocb is was successfully queued; no need to do anything
()
},
Err(Error::Sys(Errno::EINVAL)) => panic!(
"AioCb was never submitted, or already finalized"),
Expand Down
2 changes: 1 addition & 1 deletion src/sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl KEvent {
pub fn new(ident: uintptr_t, filter: EventFilter, flags: EventFlag,
fflags:FilterFlag, data: intptr_t, udata: intptr_t) -> KEvent {
KEvent { kevent: libc::kevent {
ident: ident,
ident,
filter: filter as type_of_event_filter,
flags: flags.bits(),
fflags: fflags.bits(),
Expand Down
6 changes: 6 additions & 0 deletions src/sys/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ impl FdSet {
}
}

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

/// Monitors file descriptors for readiness
///
/// Returns the total number of ready file descriptors in all sets. The sets are changed so that all
Expand Down
4 changes: 3 additions & 1 deletion src/sys/sendfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ cfg_if! {
///
/// For more information, see
/// [the sendfile(2) man page.](https://www.freebsd.org/cgi/man.cgi?query=sendfile&sektion=2)
#[allow(clippy::too_many_arguments)]
pub fn sendfile(
in_fd: RawFd,
out_sock: RawFd,
Expand All @@ -136,7 +137,8 @@ cfg_if! {
// Readahead goes in upper 16 bits
// Flags goes in lower 16 bits
// see `man 2 sendfile`
let flags: u32 = ((readahead as u32) << 16) | (flags.bits() as u32);
let ra32 = u32::from(readahead);
let flags: u32 = (ra32 << 16) | (flags.bits() as u32);
let mut bytes_sent: off_t = 0;
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);
Expand Down
4 changes: 2 additions & 2 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,14 @@ impl SigSet {
let mut sigset: libc::sigset_t = unsafe { mem::uninitialized() };
let _ = unsafe { libc::sigfillset(&mut sigset as *mut libc::sigset_t) };

SigSet { sigset: sigset }
SigSet { sigset }
}

pub fn empty() -> SigSet {
let mut sigset: libc::sigset_t = unsafe { mem::uninitialized() };
let _ = unsafe { libc::sigemptyset(&mut sigset as *mut libc::sigset_t) };

SigSet { sigset: sigset }
SigSet { sigset }
}

pub fn add(&mut self, signal: Signal) {
Expand Down
Loading