From e8deda5d2e45eb634700614009cbcc5b35949e26 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 25 Jun 2022 17:37:29 -0500 Subject: clippo --- azalea-block/block-macros/src/lib.rs | 20 ++++++++------------ azalea-block/block-macros/src/utils.rs | 15 ++++++--------- azalea-block/src/lib.rs | 6 ++++-- 3 files changed, 18 insertions(+), 23 deletions(-) (limited to 'azalea-block') diff --git a/azalea-block/block-macros/src/lib.rs b/azalea-block/block-macros/src/lib.rs index 6206bb65..01ce556d 100644 --- a/azalea-block/block-macros/src/lib.rs +++ b/azalea-block/block-macros/src/lib.rs @@ -36,7 +36,7 @@ struct BlockDefinition { properties_and_defaults: Vec, } impl PropertyAndDefault { - fn into_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault { + fn as_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault { PropertyWithNameAndDefault { name, struct_name: self.struct_name.clone(), @@ -110,11 +110,7 @@ impl Parse for BlockDefinition { let mut properties_and_defaults = Vec::new(); - loop { - let property = match content.parse() { - Ok(property) => property, - Err(_) => break, - }; + while let Ok(property) = content.parse() { content.parse::()?; let property_default = content.parse()?; properties_and_defaults.push(PropertyAndDefault { @@ -248,7 +244,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { for property_name in block_property_names { let property_variants = properties_map .get(property_name) - .expect(format!("Property '{}' not found", property_name).as_str()) + .unwrap_or_else(|| panic!("Property '{}' not found", property_name)) .clone(); block_properties_vec.push(property_variants); } @@ -274,13 +270,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { }; let mut property_name = property_struct_names_to_names .get(&property.struct_name.to_string()) - .expect(format!("Property '{}' is bad", property.struct_name).as_str()) + .unwrap_or_else(|| panic!("Property '{}' is bad", property.struct_name)) .clone(); if let Some(index) = index { property_name.push_str(&format!("_{}", &index.to_string())); } properties_with_name - .push(property.into_property_with_name_and_default(property_name.clone())); + .push(property.as_property_with_name_and_default(property_name.clone())); } // pub face: properties::Face, @@ -297,7 +293,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { { // let property_name_snake = // Ident::new(&property.to_string(), proc_macro2::Span::call_site()); - let name_ident = Ident::new(&name, proc_macro2::Span::call_site()); + let name_ident = Ident::new(name, proc_macro2::Span::call_site()); block_struct_fields.extend(quote! { pub #name_ident: #struct_name, }) @@ -317,7 +313,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let first_state_id = state_id; // if there's no properties, then the block is just a single state - if block_properties_vec.len() == 0 { + if block_properties_vec.is_empty() { block_state_enum_variants.extend(quote! { #block_name_pascal_case, }); @@ -418,7 +414,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let block_behavior = &block.behavior; let block_id = block.name.to_string(); - let from_block_to_state_match = if block.properties_and_defaults.len() > 0 { + let from_block_to_state_match = if !block.properties_and_defaults.is_empty() { quote! { match b { #from_block_to_state_match_inner diff --git a/azalea-block/block-macros/src/utils.rs b/azalea-block/block-macros/src/utils.rs index 019fd60f..82095d86 100644 --- a/azalea-block/block-macros/src/utils.rs +++ b/azalea-block/block-macros/src/utils.rs @@ -1,6 +1,6 @@ pub fn combinations_of(items: &[Vec]) -> Vec> { let mut combinations = Vec::new(); - if items.len() == 0 { + if items.is_empty() { return combinations; }; if items.len() == 1 { @@ -13,8 +13,7 @@ pub fn combinations_of(items: &[Vec]) -> Vec> { for i in 0..items[0].len() { let item = &items[0][i]; for other_combinations in combinations_of(&items[1..]) { - let mut combination = Vec::new(); - combination.push(item.clone()); + let mut combination = vec![item.clone()]; combination.extend(other_combinations); combinations.push(combination); } @@ -29,13 +28,11 @@ pub fn to_pascal_case(s: &str) -> String { for c in s.chars() { if c == '_' { prev_was_underscore = true; + } else if prev_was_underscore { + result.push(c.to_ascii_uppercase()); + prev_was_underscore = false; } else { - if prev_was_underscore { - result.push(c.to_ascii_uppercase()); - prev_was_underscore = false; - } else { - result.push(c); - } + result.push(c); } } result diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index a6de1e92..f07b1bce 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -7,8 +7,10 @@ pub use blocks::*; use std::mem; impl BlockState { - /// Transmutes a u32 to a block state. UB if the value is not a valid block - /// state. + /// Transmutes a u32 to a block state. + /// + /// # Safety + /// The `state_id` should be a valid block state. #[inline] pub unsafe fn from_u32_unsafe(state_id: u32) -> Self { mem::transmute::(state_id) -- cgit v1.2.3