From 75718a5a8b3eeadd42297d2b7cf3caa2c8fc5160 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 11 Feb 2023 00:17:26 +0800 Subject: [PATCH] Satisfy clippy. --- src/ast/stmt.rs | 4 ++-- src/bin/rhai-dbg.rs | 9 +++------ src/eval/debugger.rs | 4 ++-- src/lib.rs | 9 ++++++--- src/module/mod.rs | 4 ++-- src/packages/blob_basic.rs | 4 +++- src/packages/lang_core.rs | 18 +++++++++--------- src/parser.rs | 8 ++++---- src/tokenizer.rs | 8 ++++---- 9 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/ast/stmt.rs b/src/ast/stmt.rs index af4ff064..00ed80e2 100644 --- a/src/ast/stmt.rs +++ b/src/ast/stmt.rs @@ -93,7 +93,7 @@ impl OpAssignment { #[inline(always)] pub fn new_op_assignment_from_base(name: &str, pos: Position) -> Self { let op = Token::lookup_symbol_from_syntax(name).expect("operator"); - Self::new_op_assignment_from_base_token(op, pos) + Self::new_op_assignment_from_base_token(&op, pos) } /// Convert a [`Token`] into a new [`OpAssignment`]. /// @@ -102,7 +102,7 @@ impl OpAssignment { /// Panics if the token is cannot be converted into an op-assignment operator. #[inline(always)] #[must_use] - pub fn new_op_assignment_from_base_token(op: Token, pos: Position) -> Self { + pub fn new_op_assignment_from_base_token(op: &Token, pos: Position) -> Self { Self::new_op_assignment_from_token(op.convert_to_op_assignment().expect("operator"), pos) } } diff --git a/src/bin/rhai-dbg.rs b/src/bin/rhai-dbg.rs index db929a4b..6fedabb5 100644 --- a/src/bin/rhai-dbg.rs +++ b/src/bin/rhai-dbg.rs @@ -166,10 +166,7 @@ fn load_script(engine: &Engine) -> (rhai::AST, String) { let filename = match Path::new(&filename).canonicalize() { Err(err) => { - eprintln!( - "\x1b[31mError script file path: {}\n{}\x1b[39m", - filename, err - ); + eprintln!("\x1b[31mError script file path: {filename}\n{err}\x1b[39m"); exit(1); } Ok(f) => { @@ -315,7 +312,7 @@ fn debug_callback( } ["node"] => { if pos.is_none() { - println!("{:?}", node); + println!("{node:?}"); } else { match source { Some(source) => println!("{node:?} {source} @ {pos:?}"), @@ -400,7 +397,7 @@ fn debug_callback( #[cfg(not(feature = "no_position"))] rhai::debugger::BreakPoint::AtPosition { pos, .. } => { let line_num = format!("[{}] line ", i + 1); - print!("{}", line_num); + print!("{line_num}"); print_source(lines, *pos, line_num.len(), (0, 0)); } _ => println!("[{}] {bp}", i + 1), diff --git a/src/eval/debugger.rs b/src/eval/debugger.rs index 652134a3..0302bdc2 100644 --- a/src/eval/debugger.rs +++ b/src/eval/debugger.rs @@ -495,13 +495,13 @@ impl Engine { /// /// It is up to the [`Engine`] to reactivate the debugger. #[inline] - pub(crate) fn run_debugger_raw<'a>( + pub(crate) fn run_debugger_raw( &self, global: &mut GlobalRuntimeState, caches: &mut Caches, scope: &mut Scope, this_ptr: Option<&mut Dynamic>, - node: ASTNode<'a>, + node: ASTNode, event: DebuggerEvent, ) -> Result, Box> { if let Some(ref x) = self.debugger_interface { diff --git a/src/lib.rs b/src/lib.rs index a6852433..d27a2a4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,11 +80,14 @@ #![allow(clippy::upper_case_acronyms)] #![allow(clippy::match_same_arms)] // The lints below can be turned off to reduce signal/noise ratio -// #![allow(clippy::too_many_lines)] -// #![allow(clippy::let_underscore_drop)] +#![allow(clippy::too_many_lines)] +#![allow(clippy::let_underscore_drop)] #![allow(clippy::absurd_extreme_comparisons)] #![allow(clippy::unnecessary_cast)] -// #![allow(clippy::wildcard_imports)] +#![allow(clippy::wildcard_imports)] +#![allow(clippy::no_effect_underscore_binding)] +#![allow(clippy::semicolon_if_nothing_returned)] +#![allow(clippy::type_complexity)] #[cfg(feature = "no_std")] extern crate alloc; diff --git a/src/module/mod.rs b/src/module/mod.rs index e299b2a5..24cbc06e 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -233,7 +233,7 @@ impl fmt::Debug for Module { .modules .as_deref() .into_iter() - .flat_map(|m| m.keys()) + .flat_map(BTreeMap::keys) .map(SmartString::as_str) .collect::>(), ) @@ -2202,7 +2202,7 @@ impl Module { environ: ref mut e, .. } = f.func { - *e = Some(environ.clone()) + *e = Some(environ.clone()); } }); diff --git a/src/packages/blob_basic.rs b/src/packages/blob_basic.rs index 430b89c0..80449e92 100644 --- a/src/packages/blob_basic.rs +++ b/src/packages/blob_basic.rs @@ -513,11 +513,13 @@ pub mod blob_functions { /// /// print(b); // prints "[030405]" /// ``` - #[allow(clippy::cast_sign_loss, clippy::needless_pass_by_value)] + #[allow(clippy::cast_sign_loss, clippy::needless_pass_by_value, clippy::cast_possible_truncation)] pub fn chop(blob: &mut Blob, len: INT) { if !blob.is_empty() { if len <= 0 { blob.clear(); + } else if len > MAX_USIZE_INT { + // len > BLOB length } else if (len as usize) < blob.len() { blob.drain(0..blob.len() - len as usize); } diff --git a/src/packages/lang_core.rs b/src/packages/lang_core.rs index 6b55f2e1..ce75f7ef 100644 --- a/src/packages/lang_core.rs +++ b/src/packages/lang_core.rs @@ -109,10 +109,10 @@ mod core_functions { /// ``` #[cfg(not(feature = "no_std"))] pub fn sleep(seconds: INT) { - if seconds <= 0 { - return; + if seconds > 0 { + #[allow(clippy::cast_sign_loss)] + std::thread::sleep(std::time::Duration::from_secs(seconds as u64)); } - std::thread::sleep(std::time::Duration::from_secs(seconds as u64)); } /// Parse a JSON string into a value. @@ -142,23 +142,23 @@ mod reflection_functions { /// Return an array of object maps containing metadata of all script-defined functions. pub fn get_fn_metadata_list(ctx: NativeCallContext) -> Array { - collect_fn_metadata(ctx, |_, _, _, _, _| true) + collect_fn_metadata(&ctx, |_, _, _, _, _| true) } /// Return an array of object maps containing metadata of all script-defined functions /// matching the specified name. #[rhai_fn(name = "get_fn_metadata_list")] pub fn get_fn_metadata(ctx: NativeCallContext, name: &str) -> Array { - collect_fn_metadata(ctx, |_, _, n, _, _| n == name) + collect_fn_metadata(&ctx, |_, _, n, _, _| n == name) } /// Return an array of object maps containing metadata of all script-defined functions /// matching the specified name and arity (number of parameters). #[rhai_fn(name = "get_fn_metadata_list")] #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] pub fn get_fn_metadata2(ctx: NativeCallContext, name: &str, params: INT) -> Array { - if !(0..=crate::MAX_USIZE_INT).contains(¶ms) { - Array::new() + if (0..=crate::MAX_USIZE_INT).contains(¶ms) { + collect_fn_metadata(&ctx, |_, _, n, p, _| p == (params as usize) && n == name) } else { - collect_fn_metadata(ctx, |_, _, n, p, _| p == (params as usize) && n == name) + Array::new() } } } @@ -167,7 +167,7 @@ mod reflection_functions { #[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_object"))] fn collect_fn_metadata( - ctx: NativeCallContext, + ctx: &NativeCallContext, filter: impl Fn(FnNamespace, FnAccess, &str, usize, &crate::Shared) -> bool + Copy, ) -> crate::Array { diff --git a/src/parser.rs b/src/parser.rs index 9051b65d..465166ed 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3560,7 +3560,9 @@ impl Engine { // try { try_block } catch ( var ) { catch_block } let branch = self.parse_block(input, state, lib, settings)?.into(); - let expr = if !catch_var.is_empty() { + let expr = if catch_var.is_empty() { + Expr::Unit(catch_var.pos) + } else { // Remove the error variable from the stack state.stack.as_deref_mut().unwrap().pop(); @@ -3569,12 +3571,10 @@ impl Engine { None, catch_var.pos, ) - } else { - Expr::Unit(catch_var.pos) }; Ok(Stmt::TryCatch( - FlowControl { body, expr, branch }.into(), + FlowControl { expr, body, branch }.into(), settings.pos, )) } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 5102ed9f..717b7282 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -1830,10 +1830,10 @@ fn get_next_token_inner( return Some((Token::NotIn, start_pos)); } } - } else { - stream.unget('i'); - return Some((Token::Bang, start_pos)); } + + stream.unget('i'); + return Some((Token::Bang, start_pos)); } ('!', '=') => { eat_next_and_advance(stream, pos); @@ -2137,7 +2137,7 @@ impl InputStream for MultiInputsStream<'_> { } fn peek_next(&mut self) -> Option { if let ch @ Some(..) = self.buf.last() { - return ch.cloned(); + return ch.copied(); } loop {