Skip to content

Commit b4686c6

Browse files
committed
Auto merge of #3456 - samueltardieu:no-uninhabited-refs-deref, r=JohnTitor
Do not dereference uninhabited types refs in Clone implementations A reference to an uninhabited type should never be dereferenced: this is UB. `Copy` should not be implemented on such a type, and an upcoming Clippy lint (`uninhabited_reference`) may flag such dereferences as suspicious. Since those types are not structs, they do not need to get `Copy` and `Clone` implementations. A `missing!` macro limits code duplication.
2 parents 78079f5 + a693257 commit b4686c6

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

src/macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ macro_rules! s_no_extra_traits {
120120
);
121121
}
122122

123+
macro_rules! missing {
124+
($($(#[$attr:meta])* pub enum $i:ident {})*) => ($(
125+
$(#[$attr])* #[allow(missing_copy_implementations)] pub enum $i { }
126+
)*);
127+
}
128+
123129
macro_rules! e {
124130
($($(#[$attr:meta])* pub enum $i:ident { $($field:tt)* })*) => ($(
125131
__item! {

src/unix/linux_like/linux/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,9 @@ pub type iconv_t = *mut ::c_void;
5353
pub type sctp_assoc_t = ::__s32;
5454

5555
pub type eventfd_t = u64;
56-
#[cfg_attr(feature = "extra_traits", derive(Debug))]
57-
pub enum fpos64_t {} // FIXME: fill this out with a struct
58-
impl ::Copy for fpos64_t {}
59-
impl ::Clone for fpos64_t {
60-
fn clone(&self) -> fpos64_t {
61-
*self
62-
}
56+
missing! {
57+
#[cfg_attr(feature = "extra_traits", derive(Debug))]
58+
pub enum fpos64_t {} // FIXME: fill this out with a struct
6359
}
6460

6561
s! {

src/unix/linux_like/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ pub type timer_t = *mut ::c_void;
66
pub type key_t = ::c_int;
77
pub type id_t = ::c_uint;
88

9-
#[cfg_attr(feature = "extra_traits", derive(Debug))]
10-
pub enum timezone {}
11-
impl ::Copy for timezone {}
12-
impl ::Clone for timezone {
13-
fn clone(&self) -> timezone {
14-
*self
15-
}
9+
missing! {
10+
#[cfg_attr(feature = "extra_traits", derive(Debug))]
11+
pub enum timezone {}
1612
}
1713

1814
s! {

src/unix/mod.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ cfg_if! {
4141
}
4242
}
4343

44-
#[cfg_attr(feature = "extra_traits", derive(Debug))]
45-
pub enum DIR {}
46-
impl ::Copy for DIR {}
47-
impl ::Clone for DIR {
48-
fn clone(&self) -> DIR {
49-
*self
50-
}
44+
missing! {
45+
#[cfg_attr(feature = "extra_traits", derive(Debug))]
46+
pub enum DIR {}
5147
}
5248
pub type locale_t = *mut ::c_void;
5349

@@ -414,21 +410,11 @@ cfg_if! {
414410
}
415411
}
416412

417-
#[cfg_attr(feature = "extra_traits", derive(Debug))]
418-
pub enum FILE {}
419-
impl ::Copy for FILE {}
420-
impl ::Clone for FILE {
421-
fn clone(&self) -> FILE {
422-
*self
423-
}
424-
}
425-
#[cfg_attr(feature = "extra_traits", derive(Debug))]
426-
pub enum fpos_t {} // FIXME: fill this out with a struct
427-
impl ::Copy for fpos_t {}
428-
impl ::Clone for fpos_t {
429-
fn clone(&self) -> fpos_t {
430-
*self
431-
}
413+
missing! {
414+
#[cfg_attr(feature = "extra_traits", derive(Debug))]
415+
pub enum FILE {}
416+
#[cfg_attr(feature = "extra_traits", derive(Debug))]
417+
pub enum fpos_t {} // FIXME: fill this out with a struct
432418
}
433419

434420
extern "C" {

0 commit comments

Comments
 (0)