Skip to content

Commit

Permalink
Fix new clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed May 22, 2024
1 parent 5f5b327 commit ae22feb
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 18 deletions.
11 changes: 0 additions & 11 deletions soa-derive-internal/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ impl Input {

pub(crate) trait TokenStreamIterator: Iterator<Item = proc_macro2::TokenStream> {
fn concat_by(self, f: impl Fn(proc_macro2::TokenStream, proc_macro2::TokenStream) -> proc_macro2::TokenStream) -> proc_macro2::TokenStream;
fn concat(self) -> proc_macro2::TokenStream;
}

impl<T: Iterator<Item = proc_macro2::TokenStream>> TokenStreamIterator for T {
Expand All @@ -206,22 +205,12 @@ impl<T: Iterator<Item = proc_macro2::TokenStream>> TokenStreamIterator for T {
None => quote!{},
}
}

fn concat(self) -> proc_macro2::TokenStream {
self.concat_by(|a, b| quote! { #a #b })
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn concat() {
let token_streams = vec![quote!{a}, quote!{b}, quote!{c}];
assert_eq!(token_streams.into_iter().concat().to_string(), "a b c");
}

#[test]
fn concat_by() {
let token_streams = vec![quote!{a}, quote!{b}, quote!{c}];
Expand Down
3 changes: 3 additions & 0 deletions soa-derive-internal/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub fn derive(input: &Input) -> TokenStream {

/// Similar to [`*mut T::write()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.write),
/// with the same safety caveats.
#[allow(clippy::forget_non_drop)]
pub unsafe fn write(self, val: #name) {
unsafe {
#(self.#fields_names.write(::std::ptr::read(&val.#fields_names));)*
Expand All @@ -314,6 +315,7 @@ pub fn derive(input: &Input) -> TokenStream {

/// Similar to [`*mut T::write_volatile()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.write_volatile),
/// with the same safety caveats.
#[allow(clippy::forget_non_drop)]
pub unsafe fn write_volatile(self, val: #name) {
unsafe {
#(self.#fields_names.write_volatile(::std::ptr::read(&val.#fields_names));)*
Expand All @@ -325,6 +327,7 @@ pub fn derive(input: &Input) -> TokenStream {

/// Similar to [`*mut T::write_unaligned()`](https://doc.rust-lang.org/std/primitive.pointer.html#method.write_unaligned),
/// with the same safety caveats.
#[allow(clippy::forget_non_drop)]
pub unsafe fn write_unaligned(self, val: #name) {
unsafe {
#(self.#fields_names.write_unaligned(::std::ptr::read(&val.#fields_names));)*
Expand Down
1 change: 1 addition & 0 deletions soa-derive-internal/src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pub fn derive(input: &Input) -> TokenStream {
}

/// Similar to [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html).
#[allow(clippy::forget_non_drop)]
pub fn replace(&mut self, val: #name) -> #name {
#(
let field = unsafe { ::std::ptr::read(&val.#fields_names) };
Expand Down
3 changes: 3 additions & 0 deletions soa-derive-internal/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub fn derive(input: &Input) -> TokenStream {
/// Similar to [`
#[doc = #vec_name_str]
/// ::push()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push).
#[allow(clippy::forget_non_drop)]
pub fn push(&mut self, value: #name) {
// We need to use ptr read/write instead of moving out of the
// fields in case the value struct implements Drop.
Expand Down Expand Up @@ -193,6 +194,7 @@ pub fn derive(input: &Input) -> TokenStream {
/// Similar to [`
#[doc = #vec_name_str]
/// ::insert()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.insert).
#[allow(clippy::forget_non_drop)]
pub fn insert(&mut self, index: usize, element: #name) {
if index > self.len() {
panic!("index out of bounds: the len is {} but the index is {}", self.len(), index);
Expand All @@ -209,6 +211,7 @@ pub fn derive(input: &Input) -> TokenStream {
}

/// Similar to [`std::mem::replace()`](https://doc.rust-lang.org/std/mem/fn.replace.html).
#[allow(clippy::forget_non_drop)]
pub fn replace(&mut self, index: usize, element: #name) -> #name {
if index > self.len() {
panic!("index out of bounds: the len is {} but the index is {}", self.len(), index);
Expand Down
12 changes: 6 additions & 6 deletions tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fn index_vec_with_usize() {
soa.push(particle.clone());

// SoAIndex
assert_eq!(soa.get(0).unwrap().name, &aos.get(0).unwrap().name);
assert_eq!(soa.get(0).unwrap().mass, &aos.get(0).unwrap().mass);
assert_eq!(soa.get(0).unwrap().name, &aos.first().unwrap().name);
assert_eq!(soa.get(0).unwrap().mass, &aos.first().unwrap().mass);
assert_eq!(aos.get(1), None);
assert_eq!(soa.get(1), None);

Expand Down Expand Up @@ -64,7 +64,7 @@ fn index_vec_with_usize() {

#[test]
fn index_vec_with_ranges() {
let particles = vec![
let particles = [
Particle::new(String::from("Cl"), 1.0),
Particle::new(String::from("Na"), 2.0),
Particle::new(String::from("Br"), 3.0),
Expand Down Expand Up @@ -142,8 +142,8 @@ fn index_slice_with_usize() {
let aos_slice = aos.as_slice();
let soa_slice = soa.as_slice();

assert_eq!(soa_slice.get(0).unwrap().name, &aos_slice.get(0).unwrap().name);
assert_eq!(soa_slice.get(0).unwrap().mass, &aos_slice.get(0).unwrap().mass);
assert_eq!(soa_slice.get(0).unwrap().name, &aos_slice.first().unwrap().name);
assert_eq!(soa_slice.get(0).unwrap().mass, &aos_slice.first().unwrap().mass);
assert_eq!(aos_slice.get(1), None);
assert_eq!(soa_slice.get(1), None);

Expand Down Expand Up @@ -183,7 +183,7 @@ fn index_slice_with_usize() {

#[test]
fn index_slice_with_ranges() {
let particles = vec![
let particles = [
Particle::new(String::from("Cl"), 1.0),
Particle::new(String::from("Na"), 2.0),
Particle::new(String::from("Br"), 3.0),
Expand Down
1 change: 0 additions & 1 deletion tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,5 @@ fn write() {
assert_eq!(vec.data[0], 4);
}

// std::mem::forget(vec);
assert_eq!(DROP_COUNTER.load(Ordering::SeqCst), 1);
}

0 comments on commit ae22feb

Please sign in to comment.