Skip to content

Commit 347b6fc

Browse files
committed
Fix all clippy lints
1 parent 089c2d3 commit 347b6fc

File tree

14 files changed

+130
-88
lines changed

14 files changed

+130
-88
lines changed

libs/context/src/collector.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! The interface to a collector
2+
#![allow(clippy::missing_safety_doc)]
23

34
use core::fmt::{self, Debug, Formatter};
45
use core::ptr::NonNull;
@@ -142,10 +143,7 @@ pub unsafe trait CollectorPtr<C: RawCollectorImpl<Ptr=Self>>: Copy + Eq
142143
fn upgrade_weak_raw(weak: &Self::Weak) -> Option<Self>;
143144
#[inline]
144145
fn upgrade_weak(weak: &Self::Weak) -> Option<CollectorRef<C>> {
145-
match Self::upgrade_weak_raw(weak) {
146-
Some(ptr) => Some(CollectorRef { ptr }),
147-
None => None
148-
}
146+
Self::upgrade_weak_raw(weak).map(|ptr| CollectorRef { ptr })
149147
}
150148
unsafe fn assume_weak_valid(weak: &Self::Weak) -> Self;
151149
unsafe fn create_weak(&self) -> Self::Weak;
@@ -182,14 +180,9 @@ unsafe impl<C: RawCollectorImpl<Ptr=Self>> CollectorPtr<C> for NonNull<C> {
182180

183181
#[inline]
184182
fn upgrade_weak_raw(weak: &Self::Weak) -> Option<Self> {
185-
match weak.upgrade() {
186-
Some(arc) => {
187-
Some(unsafe {
188-
Self::from_raw(Arc::into_raw(arc) as *mut _)
189-
})
190-
},
191-
None => None
192-
}
183+
weak.upgrade().map(|arc| unsafe {
184+
Self::from_raw(Arc::into_raw(arc) as *mut _)
185+
})
193186
}
194187

195188
#[inline]
@@ -496,7 +489,7 @@ impl<C: SyncCollector> CollectorRef<C> {
496489
/// Warning: Only one collector should be created per thread.
497490
/// Doing otherwise can cause deadlocks/panics.
498491
pub fn create_context(&self) -> CollectorContext<C> {
499-
unsafe { CollectorContext::register_root(&self) }
492+
unsafe { CollectorContext::register_root(self) }
500493
}
501494
}
502495
impl<C: RawCollectorImpl> Drop for CollectorRef<C> {

libs/context/src/handle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct GcHandleList<C: RawHandleImpl> {
4040
/// If the list is empty, this is null
4141
last_free_slot: AtomicPtr<HandleSlot<C>>
4242
}
43+
#[allow(clippy::new_without_default)]
4344
impl<C: RawHandleImpl> GcHandleList<C> {
4445
pub fn new() -> Self {
4546
use core::ptr::null_mut;

libs/context/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
untagged_unions, // I want to avoid ManuallyDrop in unions
44
const_fn_trait_bound, // So generics + const fn are unstable, huh?
55
)]
6+
#![allow(
7+
clippy::missing_safety_doc, // Entirely internal code
8+
)]
69
#![cfg_attr(not(feature = "std"), no_std)]
710
//! The implementation of (GcContext)[`::zerogc::GcContext`] that is
811
//! shared among both thread-safe and thread-unsafe code.
@@ -119,14 +122,14 @@ impl<C: RawCollectorImpl> CollectorContext<C> {
119122
pub(crate) unsafe fn register_root(collector: &CollectorRef<C>) -> Self {
120123
CollectorContext {
121124
raw: Box::into_raw(ManuallyDrop::into_inner(
122-
C::RawContext::register_new(&collector)
125+
C::RawContext::register_new(collector)
123126
)),
124127
root: true, // We are responsible for unregistering
125128
}
126129
}
127130
#[inline]
128131
pub fn collector(&self) -> &C {
129-
unsafe { &(*self.raw).collector() }
132+
unsafe { (*self.raw).collector() }
130133
}
131134
#[inline(always)]
132135
unsafe fn with_shadow_stack<R, T: Trace>(

libs/context/src/state/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ pub unsafe trait CollectionManager<C>: self::sealed::Sealed
2121
fn new() -> Self;
2222
fn is_collecting(&self) -> bool;
2323
fn should_trigger_collection(&self) -> bool;
24+
/// Freeze this context
25+
///
26+
/// ## Safety
27+
/// See [GcContext::free_context]
2428
unsafe fn freeze_context(&self, context: &Self::Context);
29+
/// Unfreeze the context
30+
///
31+
/// ## Safety
32+
/// See [GcContext::unfreeze_context]
2533
unsafe fn unfreeze_context(&self, context: &Self::Context);
2634

2735
//
@@ -54,12 +62,19 @@ pub unsafe trait RawContext<C>: Debug + self::sealed::Sealed
5462
/// potentially blocking until completion..
5563
///
5664
/// Undefined behavior if mutated during collection
65+
///
66+
/// ## Safety
67+
/// See [GcContext::unchecked_safepoint]
5768
unsafe fn trigger_safepoint(&self);
5869
/// Borrow a reference to the shadow stack,
5970
/// assuming this context is valid (not active).
6071
///
6172
/// A context is valid if it is either frozen
6273
/// or paused at a safepoint.
74+
///
75+
/// ## Safety
76+
/// The context must be "inactive",
77+
/// either frozen or paused at a safepoint.
6378
#[inline]
6479
unsafe fn assume_valid_shadow_stack(&self) -> &ShadowStack<C> {
6580
match self.state() {
@@ -72,6 +87,10 @@ pub unsafe trait RawContext<C>: Debug + self::sealed::Sealed
7287
fn shadow_stack_ptr(&self) -> *mut ShadowStack<C>;
7388
/// Get a reference to the collector,
7489
/// assuming that it's valid
90+
///
91+
/// ## Safety
92+
/// Assumes that the underlying collector
93+
/// is still valid.
7594
unsafe fn collector(&self) -> &C;
7695
fn state(&self) -> ContextState;
7796
}

libs/context/src/state/nosync.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ unsafe impl<C> super::RawContext<C> for RawContext<C>
207207
pub struct CollectorState {
208208
next_pending_id: u64
209209
}
210+
#[allow(clippy::new_without_default)]
210211
impl CollectorState {
211212
pub fn new() -> Self {
212213
CollectorState {

libs/context/src/state/sync.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ pub struct CollectorState<C: RawCollectorImpl> {
328328
known_contexts: Mutex<HashSet<*mut RawContext<C>>>,
329329
next_pending_id: u64
330330
}
331+
#[allow(clippy::new_without_default)]
331332
impl<C: RawCollectorImpl> CollectorState<C> {
332333
pub fn new() -> Self {
333334
CollectorState {
@@ -494,10 +495,7 @@ pub(crate) trait SyncCollectorImpl: RawCollectorImpl<Manager=CollectionManager<S
494495
pending.valid_contexts.clone()
495496
} else {
496497
// Otherwise we might as well be done with it
497-
mem::replace(
498-
&mut pending.valid_contexts,
499-
Vec::new()
500-
)
498+
mem::take(&mut pending.valid_contexts)
501499
};
502500
perform_collection(&mut *lock, contexts);
503501
drop(lock);

libs/derive/src/lib.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl TypeAttrs {
200200
});
201201
match (iter.next(), iter.next()) {
202202
(Some(_), Some((dup_attr, _))) => {
203-
return Err(Error::new(dup_attr.path.span(), "Duplicate #[zerogc] attribute"))
203+
Err(Error::new(dup_attr.path.span(), "Duplicate #[zerogc] attribute"))
204204
},
205205
(Some((_, parsed)), None) => {
206206
parsed
@@ -495,22 +495,22 @@ pub fn derive_trace(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
495495

496496
fn impl_derive_trace(input: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream, syn::Error> {
497497
let trace_impl = if info.config.nop_trace {
498-
impl_nop_trace(&input, &info)?
498+
impl_nop_trace(input, info)?
499499
} else {
500-
impl_trace(&input, &info)?
500+
impl_trace(input, info)?
501501
};
502502
let rebrand_impl = if info.config.nop_trace {
503-
impl_rebrand_nop(&input, &info)?
503+
impl_rebrand_nop(input, info)?
504504
} else {
505-
impl_rebrand(&input, &info)?
505+
impl_rebrand(input, info)?
506506
};
507507
let erase_impl = if info.config.nop_trace {
508-
impl_erase_nop(&input, &info)?
508+
impl_erase_nop(input, info)?
509509
} else {
510-
impl_erase(&input, &info)?
510+
impl_erase(input, info)?
511511
};
512-
let gc_safe_impl = impl_gc_safe(&input, &info)?;
513-
let extra_impls = impl_extras(&input, &info)?;
512+
let gc_safe_impl = impl_gc_safe(input, info)?;
513+
let extra_impls = impl_extras(input, info)?;
514514
Ok(quote! {
515515
#trace_impl
516516
#rebrand_impl
@@ -755,7 +755,7 @@ fn impl_erase(target: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream, Er
755755
Ok(parse_quote!('new_gc))
756756
},
757757
TypeParamBound::Lifetime(ref lt) => {
758-
return Err(unsupported_lifetime_param(lt))
758+
Err(unsupported_lifetime_param(lt))
759759
}
760760
}
761761
}
@@ -922,7 +922,7 @@ fn impl_rebrand(target: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream,
922922
Ok(parse_quote!('new_gc))
923923
},
924924
TypeParamBound::Lifetime(ref lt) => {
925-
return Err(unsupported_lifetime_param(lt))
925+
Err(unsupported_lifetime_param(lt))
926926
}
927927
}
928928
}
@@ -1126,6 +1126,7 @@ fn impl_gc_safe(target: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream,
11261126
)?;
11271127
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
11281128
let target_fields = collect_fields(target)?;
1129+
#[allow(clippy::if_same_then_else)] // Only necessary because of detailed comments
11291130
let fake_drop_impl = if info.config.is_copy {
11301131
/*
11311132
* If this type can be proven to implement Copy,
@@ -1134,27 +1135,27 @@ fn impl_gc_safe(target: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream,
11341135
* In fact, adding one would prevent it from being Copy
11351136
* in the first place.
11361137
*/
1137-
quote!()
1138+
None
11381139
} else if info.config.unsafe_skip_drop {
1139-
quote!() // Skip generating drop at user's request
1140+
None // Skip generating drop at user's request
11401141
} else if info.config.nop_trace {
11411142
/*
11421143
* A NullTrace type is implicitly drop safe.
11431144
*
11441145
* No tracing needed implies there are no reachable GC references.
11451146
* Therefore there is nothing to fear about finalizers resurrecting things
11461147
*/
1147-
quote!()
1148+
None
11481149
} else {
1149-
quote!(impl #impl_generics Drop for #name #ty_generics #where_clause {
1150+
Some(quote!(impl #impl_generics Drop for #name #ty_generics #where_clause {
11501151
#[inline]
11511152
fn drop(&mut self) {
11521153
/*
11531154
* This is only here to prevent the user
11541155
* from implementing their own Drop functionality.
11551156
*/
11561157
}
1157-
})
1158+
}))
11581159
};
11591160
let verify_gc_safe = if info.config.is_copy {
11601161
quote!(#zerogc_crate::assert_copy::<Self>())
@@ -1215,13 +1216,13 @@ fn impl_nop_trace(target: &DeriveInput, info: &GcTypeInfo) -> Result<TokenStream
12151216
return Ok(quote!());
12161217
}
12171218
let ty_span = t.span();
1218-
Ok(quote_spanned! { ty_span =>
1219+
Ok(quote_spanned! { ty_span => #[allow(clippy::assertions_on_constants)] /* Not actually a constant */ {
12191220
assert!(
12201221
!<#t as #zerogc_crate::Trace>::NEEDS_TRACE,
12211222
"Can't #[derive(NullTrace) with {}",
12221223
stringify!(#t)
12231224
);
1224-
})
1225+
} })
12251226
}).collect::<Result<Vec<_>, Error>>()?;
12261227
let needs_drop = needs_drop(info, &*target_fields);
12271228
Ok(quote! {
@@ -1259,7 +1260,7 @@ fn add_trait_bounds_except(
12591260
) -> Result<Generics, Error> {
12601261
let mut actually_ignored_args = HashSet::<Ident>::new();
12611262
let generics = add_trait_bounds(
1262-
&generics, bound,
1263+
generics, bound,
12631264
&mut |param: &TypeParam| {
12641265
if let Some(ref mut extra) = extra_ignore {
12651266
if extra(&param.ident) {
@@ -1321,9 +1322,9 @@ fn debug_derive(key: &str, target: &dyn ToString, message: &dyn Display, value:
13211322
Ok(ref var) if var == "0" => { return /* disabled */ }
13221323
Ok(var) => {
13231324
let target_parts = std::iter::once(key)
1324-
.chain(target.split(":")).collect::<Vec<_>>();
1325-
for pattern in var.split_terminator(",") {
1326-
let pattern_parts = pattern.split(":").collect::<Vec<_>>();
1325+
.chain(target.split(':')).collect::<Vec<_>>();
1326+
for pattern in var.split_terminator(',') {
1327+
let pattern_parts = pattern.split(':').collect::<Vec<_>>();
13271328
if pattern_parts.len() > target_parts.len() { continue }
13281329
for (&pattern_part, &target_part) in pattern_parts.iter()
13291330
.chain(std::iter::repeat(&"*")).zip(&target_parts) {

0 commit comments

Comments
 (0)