Skip to content

Commit a4a465d

Browse files
bors[bot]asomers
andauthored
Merge #1107
1107: Clippy cleanup r=asomers a=asomers This is preparation for several changes relating to raising the MSRV, most importantly eliminating `mem::uninitialized`. Co-authored-by: Alan Somers <[email protected]>
2 parents faaacf9 + 1c267ca commit a4a465d

21 files changed

+216
-120
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased] - ReleaseDate
77
### Added
8+
9+
- Implemented `Default` for `FdSet`
10+
([#1107](https://github.com/nix-rust/nix/pull/1107))
11+
12+
- Added `NixPath::is_empty`.
13+
([#1107](https://github.com/nix-rust/nix/pull/1107))
14+
815
### Changed
916
- Changed `readlink` and `readlinkat` to return `OsString`
1017
([#1109](https://github.com/nix-rust/nix/pull/1109))
@@ -25,6 +32,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2532

2633
- Minimum supported Rust version is now 1.36.0.
2734
([#1108](https://github.com/nix-rust/nix/pull/1108))
35+
36+
- `Ipv4Addr::octets`, `Ipv4Addr::to_std`, `Error::as_errno`,
37+
`ForkResult::is_child`, `ForkResult::is_parent`, `Gid::as_raw`,
38+
`Uid::is_root`, `Uid::as_raw`, `Pid::as_raw`, and `PollFd::revents` now take
39+
`self` by value.
40+
([#1107](https://github.com/nix-rust/nix/pull/1107))
41+
2842
### Fixed
2943
### Removed
3044

src/dir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ impl<'d> Iterator for Iter<'d> {
107107
if let Err(e) = Errno::result(readdir_r((self.0).0.as_ptr(), &mut ent.0, &mut result)) {
108108
return Some(Err(e));
109109
}
110-
if result == ptr::null_mut() {
110+
if result.is_null() {
111111
return None;
112112
}
113113
assert_eq!(result, &mut ent.0 as *mut dirent);
114-
return Some(Ok(ent));
114+
Some(Ok(ent))
115115
}
116116
}
117117
}
@@ -165,7 +165,7 @@ impl Entry {
165165
target_os = "macos",
166166
target_os = "solaris")))]
167167
pub fn ino(&self) -> u64 {
168-
self.0.d_fileno as u64
168+
u64::from(self.0.d_fileno)
169169
}
170170

171171
/// Returns the bare file name of this directory entry without any other leading path component.

src/errno.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ cfg_if! {
4646
}
4747

4848
/// Sets the platform-specific errno to no-error
49-
unsafe fn clear() -> () {
49+
unsafe fn clear() {
5050
*errno_location() = 0;
5151
}
5252

@@ -70,7 +70,7 @@ impl Errno {
7070
from_i32(err)
7171
}
7272

73-
pub unsafe fn clear() -> () {
73+
pub unsafe fn clear() {
7474
clear()
7575
}
7676

src/fcntl.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,17 @@ libc_bitflags!(
152152

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

158159
Errno::result(fd)
159160
}
160161

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

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

199201

200-
pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
202+
pub fn readlinkat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
201203
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
202204
let res = path.with_nix_path(|cstr| {
203205
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.capacity() as size_t) }

src/ifaddrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl InterfaceAddress {
5252
let mut addr = InterfaceAddress {
5353
interface_name: ifname.to_string_lossy().to_string(),
5454
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
55-
address: address,
56-
netmask: netmask,
55+
address,
56+
netmask,
5757
broadcast: None,
5858
destination: None,
5959
};

src/lib.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ impl Error {
121121
/// let e = Error::from(Errno::EPERM);
122122
/// assert_eq!(Some(Errno::EPERM), e.as_errno());
123123
/// ```
124-
pub fn as_errno(&self) -> Option<Errno> {
125-
if let &Error::Sys(ref e) = self {
126-
Some(*e)
124+
pub fn as_errno(self) -> Option<Errno> {
125+
if let Error::Sys(e) = self {
126+
Some(e)
127127
} else {
128128
None
129129
}
@@ -177,13 +177,19 @@ impl fmt::Display for Error {
177177
}
178178

179179
pub trait NixPath {
180+
fn is_empty(&self) -> bool;
181+
180182
fn len(&self) -> usize;
181183

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

186188
impl NixPath for str {
189+
fn is_empty(&self) -> bool {
190+
NixPath::is_empty(OsStr::new(self))
191+
}
192+
187193
fn len(&self) -> usize {
188194
NixPath::len(OsStr::new(self))
189195
}
@@ -195,6 +201,10 @@ impl NixPath for str {
195201
}
196202

197203
impl NixPath for OsStr {
204+
fn is_empty(&self) -> bool {
205+
self.as_bytes().is_empty()
206+
}
207+
198208
fn len(&self) -> usize {
199209
self.as_bytes().len()
200210
}
@@ -206,6 +216,10 @@ impl NixPath for OsStr {
206216
}
207217

208218
impl NixPath for CStr {
219+
fn is_empty(&self) -> bool {
220+
self.to_bytes().is_empty()
221+
}
222+
209223
fn len(&self) -> usize {
210224
self.to_bytes().len()
211225
}
@@ -222,6 +236,10 @@ impl NixPath for CStr {
222236
}
223237

224238
impl NixPath for [u8] {
239+
fn is_empty(&self) -> bool {
240+
self.is_empty()
241+
}
242+
225243
fn len(&self) -> usize {
226244
self.len()
227245
}
@@ -249,6 +267,10 @@ impl NixPath for [u8] {
249267
}
250268

251269
impl NixPath for Path {
270+
fn is_empty(&self) -> bool {
271+
NixPath::is_empty(self.as_os_str())
272+
}
273+
252274
fn len(&self) -> usize {
253275
NixPath::len(self.as_os_str())
254276
}
@@ -259,6 +281,10 @@ impl NixPath for Path {
259281
}
260282

261283
impl NixPath for PathBuf {
284+
fn is_empty(&self) -> bool {
285+
NixPath::is_empty(self.as_os_str())
286+
}
287+
262288
fn len(&self) -> usize {
263289
NixPath::len(self.as_os_str())
264290
}
@@ -270,6 +296,10 @@ impl NixPath for PathBuf {
270296

271297
/// Treats `None` as an empty string.
272298
impl<'a, NP: ?Sized + NixPath> NixPath for Option<&'a NP> {
299+
fn is_empty(&self) -> bool {
300+
self.map_or(true, NixPath::is_empty)
301+
}
302+
273303
fn len(&self) -> usize {
274304
self.map_or(0, NixPath::len)
275305
}

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,6 @@ macro_rules! libc_enum {
259259
/// offset of `field` within struct `ty`
260260
macro_rules! offset_of {
261261
($ty:ty, $field:ident) => {
262-
&(*(0 as *const $ty)).$field as *const _ as usize
262+
&(*(ptr::null() as *const $ty)).$field as *const _ as usize
263263
}
264264
}

src/mqueue.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ impl MqAttr {
5757
/// Open a message queue
5858
///
5959
/// See also [`mq_open(2)`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html)
60+
// The mode.bits cast is only lossless on some OSes
61+
#[allow(clippy::cast_lossless)]
6062
pub fn mq_open(name: &CString,
6163
oflag: MQ_OFlag,
6264
mode: Mode,
@@ -142,7 +144,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
142144
/// Returns the old attributes
143145
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
144146
let oldattr = mq_getattr(mqd)?;
145-
let newattr = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as c_long,
147+
let newattr = MqAttr::new(c_long::from(MQ_OFlag::O_NONBLOCK.bits()),
146148
oldattr.mq_attr.mq_maxmsg,
147149
oldattr.mq_attr.mq_msgsize,
148150
oldattr.mq_attr.mq_curmsgs);

src/poll.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ impl PollFd {
2929
pub fn new(fd: RawFd, events: PollFlags) -> PollFd {
3030
PollFd {
3131
pollfd: libc::pollfd {
32-
fd: fd,
32+
fd,
3333
events: events.bits(),
3434
revents: PollFlags::empty().bits(),
3535
},
3636
}
3737
}
3838

3939
/// Returns the events that occured in the last call to `poll` or `ppoll`.
40-
pub fn revents(&self) -> Option<PollFlags> {
40+
pub fn revents(self) -> Option<PollFlags> {
4141
PollFlags::from_bits(self.pollfd.revents)
4242
}
4343
}

src/pty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ pub fn openpty<'a, 'b, T: Into<Option<&'a Winsize>>, U: Into<Option<&'b Termios>
274274
Errno::result(ret)?;
275275

276276
Ok(OpenptyResult {
277-
master: master,
278-
slave: slave,
277+
master,
278+
slave,
279279
})
280280
}
281281

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

321321
Ok(ForkptyResult {
322-
master: master,
323-
fork_result: fork_result,
322+
master,
323+
fork_result,
324324
})
325325
}
326326

src/sys/aio.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,6 @@ impl<'a> LioCb<'a> {
12131213
},
12141214
Err(Error::Sys(Errno::EINPROGRESS)) => {
12151215
// aiocb is was successfully queued; no need to do anything
1216-
()
12171216
},
12181217
Err(Error::Sys(Errno::EINVAL)) => panic!(
12191218
"AioCb was never submitted, or already finalized"),

src/sys/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl KEvent {
234234
pub fn new(ident: uintptr_t, filter: EventFilter, flags: EventFlag,
235235
fflags:FilterFlag, data: intptr_t, udata: intptr_t) -> KEvent {
236236
KEvent { kevent: libc::kevent {
237-
ident: ident,
237+
ident,
238238
filter: filter as type_of_event_filter,
239239
flags: flags.bits(),
240240
fflags: fflags.bits(),

src/sys/select.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ impl FdSet {
6969
}
7070
}
7171

72+
impl Default for FdSet {
73+
fn default() -> Self {
74+
Self::new()
75+
}
76+
}
77+
7278
/// Monitors file descriptors for readiness
7379
///
7480
/// Returns the total number of ready file descriptors in all sets. The sets are changed so that all

src/sys/sendfile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ cfg_if! {
123123
///
124124
/// For more information, see
125125
/// [the sendfile(2) man page.](https://www.freebsd.org/cgi/man.cgi?query=sendfile&sektion=2)
126+
#[allow(clippy::too_many_arguments)]
126127
pub fn sendfile(
127128
in_fd: RawFd,
128129
out_sock: RawFd,
@@ -136,7 +137,8 @@ cfg_if! {
136137
// Readahead goes in upper 16 bits
137138
// Flags goes in lower 16 bits
138139
// see `man 2 sendfile`
139-
let flags: u32 = ((readahead as u32) << 16) | (flags.bits() as u32);
140+
let ra32 = u32::from(readahead);
141+
let flags: u32 = (ra32 << 16) | (flags.bits() as u32);
140142
let mut bytes_sent: off_t = 0;
141143
let hdtr = headers.or(trailers).map(|_| SendfileHeaderTrailer::new(headers, trailers));
142144
let hdtr_ptr = hdtr.as_ref().map_or(ptr::null(), |s| &s.0 as *const libc::sf_hdtr);

src/sys/signal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ impl SigSet {
338338
let mut sigset: libc::sigset_t = unsafe { mem::uninitialized() };
339339
let _ = unsafe { libc::sigfillset(&mut sigset as *mut libc::sigset_t) };
340340

341-
SigSet { sigset: sigset }
341+
SigSet { sigset }
342342
}
343343

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

348-
SigSet { sigset: sigset }
348+
SigSet { sigset }
349349
}
350350

351351
pub fn add(&mut self, signal: Signal) {

0 commit comments

Comments
 (0)