From 6600862c22821266643198eb8d8a801bd8292beb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 25 Nov 2022 09:46:13 +0800 Subject: [PATCH] Satisfy clippy. --- src/api/custom_syntax.rs | 8 ++-- src/api/mod.rs | 5 +-- src/api/register.rs | 3 +- src/config/hashing.rs | 2 +- src/func/native.rs | 2 +- src/lib.rs | 3 ++ src/module/mod.rs | 86 ++++++++++++++++--------------------- src/packages/lang_core.rs | 4 +- src/packages/math_basic.rs | 2 +- src/packages/string_more.rs | 2 +- src/parser.rs | 20 ++++----- src/types/custom_types.rs | 9 +++- src/types/dynamic.rs | 84 ++++++++++++++++-------------------- 13 files changed, 107 insertions(+), 123 deletions(-) diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index 7cc299c1..e378e661 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -12,7 +12,7 @@ use crate::{ }; #[cfg(feature = "no_std")] use std::prelude::v1::*; -use std::{borrow::Borrow, collections::BTreeMap, ops::Deref}; +use std::{borrow::Borrow, ops::Deref}; /// Collection of special markers for custom syntax definition. pub mod markers { @@ -268,7 +268,7 @@ impl Engine { .map_or(false, |m| m.contains_key(s)) { self.custom_keywords - .get_or_insert_with(|| BTreeMap::new().into()) + .get_or_insert_with(Default::default) .insert(s.into(), None); } s.into() @@ -304,7 +304,7 @@ impl Engine { .map_or(false, |m| m.contains_key(s))) { self.custom_keywords - .get_or_insert_with(|| BTreeMap::new().into()) + .get_or_insert_with(Default::default) .insert(s.into(), None); } s.into() @@ -391,7 +391,7 @@ impl Engine { func: impl Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult + SendSync + 'static, ) -> &mut Self { self.custom_syntax - .get_or_insert_with(|| BTreeMap::new().into()) + .get_or_insert_with(Default::default) .insert( key.into(), CustomSyntax { diff --git a/src/api/mod.rs b/src/api/mod.rs index 6b48c744..2080325e 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -35,7 +35,6 @@ pub mod definitions; use crate::{Dynamic, Engine, Identifier}; -use std::collections::BTreeSet; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -109,7 +108,7 @@ impl Engine { #[inline(always)] pub fn disable_symbol(&mut self, symbol: impl Into) -> &mut Self { self.disabled_symbols - .get_or_insert_with(|| BTreeSet::new().into()) + .get_or_insert_with(Default::default) .insert(symbol.into()); self } @@ -199,7 +198,7 @@ impl Engine { // Add to custom keywords self.custom_keywords - .get_or_insert_with(|| std::collections::BTreeMap::new().into()) + .get_or_insert_with(Default::default) .insert(keyword.into(), Some(precedence)); Ok(self) diff --git a/src/api/register.rs b/src/api/register.rs index 518c524b..1591c564 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -720,8 +720,7 @@ impl Engine { } register_static_module_raw( - self.global_sub_modules - .get_or_insert_with(|| BTreeMap::new().into()), + self.global_sub_modules.get_or_insert_with(Default::default), name.as_ref(), module, ); diff --git a/src/config/hashing.rs b/src/config/hashing.rs index b0a91c4b..dee62dfe 100644 --- a/src/config/hashing.rs +++ b/src/config/hashing.rs @@ -121,7 +121,7 @@ impl SusLock { #[must_use] pub fn get(&self) -> Option<&'static T> { if self.initialized.load(Ordering::SeqCst) { - let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) }); + let hokma = hokmalock(self.data.get() as usize); // we forgo the optimistic read, because we don't really care let guard = hokma.write(); let cast: *const T = self.data.get().cast(); diff --git a/src/func/native.rs b/src/func/native.rs index 22a5579b..4114bf1a 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -182,7 +182,7 @@ impl<'a> NativeCallContext<'a> { Self { engine, fn_name: &context.fn_name, - source: context.source.as_ref().map(String::as_str), + source: context.source.as_deref(), global: &context.global, pos: context.pos, } diff --git a/src/lib.rs b/src/lib.rs index c2c070e8..fa95cb95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,9 @@ #![allow(clippy::module_name_repetitions)] #![allow(clippy::negative_feature_names)] #![allow(clippy::module_inception)] +#![allow(clippy::box_collection)] +#![allow(clippy::too_many_arguments)] +#![allow(clippy::upper_case_acronyms)] #[cfg(feature = "no_std")] extern crate alloc; diff --git a/src/module/mod.rs b/src/module/mod.rs index 157bdfe6..5a639d45 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -462,7 +462,7 @@ impl Module { #[inline(always)] pub fn set_custom_type(&mut self, name: &str) -> &mut Self { self.custom_types - .get_or_insert_with(|| CustomTypesCollection::new().into()) + .get_or_insert_with(Default::default) .add_type::(name); self } @@ -488,7 +488,7 @@ impl Module { name: impl Into, ) -> &mut Self { self.custom_types - .get_or_insert_with(|| CustomTypesCollection::new().into()) + .get_or_insert_with(Default::default) .add(type_path, name); self } @@ -656,11 +656,11 @@ impl Module { if self.is_indexed() { let hash_var = crate::calc_var_hash(Some(""), &ident); self.all_variables - .get_or_insert_with(|| Default::default()) + .get_or_insert_with(Default::default) .insert(hash_var, value.clone()); } self.variables - .get_or_insert_with(|| Default::default()) + .get_or_insert_with(Default::default) .insert(ident, value); self } @@ -687,25 +687,23 @@ impl Module { let hash_script = crate::calc_fn_hash(None, &fn_def.name, num_params); #[cfg(feature = "metadata")] let params_info = fn_def.params.iter().map(Into::into).collect(); - self.functions - .get_or_insert_with(|| StraightHashMap::default().into()) - .insert( - hash_script, - FuncInfo { - name: fn_def.name.as_str().into(), - namespace: FnNamespace::Internal, - access: fn_def.access, - num_params, - param_types: StaticVec::new_const(), - #[cfg(feature = "metadata")] - params_info, - #[cfg(feature = "metadata")] - return_type: "".into(), - #[cfg(feature = "metadata")] - comments: Box::default(), - func: fn_def.into(), - }, - ); + self.functions.get_or_insert_with(Default::default).insert( + hash_script, + FuncInfo { + name: fn_def.name.as_str().into(), + namespace: FnNamespace::Internal, + access: fn_def.access, + num_params, + param_types: StaticVec::new_const(), + #[cfg(feature = "metadata")] + params_info, + #[cfg(feature = "metadata")] + return_type: "".into(), + #[cfg(feature = "metadata")] + comments: Box::default(), + func: fn_def.into(), + }, + ); self.flags &= !ModuleFlags::INDEXED & !ModuleFlags::INDEXED_GLOBAL_FUNCTIONS; hash_script } @@ -747,7 +745,7 @@ impl Module { self.all_type_iterators = None; self.flags &= !ModuleFlags::INDEXED & !ModuleFlags::INDEXED_GLOBAL_FUNCTIONS; - self.modules.get_or_insert_with(|| Default::default()) + self.modules.get_or_insert_with(Default::default) } /// Does a sub-module exist in the [`Module`]? @@ -809,7 +807,7 @@ impl Module { sub_module: impl Into, ) -> &mut Self { self.modules - .get_or_insert_with(|| Default::default()) + .get_or_insert_with(Default::default) .insert(name.into(), sub_module.into()); self.flags &= !ModuleFlags::INDEXED & !ModuleFlags::INDEXED_GLOBAL_FUNCTIONS; self @@ -1605,12 +1603,10 @@ impl Module { #[cfg(feature = "metadata")] if !other.doc.as_ref().map_or(true, |s| s.is_empty()) { if !self.doc.as_ref().map_or(true, |s| s.is_empty()) { - self.doc - .get_or_insert_with(|| SmartString::new_const()) - .push('\n'); + self.doc.get_or_insert_with(Default::default).push('\n'); } self.doc - .get_or_insert_with(|| SmartString::new_const()) + .get_or_insert_with(Default::default) .push_str(other.doc.as_ref().unwrap()); } @@ -1657,12 +1653,10 @@ impl Module { #[cfg(feature = "metadata")] if !other.doc.as_ref().map_or(true, |s| s.is_empty()) { if !self.doc.as_ref().map_or(true, |s| s.is_empty()) { - self.doc - .get_or_insert_with(|| SmartString::new_const()) - .push('\n'); + self.doc.get_or_insert_with(Default::default).push('\n'); } self.doc - .get_or_insert_with(|| SmartString::new_const()) + .get_or_insert_with(Default::default) .push_str(other.doc.as_ref().unwrap()); } @@ -1674,7 +1668,7 @@ impl Module { #[inline] pub fn fill_with(&mut self, other: &Self) -> &mut Self { if let Some(ref modules) = other.modules { - let m = self.modules.get_or_insert_with(|| Default::default()); + let m = self.modules.get_or_insert_with(Default::default); for (k, v) in modules.iter() { if !m.contains_key(k) { @@ -1684,7 +1678,7 @@ impl Module { } if let Some(ref variables) = other.variables { for (k, v) in variables.iter() { - let map = self.variables.get_or_insert_with(|| Default::default()); + let map = self.variables.get_or_insert_with(Default::default); if !map.contains_key(k) { map.insert(k.clone(), v.clone()); @@ -1693,7 +1687,7 @@ impl Module { } if let Some(ref functions) = other.functions { for (k, f) in functions.iter() { - let map = self.functions.get_or_insert_with(|| Default::default()); + let map = self.functions.get_or_insert_with(Default::default); if !map.contains_key(k) { map.insert(*k, f.clone()); @@ -1702,9 +1696,7 @@ impl Module { } self.dynamic_functions_filter += &other.dynamic_functions_filter; if let Some(ref type_iterators) = other.type_iterators { - let t = self - .type_iterators - .get_or_insert_with(|| Default::default()); + let t = self.type_iterators.get_or_insert_with(Default::default); for (&k, v) in type_iterators.iter() { t.entry(k).or_insert_with(|| v.clone()); @@ -1718,12 +1710,10 @@ impl Module { #[cfg(feature = "metadata")] if !other.doc.as_ref().map_or(true, |s| s.is_empty()) { if !self.doc.as_ref().map_or(true, |s| s.is_empty()) { - self.doc - .get_or_insert_with(|| SmartString::new_const()) - .push('\n'); + self.doc.get_or_insert_with(Default::default).push('\n'); } self.doc - .get_or_insert_with(|| SmartString::new_const()) + .get_or_insert_with(Default::default) .push_str(other.doc.as_ref().unwrap()); } @@ -1797,12 +1787,10 @@ impl Module { #[cfg(feature = "metadata")] if !other.doc.as_ref().map_or(true, |s| s.is_empty()) { if !self.doc.as_ref().map_or(true, |s| s.is_empty()) { - self.doc - .get_or_insert_with(|| SmartString::new_const()) - .push('\n'); + self.doc.get_or_insert_with(Default::default).push('\n'); } self.doc - .get_or_insert_with(|| SmartString::new_const()) + .get_or_insert_with(Default::default) .push_str(other.doc.as_ref().unwrap()); } @@ -2285,11 +2273,11 @@ impl Module { let func = Shared::new(func); if self.is_indexed() { self.all_type_iterators - .get_or_insert_with(|| Default::default()) + .get_or_insert_with(Default::default) .insert(type_id, func.clone()); } self.type_iterators - .get_or_insert_with(|| Default::default()) + .get_or_insert_with(Default::default) .insert(type_id, func); self } diff --git a/src/packages/lang_core.rs b/src/packages/lang_core.rs index a4396514..717691d9 100644 --- a/src/packages/lang_core.rs +++ b/src/packages/lang_core.rs @@ -304,12 +304,12 @@ fn collect_fn_metadata( let mut ns = SmartString::new_const(); write!(&mut ns, "{namespace}{}{name}", DoubleColon.literal_syntax()).unwrap(); - scan_module(engine, list, &ns, &**m, filter); + scan_module(engine, list, &ns, m, filter); } } for (ns, m) in ctx.iter_imports_raw() { - scan_module(engine, &mut list, ns, &**m, filter); + scan_module(engine, &mut list, ns, m, filter); } } diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index f5434279..4c54f570 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -574,7 +574,7 @@ mod decimal_functions { .into(), ) }, - |n| Ok(n), + Ok, ) } /// Return the integral part of the decimal number. diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 400ea159..70e95363 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -541,7 +541,7 @@ mod string_functions { /// ``` #[rhai_fn(name = "contains")] pub fn contains_char(string: &str, character: char) -> bool { - string.contains(character).into() + string.contains(character) } /// Return `true` if the string starts with a specified string. diff --git a/src/parser.rs b/src/parser.rs index d299e121..247db9e9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -201,7 +201,7 @@ impl<'e, 's> ParseState<'e, 's> { .any(|v| v.as_str() == name) { self.external_vars - .get_or_insert_with(|| FnArgsVec::new().into()) + .get_or_insert_with(Default::default) .push(Ident { name: name.into(), pos: _pos, @@ -1659,7 +1659,7 @@ impl Engine { } // Access to `this` as a variable is OK within a function scope #[cfg(not(feature = "no_function"))] - _ if &*s == KEYWORD_THIS && settings.has_flag(ParseSettingFlags::FN_SCOPE) => { + _ if *s == KEYWORD_THIS && settings.has_flag(ParseSettingFlags::FN_SCOPE) => { Expr::Variable( (None, ns, 0, state.get_interned_string(*s)).into(), None, @@ -1667,7 +1667,7 @@ impl Engine { ) } // Cannot access to `this` as a variable not in a function scope - _ if &*s == KEYWORD_THIS => { + _ if *s == KEYWORD_THIS => { let msg = format!("'{s}' can only be used in functions"); return Err( LexError::ImproperSymbol(s.to_string(), msg).into_err(settings.pos) @@ -2055,7 +2055,7 @@ impl Engine { } // var (indexed) = rhs Expr::Variable(ref x, i, var_pos) => { - let stack = state.stack.get_or_insert_with(|| Scope::new().into()); + let stack = state.stack.get_or_insert_with(Default::default); let (index, .., name) = &**x; let index = i.map_or_else( || index.expect("either long or short index is `None`").get(), @@ -2482,7 +2482,7 @@ impl Engine { let marker = state.get_interned_string(SCOPE_SEARCH_BARRIER_MARKER); state .stack - .get_or_insert_with(|| Scope::new().into()) + .get_or_insert_with(Default::default) .push(marker, ()); } @@ -2846,7 +2846,7 @@ impl Engine { }; let prev_stack_len = { - let stack = state.stack.get_or_insert_with(|| Scope::new().into()); + let stack = state.stack.get_or_insert_with(Default::default); let prev_stack_len = stack.len(); @@ -2887,7 +2887,7 @@ impl Engine { let (name, pos) = parse_var_name(input)?; { - let stack = state.stack.get_or_insert_with(|| Scope::new().into()); + let stack = state.stack.get_or_insert_with(Default::default); if !self.allow_shadowing() && stack.iter().any(|(v, ..)| v == name) { return Err(PERR::VariableExists(name.into()).into_err(pos)); @@ -3486,7 +3486,7 @@ impl Engine { let name = state.get_interned_string(name); state .stack - .get_or_insert_with(|| Scope::new().into()) + .get_or_insert_with(Default::default) .push(name.clone(), ()); Ident { name, pos } } else { @@ -3568,7 +3568,7 @@ impl Engine { let s = state.get_interned_string(*s); state .stack - .get_or_insert_with(|| Scope::new().into()) + .get_or_insert_with(Default::default) .push(s.clone(), ()); params.push((s, pos)); } @@ -3713,7 +3713,7 @@ impl Engine { let s = state.get_interned_string(*s); state .stack - .get_or_insert_with(|| Scope::new().into()) + .get_or_insert_with(Default::default) .push(s.clone(), ()); params_list.push(s); } diff --git a/src/types/custom_types.rs b/src/types/custom_types.rs index ca43e1e8..8135a506 100644 --- a/src/types/custom_types.rs +++ b/src/types/custom_types.rs @@ -13,7 +13,7 @@ pub struct CustomTypeInfo { /// _(internals)_ A collection of custom types. /// Exported under the `internals` feature only. -#[derive(Clone, Hash, Default)] +#[derive(Clone, Hash)] pub struct CustomTypesCollection(BTreeMap); impl fmt::Debug for CustomTypesCollection { @@ -25,6 +25,13 @@ impl fmt::Debug for CustomTypesCollection { } } +impl Default for CustomTypesCollection { + #[inline(always)] + fn default() -> Self { + Self::new() + } +} + impl CustomTypesCollection { /// Create a new [`CustomTypesCollection`]. #[inline(always)] diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index a92bc51d..47fa5c22 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1711,10 +1711,9 @@ impl Dynamic { match self.0 { Union::Unit(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Unit(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Unit(..)) + } _ => false, } } @@ -1725,10 +1724,9 @@ impl Dynamic { match self.0 { Union::Int(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Int(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Int(..)) + } _ => false, } } @@ -1742,10 +1740,9 @@ impl Dynamic { match self.0 { Union::Float(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Float(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Float(..)) + } _ => false, } } @@ -1759,10 +1756,9 @@ impl Dynamic { match self.0 { Union::Decimal(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Decimal(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Decimal(..)) + } _ => false, } } @@ -1773,10 +1769,9 @@ impl Dynamic { match self.0 { Union::Bool(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Bool(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Bool(..)) + } _ => false, } } @@ -1787,10 +1782,9 @@ impl Dynamic { match self.0 { Union::Char(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Char(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Char(..)) + } _ => false, } } @@ -1801,10 +1795,9 @@ impl Dynamic { match self.0 { Union::Str(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Str(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Str(..)) + } _ => false, } } @@ -1818,10 +1811,9 @@ impl Dynamic { match self.0 { Union::Array(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Array(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Array(..)) + } _ => false, } } @@ -1835,10 +1827,9 @@ impl Dynamic { match self.0 { Union::Blob(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Blob(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Blob(..)) + } _ => false, } } @@ -1852,10 +1843,9 @@ impl Dynamic { match self.0 { Union::Map(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::Map(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::Map(..)) + } _ => false, } } @@ -1866,10 +1856,9 @@ impl Dynamic { match self.0 { Union::FnPtr(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::FnPtr(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::FnPtr(..)) + } _ => false, } } @@ -1883,10 +1872,9 @@ impl Dynamic { match self.0 { Union::TimeStamp(..) => true, #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, ..) => match crate::func::locked_read(cell).0 { - Union::TimeStamp(..) => true, - _ => false, - }, + Union::Shared(ref cell, ..) => { + matches!(crate::func::locked_read(cell).0, Union::TimeStamp(..)) + } _ => false, } }