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

Warning fixes and 6.13 backport #25

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0

check-private-items = true
13 changes: 11 additions & 2 deletions .github/workflows/safety.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ jobs:
RUSTFLAGS: "--cfg NO_UI_TESTS --cfg NO_ALLOC_FAIL_TESTS -Z sanitizer=leak"
miri:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
flags: [
"",
"-Zmiri-tree-borrows",
"-Zmiri-strict-provenance",
"-Zmiri-tree-borrows -Zmiri-strict-provenance"
]
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -48,10 +57,10 @@ jobs:
with:
toolchain: ${{ env.NIGHTLY }}
components: miri
- name: cargo miri test
- name: ${{ matrix.flags }} cargo miri test
run: cargo miri test
env:
MIRIFLAGS: ""
MIRIFLAGS: ${{ matrix.flags }}
# loom:
# runs-on: ubuntu-latest
# steps:
Expand Down
22 changes: 22 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ macrotest = "1.0"
prettyplease = { version = "0.2", features = ["verbatim"] }

[lints.rust]
non_ascii_idents = "deny"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(NO_UI_TESTS)', 'cfg(NO_ALLOC_FAIL_TESTS)'] }
unsafe_op_in_unsafe_fn = "deny"
unused_attributes = "deny"
warnings = "deny"

[lints.rustdoc]
missing_crate_level_docs = "deny"
unescaped_backticks = "deny"

[lints.clippy]
# allow this until the modules in examples/ and tests/ are cleaned up
duplicate_mod = "allow"

ignored_unit_patterns = "deny"
mut_mut = "deny"
needless_bitwise_bool = "deny"
needless_continue = "deny"
needless_lifetimes = "deny"
no_mangle_with_rust_abi = "deny"
undocumented_unsafe_blocks = "deny"
unnecessary_safety_comment = "deny"
unnecessary_safety_doc = "deny"
1 change: 1 addition & 0 deletions examples/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ impl From<AllocError> for Error {
}
}

#[allow(dead_code)]
fn main() {}
11 changes: 10 additions & 1 deletion examples/linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]

