Skip to content

Commit 06edfd0

Browse files
Lint code and add SAFETY comments
1 parent 3681ba5 commit 06edfd0

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

src/imp/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use rand::distr::Alphanumeric;
21
use rand::Rng;
2+
use rand::distr::Alphanumeric;
33
use std::ffi::OsStr;
44
use std::ffi::OsString;
55
use std::fs;

src/imp/unix/generic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::imp::unix::Dir;
2+
use crate::imp::unix::OpenOptions;
13
use crate::imp::unix::copy_file_perms;
24
use crate::imp::unix::create_temporary_file;
35
use crate::imp::unix::remove_temporary_file;
46
use crate::imp::unix::rename_temporary_file;
5-
use crate::imp::unix::Dir;
6-
use crate::imp::unix::OpenOptions;
77
use nix::errno::Errno;
88
use std::ffi::OsString;
99
use std::fs::File;

src/imp/unix/linux.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
use crate::imp::unix::Dir;
2+
use crate::imp::unix::OpenOptions;
3+
use crate::imp::unix::RandomName;
14
use crate::imp::unix::copy_file_perms;
25
use crate::imp::unix::create_temporary_file;
36
use crate::imp::unix::remove_temporary_file;
47
use crate::imp::unix::rename_temporary_file;
5-
use crate::imp::unix::Dir;
6-
use crate::imp::unix::OpenOptions;
7-
use crate::imp::unix::RandomName;
88
use nix::errno::Errno;
9-
use nix::fcntl::openat;
109
use nix::fcntl::AtFlags;
1110
use nix::fcntl::OFlag;
11+
use nix::fcntl::openat;
1212
use nix::libc;
1313
use nix::sys::stat::Mode;
1414
use nix::unistd::fdatasync;
@@ -35,6 +35,7 @@ fn create_unnamed_temporary_file(dir: &Dir, opts: &OpenOptions) -> nix::Result<F
3535

3636
let file_fd = openat(Some(dir.as_raw_fd()), ".", flags, create_mode)?;
3737

38+
// SAFETY: `file_fd` is an exclusively owned file descriptor, and it's open
3839
let file = unsafe { File::from_raw_fd(file_fd) };
3940
Ok(file)
4041
}

src/imp/unix/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use nix::errno::Errno;
2+
use nix::fcntl::AtFlags;
3+
use nix::fcntl::OFlag;
24
use nix::fcntl::open;
35
use nix::fcntl::openat;
46
use nix::fcntl::renameat;
5-
use nix::fcntl::AtFlags;
6-
use nix::fcntl::OFlag;
77
use nix::libc;
8+
use nix::sys::stat::Mode;
89
use nix::sys::stat::fchmod;
910
use nix::sys::stat::fstatat;
1011
use nix::sys::stat::mode_t;
11-
use nix::sys::stat::Mode;
12-
use nix::unistd::fchown;
13-
use nix::unistd::fsync;
14-
use nix::unistd::unlinkat;
1512
use nix::unistd::Gid;
1613
use nix::unistd::Uid;
1714
use nix::unistd::UnlinkatFlags;
18-
use rand::distr::Alphanumeric;
15+
use nix::unistd::fchown;
16+
use nix::unistd::fsync;
17+
use nix::unistd::unlinkat;
1918
use rand::Rng;
19+
use rand::distr::Alphanumeric;
2020
use std::ffi::OsStr;
2121
use std::ffi::OsString;
2222
use std::fs::File;
@@ -99,6 +99,7 @@ impl Dir {
9999
OFlag::O_DIRECTORY | OFlag::O_CLOEXEC,
100100
Mode::empty(),
101101
)?;
102+
// SAFETY: `fd` is an exclusively owned file descriptor, and it's open
102103
Ok(unsafe { Self::from_raw_fd(fd) })
103104
}
104105
}
@@ -120,7 +121,8 @@ impl AsRawFd for Dir {
120121
impl FromRawFd for Dir {
121122
unsafe fn from_raw_fd(fd: RawFd) -> Self {
122123
Self {
123-
fd: OwnedFd::from_raw_fd(fd),
124+
// SAFETY: upheld by the caller
125+
fd: unsafe { OwnedFd::from_raw_fd(fd) },
124126
}
125127
}
126128
}
@@ -190,6 +192,7 @@ fn create_temporary_file(
190192
}
191193
};
192194

195+
// SAFETY: `file_fd` is an exclusively owned file descriptor, and it's open
193196
let file = unsafe { File::from_raw_fd(file_fd) };
194197
let temporary_name = random_name.into_os_string();
195198
Ok((file, temporary_name))

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@
166166
#![warn(clippy::dbg_macro)]
167167
#![warn(clippy::print_stderr)]
168168
#![warn(clippy::print_stdout)]
169+
#![warn(clippy::missing_safety_doc)]
170+
#![warn(clippy::unnecessary_safety_doc)]
171+
#![warn(clippy::unnecessary_safety_comment)]
172+
#![warn(clippy::undocumented_unsafe_blocks)]
169173
#![warn(missing_debug_implementations)]
170174
#![warn(unnameable_types)]
171175
#![warn(unused_macro_rules)]

src/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ fn verify_temporary_file_name<P1: AsRef<Path>, P2: AsRef<Path>>(
6363
) {
6464
let dst_file_name = dst_file_name.as_ref().to_string_lossy().to_string();
6565
let temp_file_name = temp_file_name.as_ref().to_string_lossy().to_string();
66-
let prefix = format!(".{}.", dst_file_name);
66+
let prefix = format!(".{dst_file_name}.");
6767
assert!(
6868
temp_file_name.is_ascii()
6969
&& temp_file_name.starts_with(&prefix)
7070
&& temp_file_name.len() == prefix.len() + 6,
71-
"invalid temporary file name: {:?}",
72-
temp_file_name
71+
"invalid temporary file name: {temp_file_name:?}"
7372
);
7473
}
7574

src/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Unix-specific extensions to [`AtomicWriteFile`] and [`OpenOptions`].
22
3-
use crate::imp::Preserve;
43
use crate::AtomicWriteFile;
54
use crate::OpenOptions;
5+
use crate::imp::Preserve;
66
use nix::sys::stat::mode_t;
77
use std::io::Result;
88
use std::os::unix::fs;

0 commit comments

Comments
 (0)