Skip to content

Commit

Permalink
Auto merge of #3456 - samueltardieu:no-uninhabited-refs-deref, r=John…
Browse files Browse the repository at this point in the history
…Titor

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.
  • Loading branch information
bors committed Nov 30, 2023
2 parents 78079f5 + a693257 commit b4686c6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ macro_rules! s_no_extra_traits {
);
}

macro_rules! missing {
($($(#[$attr:meta])* pub enum $i:ident {})*) => ($(
$(#[$attr])* #[allow(missing_copy_implementations)] pub enum $i { }
)*);
}

macro_rules! e {
($($(#[$attr:meta])* pub enum $i:ident { $($field:tt)* })*) => ($(
__item! {
Expand Down
10 changes: 3 additions & 7 deletions src/unix/linux_like/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,9 @@ pub type iconv_t = *mut ::c_void;
pub type sctp_assoc_t = ::__s32;

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

s! {
Expand Down
10 changes: 3 additions & 7 deletions src/unix/linux_like/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ pub type timer_t = *mut ::c_void;
pub type key_t = ::c_int;
pub type id_t = ::c_uint;

#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum timezone {}
impl ::Copy for timezone {}
impl ::Clone for timezone {
fn clone(&self) -> timezone {
*self
}
missing! {
#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum timezone {}
}

s! {
Expand Down
30 changes: 8 additions & 22 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ cfg_if! {
}
}

#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum DIR {}
impl ::Copy for DIR {}
impl ::Clone for DIR {
fn clone(&self) -> DIR {
*self
}
missing! {
#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum DIR {}
}
pub type locale_t = *mut ::c_void;

Expand Down Expand Up @@ -414,21 +410,11 @@ cfg_if! {
}
}

#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum FILE {}
impl ::Copy for FILE {}
impl ::Clone for FILE {
fn clone(&self) -> FILE {
*self
}
}
#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum fpos_t {} // FIXME: fill this out with a struct
impl ::Copy for fpos_t {}
impl ::Clone for fpos_t {
fn clone(&self) -> fpos_t {
*self
}
missing! {
#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum FILE {}
#[cfg_attr(feature = "extra_traits", derive(Debug))]
pub enum fpos_t {} // FIXME: fill this out with a struct
}

extern "C" {
Expand Down

0 comments on commit b4686c6

Please sign in to comment.