use core::{
Expand All @@ -9,8 +10,10 @@ use core::{
};

use pinned_init::*;

#[expect(unused_attributes)]
mod error;
pub use error::Error;
use error::Error;

#[pin_data(PinnedDrop)]
#[repr(C)]
Expand Down Expand Up @@ -89,6 +92,12 @@ impl PinnedDrop for ListHead {
struct Link(Cell<NonNull<ListHead>>);

impl Link {
/// # Safety
///
/// The contents of the pointer should form a consistent circular
/// linked list; for example, a "next" link should be pointed back
/// by the target `ListHead`'s "prev" link and a "prev" link should be
/// pointed back by the target `ListHead`'s "next" link.
#[inline]
unsafe fn new_unchecked(ptr: NonNull<ListHead>) -> Self {
Self(Cell::new(ptr))
Expand Down
15 changes: 10 additions & 5 deletions examples/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![allow(clippy::missing_safety_doc)]

use core::{
cell::{Cell, UnsafeCell},
Expand All @@ -14,10 +16,9 @@ use std::{
};

use pinned_init::*;
#[allow(unused_attributes)]
#[expect(unused_attributes)]
#[path = "./linked_list.rs"]
pub mod linked_list;
pub use linked_list::Error;
use linked_list::*;

pub struct SpinLock {
Expand All @@ -40,6 +41,7 @@ impl SpinLock {
}

#[inline]
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
Self {
inner: AtomicBool::new(false),
Expand Down Expand Up @@ -90,6 +92,8 @@ impl<T> CMutex<T> {
park();
sguard = self.spin_lock.acquire();
}
// This does have an effect, as the ListHead inside wait_entry implements Drop!
#[expect(clippy::drop_non_drop)]
drop(wait_entry);
}
self.locked.set(true);
Expand All @@ -101,6 +105,7 @@ impl<T> CMutex<T> {
}
}

#[allow(dead_code)]
pub fn get_data_mut(self: Pin<&mut Self>) -> &mut T {
// SAFETY: we have an exclusive reference and thus nobody has access to data.
unsafe { &mut *self.data.get() }
Expand All @@ -115,7 +120,7 @@ pub struct CMutexGuard<'a, T> {
_pin: PhantomPinned,
}

impl<'a, T> Drop for CMutexGuard<'a, T> {
impl<T> Drop for CMutexGuard<'_, T> {
#[inline]
fn drop(&mut self) {
let sguard = self.mtx.spin_lock.acquire();
Expand All @@ -128,7 +133,7 @@ impl<'a, T> Drop for CMutexGuard<'a, T> {
}
}

impl<'a, T> Deref for CMutexGuard<'a, T> {
impl<T> Deref for CMutexGuard<'_, T> {
type Target = T;

#[inline]
Expand All @@ -137,7 +142,7 @@ impl<'a, T> Deref for CMutexGuard<'a, T> {
}
}

impl<'a, T> DerefMut for CMutexGuard<'a, T> {
impl<T> DerefMut for CMutexGuard<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.mtx.data.get() }
Expand Down
8 changes: 5 additions & 3 deletions examples/pthread_mutex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// inspired by https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs
#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#[cfg(not(windows))]
mod pthread_mtx {
Expand Down Expand Up @@ -37,6 +38,7 @@ mod pthread_mtx {

#[derive(Debug)]
pub enum Error {
#[expect(dead_code)]
IO(std::io::Error),
Alloc,
}
Expand Down Expand Up @@ -108,22 +110,22 @@ mod pthread_mtx {
mtx: &'a PThreadMutex<T>,
}

impl<'a, T> Drop for PThreadMutexGuard<'a, T> {
impl<T> Drop for PThreadMutexGuard<'_, T> {
fn drop(&mut self) {
// SAFETY: raw is always initialized
unsafe { libc::pthread_mutex_unlock(self.mtx.raw.get()) };
}
}

impl<'a, T> Deref for PThreadMutexGuard<'a, T> {
impl<T> Deref for PThreadMutexGuard<'_, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
unsafe { &*self.mtx.data.get() }
}
}

impl<'a, T> DerefMut for PThreadMutexGuard<'a, T> {
impl<T> DerefMut for PThreadMutexGuard<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.mtx.data.get() }
}
Expand Down
2 changes: 2 additions & 0 deletions examples/static_init.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]

use core::{
Expand All @@ -13,6 +14,7 @@ use std::{
thread::{sleep, Builder},
};

#[expect(unused_attributes)]
mod mutex;
use mutex::*;

Expand Down
11 changes: 8 additions & 3 deletions src/__internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ use super::*;
/// [this table]: https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns
pub(crate) type Invariant<T> = PhantomData<fn(*mut T) -> *mut T>;

/// This is the module-internal type implementing `PinInit` and `Init`. It is unsafe to create this
/// type, since the closure needs to fulfill the same safety requirement as the
/// `__pinned_init`/`__init` functions.
/// Module-internal type implementing `PinInit` and `Init`.
///
/// It is unsafe to create this type, since the closure needs to fulfill the same safety
/// requirement as the `__pinned_init`/`__init` functions.
pub(crate) struct InitClosure<F, T: ?Sized, E>(pub(crate) F, pub(crate) Invariant<(E, T)>);

// SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
Expand Down Expand Up @@ -61,6 +62,7 @@ where
pub unsafe trait HasPinData {
type PinData: PinData;

#[expect(clippy::missing_safety_doc)]
unsafe fn __pin_data() -> Self::PinData;
}

Expand Down Expand Up @@ -90,6 +92,7 @@ pub unsafe trait PinData: Copy {
pub unsafe trait HasInitData {
type InitData: InitData;

#[expect(clippy::missing_safety_doc)]
unsafe fn __init_data() -> Self::InitData;
}

Expand Down Expand Up @@ -120,10 +123,12 @@ impl<T: ?Sized> Clone for AllData<T> {

impl<T: ?Sized> Copy for AllData<T> {}

// SAFETY: TODO.
unsafe impl<T: ?Sized> InitData for AllData<T> {
type Datee = T;
}

// SAFETY: TODO.
unsafe impl<T: ?Sized> HasInitData for T {
type InitData = AllData<T>;

Expand Down
Loading
Loading