From 3c2e0318836fe44b9faeb1a386bb6a2794dcc39d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 27 Oct 2022 13:38:21 +0800 Subject: [PATCH] Use variable interpolation for println!. --- examples/arrays_and_structs.rs | 6 +-- examples/callback.rs | 2 +- examples/custom_types.rs | 4 +- examples/custom_types_and_methods.rs | 4 +- examples/event_handler_js/main.rs | 8 ++-- examples/event_handler_main/main.rs | 6 +-- examples/event_handler_map/main.rs | 8 ++-- examples/hello.rs | 2 +- examples/reuse_scope.rs | 2 +- examples/serde.rs | 8 ++-- examples/simple_fn.rs | 2 +- examples/strings.rs | 4 +- examples/threading.rs | 2 +- src/ast/expr.rs | 16 +++---- src/ast/namespace.rs | 2 +- src/ast/stmt.rs | 4 +- src/bin/rhai-dbg.rs | 65 ++++++++++++++-------------- src/bin/rhai-repl.rs | 41 +++++++++--------- src/bin/rhai-run.rs | 11 +++-- src/engine.rs | 20 +++------ src/eval/debugger.rs | 33 +++++--------- src/func/callable_function.rs | 16 +++---- src/types/dynamic.rs | 4 +- src/types/parse_error.rs | 12 ++--- 24 files changed, 132 insertions(+), 150 deletions(-) diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index fc937bcd..b5a7889a 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -38,7 +38,7 @@ fn main() -> Result<(), Box> { engine .gen_fn_signatures(false) .into_iter() - .for_each(|func| println!("{}", func)); + .for_each(|func| println!("{func}")); println!(); } @@ -51,7 +51,7 @@ fn main() -> Result<(), Box> { ", )?; - println!("{:?}", result); + println!("{result:?}"); let result = engine.eval::( " @@ -61,7 +61,7 @@ fn main() -> Result<(), Box> { ", )?; - println!("{:?}", result); + println!("{result:?}"); Ok(()) } diff --git a/examples/callback.rs b/examples/callback.rs index ce27f77c..99984691 100644 --- a/examples/callback.rs +++ b/examples/callback.rs @@ -36,7 +36,7 @@ fn main() -> Result<(), Box> { let r2 = func(1, 2); let r3 = func(1, 2); - println!("The Answers: {}, {}, {}", r1, r2, r3); // prints 40, 42, 44 + println!("The Answers: {r1}, {r2}, {r3}"); // prints 40, 42, 44 Ok(()) } diff --git a/examples/custom_types.rs b/examples/custom_types.rs index ba57de58..c4d7804f 100644 --- a/examples/custom_types.rs +++ b/examples/custom_types.rs @@ -67,7 +67,7 @@ fn main() -> Result<(), Box> { engine .gen_fn_signatures(false) .into_iter() - .for_each(|func| println!("{}", func)); + .for_each(|func| println!("{func}")); println!(); } @@ -89,7 +89,7 @@ fn main() -> Result<(), Box> { ", )?; - println!("result: {}", result); // prints 1085764 + println!("result: {result}"); // prints 1085764 Ok(()) } diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index a70265ff..fecbccfe 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -48,7 +48,7 @@ fn main() -> Result<(), Box> { engine .gen_fn_signatures(false) .into_iter() - .for_each(|func| println!("{}", func)); + .for_each(|func| println!("{func}")); println!(); } @@ -62,7 +62,7 @@ fn main() -> Result<(), Box> { ", )?; - println!("result: {}", result); // prints 1085764 + println!("result: {result}"); // prints 1085764 Ok(()) } diff --git a/examples/event_handler_js/main.rs b/examples/event_handler_js/main.rs index db5740f3..22c128c3 100644 --- a/examples/event_handler_js/main.rs +++ b/examples/event_handler_js/main.rs @@ -78,12 +78,12 @@ pub fn main() { scope.push_constant("MY_CONSTANT", 42_i64); // Compile the handler script. - println!("> Loading script file: {}", path); + println!("> Loading script file: {path}"); let ast = match engine.compile_file_with_scope(&mut scope, path.into()) { Ok(ast) => ast, Err(err) => { - eprintln!("! Error: {}", err); + eprintln!("! Error: {err}"); println!("Cannot continue. Bye!"); return; } @@ -101,7 +101,7 @@ pub fn main() { let result = engine.call_fn_raw(&mut scope, &ast, false, true, "init", Some(&mut states), []); if let Err(err) = result { - eprintln!("! {}", err) + eprintln!("! {err}") } // Create handler instance @@ -152,7 +152,7 @@ pub fn main() { engine.call_fn_raw(scope, ast, false, true, event, this_ptr, [arg.into()]); if let Err(err) = result { - eprintln!("! {}", err) + eprintln!("! {err}") } } } diff --git a/examples/event_handler_main/main.rs b/examples/event_handler_main/main.rs index c70c8740..b5b46577 100644 --- a/examples/event_handler_main/main.rs +++ b/examples/event_handler_main/main.rs @@ -67,7 +67,7 @@ pub fn main() { scope.push_constant("MY_CONSTANT", 42_i64); // Compile the handler script. - println!("> Loading script file: {}", path); + println!("> Loading script file: {path}"); let ast = match engine.compile_file_with_scope(&mut scope, path.into()) { Ok(ast) => ast, @@ -89,7 +89,7 @@ pub fn main() { let result = engine.call_fn_raw(&mut scope, &ast, false, false, "init", None, []); if let Err(err) = result { - eprintln!("! {}", err) + eprintln!("! {err}") } // Create handler instance @@ -127,7 +127,7 @@ pub fn main() { let result = engine.call_fn::<()>(scope, ast, event, (arg.to_string(),)); if let Err(err) = result { - eprintln!("! {}", err) + eprintln!("! {err}") } } } diff --git a/examples/event_handler_map/main.rs b/examples/event_handler_map/main.rs index e45688db..b41795c4 100644 --- a/examples/event_handler_map/main.rs +++ b/examples/event_handler_map/main.rs @@ -81,12 +81,12 @@ pub fn main() { scope.push("state", states); // Compile the handler script. - println!("> Loading script file: {}", path); + println!("> Loading script file: {path}"); let ast = match engine.compile_file_with_scope(&mut scope, path.into()) { Ok(ast) => ast, Err(err) => { - eprintln!("! Error: {}", err); + eprintln!("! Error: {err}"); println!("Cannot continue. Bye!"); return; } @@ -103,7 +103,7 @@ pub fn main() { let result = engine.call_fn::<()>(&mut scope, &ast, "init", ()); if let Err(err) = result { - eprintln!("! {}", err) + eprintln!("! {err}") } // Create handler instance @@ -141,7 +141,7 @@ pub fn main() { let result = engine.call_fn::<()>(scope, ast, event, (arg.to_string(),)); if let Err(err) = result { - eprintln!("! {}", err) + eprintln!("! {err}") } } } diff --git a/examples/hello.rs b/examples/hello.rs index b85b06da..dba66e5b 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -9,7 +9,7 @@ fn main() -> Result<(), Box> { let result = engine.eval::("40 + 2")?; - println!("The Answer: {}", result); // prints 42 + println!("The Answer: {result}"); // prints 42 Ok(()) } diff --git a/examples/reuse_scope.rs b/examples/reuse_scope.rs index 3bfa6bf3..486382a2 100644 --- a/examples/reuse_scope.rs +++ b/examples/reuse_scope.rs @@ -13,7 +13,7 @@ fn main() -> Result<(), Box> { for _ in 0..10 { let result = engine.eval_with_scope::(&mut scope, "x += 1; x")?; - println!("result: {}", result); + println!("result: {result}"); } println!("x = {}", scope.get_value::("x").unwrap()); diff --git a/examples/serde.rs b/examples/serde.rs index b145e247..cbdab2b2 100644 --- a/examples/serde.rs +++ b/examples/serde.rs @@ -36,13 +36,13 @@ fn main() { }, }; - println!("Source struct: {:#?}", x); + println!("Source struct: {x:#?}"); // Convert the 'MyStruct' into a 'Dynamic' let map: Dynamic = to_dynamic(x).unwrap(); assert!(map.is::()); - println!("Serialized to Dynamic: {:#?}", map); + println!("Serialized to Dynamic: {map:#?}"); } pub fn de() { @@ -60,7 +60,7 @@ fn main() { ) .unwrap(); - println!("Source Dynamic: {:#?}", result); + println!("Source Dynamic: {result:#?}"); // Convert the 'Dynamic' object map into 'MyStruct' let x: MyStruct = from_dynamic(&result).unwrap(); @@ -77,7 +77,7 @@ fn main() { }, } ); - println!("Deserialized to struct: {:#?}", x); + println!("Deserialized to struct: {x:#?}"); } ser(); diff --git a/examples/simple_fn.rs b/examples/simple_fn.rs index 06b28c53..887cad30 100644 --- a/examples/simple_fn.rs +++ b/examples/simple_fn.rs @@ -13,7 +13,7 @@ fn main() -> Result<(), Box> { let result = engine.eval::("add(40, 2)")?; - println!("Answer: {}", result); // prints 42 + println!("Answer: {result}"); // prints 42 Ok(()) } diff --git a/examples/strings.rs b/examples/strings.rs index e3fac382..b9c7285a 100644 --- a/examples/strings.rs +++ b/examples/strings.rs @@ -37,10 +37,10 @@ fn main() -> Result<(), Box> { .register_fn("index_of", find_substring) // Register string functions using closures .register_fn("display", |label: &str, value: i64| { - println!("{}: {}", label, value) + println!("{label}: {value}") }) .register_fn("display", |label: ImmutableString, value: &str| { - println!(r#"{}: "{}""#, label, value) // Quote the input string + println!(r#"{label}: "{value}""#) // Quote the input string }); let mut scope = Scope::new(); diff --git a/examples/threading.rs b/examples/threading.rs index 64cd04dc..adcf9bea 100644 --- a/examples/threading.rs +++ b/examples/threading.rs @@ -60,7 +60,7 @@ fn main() { let mut value: i64 = 0; while value < 10 { - println!("Value: {}", value); + println!("Value: {value}"); // Send value to script tx_master.send(value).unwrap(); // Receive value from script diff --git a/src/ast/expr.rs b/src/ast/expr.rs index 72d866fc..7a9e1cae 100644 --- a/src/ast/expr.rs +++ b/src/ast/expr.rs @@ -465,13 +465,13 @@ impl fmt::Debug for Expr { let mut display_pos = format!(" @ {:?}", self.start_position()); match self { - Self::DynamicConstant(value, ..) => write!(f, "{:?}", value), - Self::BoolConstant(value, ..) => write!(f, "{:?}", value), - Self::IntegerConstant(value, ..) => write!(f, "{:?}", value), + Self::DynamicConstant(value, ..) => write!(f, "{value:?}"), + Self::BoolConstant(value, ..) => write!(f, "{value:?}"), + Self::IntegerConstant(value, ..) => write!(f, "{value:?}"), #[cfg(not(feature = "no_float"))] - Self::FloatConstant(value, ..) => write!(f, "{:?}", value), - Self::CharConstant(value, ..) => write!(f, "{:?}", value), - Self::StringConstant(value, ..) => write!(f, "{:?}", value), + Self::FloatConstant(value, ..) => write!(f, "{value:?}"), + Self::CharConstant(value, ..) => write!(f, "{value:?}"), + Self::StringConstant(value, ..) => write!(f, "{value:?}"), Self::Unit(..) => f.write_str("()"), Self::InterpolatedString(x, ..) => { @@ -502,10 +502,10 @@ impl fmt::Debug for Expr { f.write_str(&x.3)?; #[cfg(not(feature = "no_module"))] if let Some(n) = x.1.index() { - write!(f, " #{}", n)?; + write!(f, " #{n}")?; } if let Some(n) = i.map_or_else(|| x.0, |n| NonZeroUsize::new(n.get() as usize)) { - write!(f, " #{}", n)?; + write!(f, " #{n}")?; } f.write_str(")") } diff --git a/src/ast/namespace.rs b/src/ast/namespace.rs index e0939384..2888b25e 100644 --- a/src/ast/namespace.rs +++ b/src/ast/namespace.rs @@ -37,7 +37,7 @@ impl fmt::Debug for Namespace { } if let Some(index) = self.index { - write!(f, "{} -> ", index)?; + write!(f, "{index} -> ")?; } f.write_str( diff --git a/src/ast/stmt.rs b/src/ast/stmt.rs index 80ed1b36..284d0133 100644 --- a/src/ast/stmt.rs +++ b/src/ast/stmt.rs @@ -185,8 +185,8 @@ impl fmt::Debug for RangeCase { #[inline(never)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::ExclusiveInt(r, n) => write!(f, "{}..{} => {}", r.start, r.end, n), - Self::InclusiveInt(r, n) => write!(f, "{}..={} => {}", *r.start(), *r.end(), n), + Self::ExclusiveInt(r, n) => write!(f, "{}..{} => {n}", r.start, r.end), + Self::InclusiveInt(r, n) => write!(f, "{}..={} => {n}", *r.start(), *r.end()), } } } diff --git a/src/bin/rhai-dbg.rs b/src/bin/rhai-dbg.rs index 71d5152a..5649c0c1 100644 --- a/src/bin/rhai-dbg.rs +++ b/src/bin/rhai-dbg.rs @@ -75,7 +75,7 @@ fn print_current_source( } if !src.is_empty() { // Print just a line number for imported modules - println!("{} @ {:?}", src, pos); + println!("{src} @ {pos:?}"); } else { // Print the current source line print_source(lines, pos, 0, window); @@ -100,17 +100,16 @@ fn print_error(input: &str, mut err: EvalAltResult) { // Print error position if pos.is_none() { // No position - println!("{}", err); + println!("{err}"); } else { // Specific position - print line text - println!("{}{}", line_no, lines[pos.line().unwrap() - 1]); + println!("{line_no}{}", lines[pos.line().unwrap() - 1]); // Display position marker println!( - "{0:>1$} {2}", + "{0:>1$} {err}", "^", line_no.len() + pos.position().unwrap(), - err ); } } @@ -247,11 +246,11 @@ fn debug_callback( BreakPoint::AtPosition { .. } => (), BreakPoint::AtFunctionName { ref name, .. } | BreakPoint::AtFunctionCall { ref name, .. } => { - println!("! Call to function {}.", name) + println!("! Call to function {name}.") } #[cfg(not(feature = "no_object"))] BreakPoint::AtProperty { ref name, .. } => { - println!("! Property {} accessed.", name) + println!("! Property {name} accessed.") } _ => unreachable!(), } @@ -311,8 +310,8 @@ fn debug_callback( println!("{:?}", node); } else { match source { - Some(source) => println!("{:?} {} @ {:?}", node, source, pos), - None => println!("{:?} @ {:?}", node, pos), + Some(source) => println!("{node:?} {source} @ {pos:?}"), + None => println!("{node:?} @ {pos:?}"), } } println!(); @@ -327,7 +326,7 @@ fn debug_callback( ["list" | "l", n] if n.parse::().is_ok() => { let num = n.parse::().unwrap(); if num == 0 || num > lines.len() { - eprintln!("\x1b[31mInvalid line: {}\x1b[39m", num); + eprintln!("\x1b[31mInvalid line: {num}\x1b[39m"); } else { let pos = Position::new(num as u16, 0); print_current_source(&mut context, source, pos, lines, (3, 6)); @@ -340,17 +339,17 @@ fn debug_callback( ["next" | "n"] => break Ok(DebuggerCommand::Next), ["scope"] => println!("{}", context.scope()), ["print" | "p", "this"] => match context.this_ptr() { - Some(value) => println!("=> {:?}", value), + Some(value) => println!("=> {value:?}"), None => println!("`this` pointer is unbound."), }, ["print" | "p", var_name] => match context.scope().get_value::(var_name) { - Some(value) => println!("=> {:?}", value), - None => eprintln!("Variable not found: {}", var_name), + Some(value) => println!("=> {value:?}"), + None => eprintln!("Variable not found: {var_name}"), }, ["print" | "p"] => { println!("{}", context.scope().clone_visible()); if let Some(value) = context.this_ptr() { - println!("this = {:?}", value); + println!("this = {value:?}"); } } #[cfg(not(feature = "no_module"))] @@ -379,7 +378,7 @@ fn debug_callback( .iter() .rev() { - println!("{}", frame) + println!("{frame}") } } ["info" | "i", "break" | "b"] => Iterator::for_each( @@ -396,7 +395,7 @@ fn debug_callback( print!("{}", line_num); print_source(lines, *pos, line_num.len(), (0, 0)); } - _ => println!("[{}] {}", i + 1, bp), + _ => println!("[{}] {bp}", i + 1), }, ), ["enable" | "en", n] => { @@ -414,12 +413,12 @@ fn debug_callback( .get_mut(n - 1) .unwrap() .enable(true); - println!("Break-point #{} enabled.", n) + println!("Break-point #{n} enabled.") } else { - eprintln!("\x1b[31mInvalid break-point: {}\x1b[39m", n); + eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m"); } } else { - eprintln!("\x1b[31mInvalid break-point: '{}'\x1b[39m", n); + eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m"); } } ["disable" | "dis", n] => { @@ -437,12 +436,12 @@ fn debug_callback( .get_mut(n - 1) .unwrap() .enable(false); - println!("Break-point #{} disabled.", n) + println!("Break-point #{n} disabled.") } else { - eprintln!("\x1b[31mInvalid break-point: {}\x1b[39m", n); + eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m"); } } else { - eprintln!("\x1b[31mInvalid break-point: '{}'\x1b[39m", n); + eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m"); } } ["delete" | "d", n] => { @@ -458,12 +457,12 @@ fn debug_callback( .debugger .break_points_mut() .remove(n - 1); - println!("Break-point #{} deleted.", n) + println!("Break-point #{n} deleted.") } else { - eprintln!("\x1b[31mInvalid break-point: {}\x1b[39m", n); + eprintln!("\x1b[31mInvalid break-point: {n}\x1b[39m"); } } else { - eprintln!("\x1b[31mInvalid break-point: '{}'\x1b[39m", n); + eprintln!("\x1b[31mInvalid break-point: '{n}'\x1b[39m"); } } ["delete" | "d"] => { @@ -481,14 +480,14 @@ fn debug_callback( args, enabled: true, }; - println!("Break-point added for {}", bp); + println!("Break-point added for {bp}"); context .global_runtime_state_mut() .debugger .break_points_mut() .push(bp); } else { - eprintln!("\x1b[31mInvalid number of arguments: '{}'\x1b[39m", args); + eprintln!("\x1b[31mInvalid number of arguments: '{args}'\x1b[39m"); } } // Property name @@ -498,7 +497,7 @@ fn debug_callback( name: param[1..].into(), enabled: true, }; - println!("Break-point added for {}", bp); + println!("Break-point added for {bp}"); context .global_runtime_state_mut() .debugger @@ -521,14 +520,14 @@ fn debug_callback( pos: Position::new(n as u16, 0), enabled: true, }; - println!("Break-point added {}", bp); + println!("Break-point added {bp}"); context .global_runtime_state_mut() .debugger .break_points_mut() .push(bp); } else { - eprintln!("\x1b[31mInvalid line number: '{}'\x1b[39m", n); + eprintln!("\x1b[31mInvalid line number: '{n}'\x1b[39m"); } } // Function name parameter @@ -537,7 +536,7 @@ fn debug_callback( name: param.trim().into(), enabled: true, }; - println!("Break-point added for {}", bp); + println!("Break-point added for {bp}"); context .global_runtime_state_mut() .debugger @@ -551,7 +550,7 @@ fn debug_callback( pos, enabled: true, }; - println!("Break-point added {}", bp); + println!("Break-point added {bp}"); context .global_runtime_state_mut() .debugger @@ -588,7 +587,7 @@ fn debug_callback( fn main() { let title = format!("Rhai Debugger (version {})", env!("CARGO_PKG_VERSION")); - println!("{}", title); + println!("{title}"); println!("{0:=<1$}", "", title.len()); // Initialize scripting engine diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index dcab9974..71801d82 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -26,17 +26,16 @@ fn print_error(input: &str, mut err: EvalAltResult) { // Print error position if pos.is_none() { // No position - println!("{}", err); + println!("{err}"); } else { // Specific position - print line text - println!("{}{}", line_no, lines[pos.line().unwrap() - 1]); + println!("{line_no}{}", lines[pos.line().unwrap() - 1]); // Display position marker println!( - "{0:>1$} {2}", + "{0:>1$} {err}", "^", line_no.len() + pos.position().unwrap(), - err ); } } @@ -119,7 +118,7 @@ fn load_script_files(engine: &mut Engine) { for filename in env::args().skip(1) { let filename = match Path::new(&filename).canonicalize() { Err(err) => { - eprintln!("Error script file path: {}\n{}", filename, err); + eprintln!("Error script file path: {filename}\n{err}"); exit(1); } Ok(f) => { @@ -164,7 +163,7 @@ fn load_script_files(engine: &mut Engine) { let filename = filename.to_string_lossy(); eprintln!("{:=<1$}", "", filename.len()); - eprintln!("{}", filename); + eprintln!("{filename}"); eprintln!("{:=<1$}", "", filename.len()); eprintln!(); @@ -277,13 +276,13 @@ mod sample_functions { #[rhai_fn(name = "test")] pub fn test2(x: &mut INT, y: INT, z: &str) { *x += y + (z.len() as INT); - println!("{} {} {}", x, y, z); + println!("{x} {y} {z}"); } } fn main() { let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION")); - println!("{}", title); + println!("{title}"); println!("{0:=<1$}", "", title.len()); #[cfg(not(feature = "no_optimize"))] @@ -338,11 +337,11 @@ fn main() { history_offset += 1; } if input.contains('\n') { - println!("[{}] ~~~~", replacement_index); - println!("{}", input); + println!("[{replacement_index}] ~~~~"); + println!("{input}"); println!("~~~~"); } else { - println!("[{}] {}", replacement_index, input); + println!("[{replacement_index}] {input}"); } replacement_index = 0; } else { @@ -374,7 +373,7 @@ fn main() { Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break 'main_loop, Err(err) => { - eprintln!("Error: {:?}", err); + eprintln!("Error: {err:?}"); break 'main_loop; } } @@ -401,12 +400,12 @@ fn main() { "history" => { for (i, h) in rl.history().iter().enumerate() { match &h.split('\n').collect::>()[..] { - [line] => println!("[{}] {}", history_offset + i, line), + [line] => println!("[{}] {line}", history_offset + i), lines => { for (x, line) in lines.iter().enumerate() { let number = format!("[{}]", history_offset + i); if x == 0 { - println!("{} {}", number, line.trim_end()); + println!("{number} {}", line.trim_end()); } else { println!("{0:>1$} {2}", "", number.len(), line.trim_end()); } @@ -439,30 +438,30 @@ fn main() { continue; } "scope" => { - println!("{}", scope); + println!("{scope}"); continue; } #[cfg(not(feature = "no_optimize"))] "astu" => { // print the last un-optimized AST - println!("{:#?}\n", ast_u); + println!("{ast_u:#?}\n"); continue; } "ast" => { // print the last AST - println!("{:#?}\n", ast); + println!("{ast:#?}\n"); continue; } #[cfg(feature = "metadata")] "functions" => { // print a list of all registered functions for f in engine.gen_fn_signatures(false) { - println!("{}", f) + println!("{f}") } #[cfg(not(feature = "no_function"))] for f in main_ast.iter_functions() { - println!("{}", f) + println!("{f}") } println!(); @@ -505,7 +504,7 @@ fn main() { replacement = Some(line.clone()); replacement_index = history_offset + (rl.history().len() - 1 - n); } - None => eprintln!("History line not found: {}", text), + None => eprintln!("History line not found: {text}"), } continue; } @@ -561,7 +560,7 @@ fn main() { engine.eval_ast_with_scope::(&mut scope, &main_ast) }) { Ok(result) if !result.is::<()>() => { - println!("=> {:?}", result); + println!("=> {result:?}"); println!(); } Ok(_) => (), diff --git a/src/bin/rhai-run.rs b/src/bin/rhai-run.rs index e87e99b4..5bd464a3 100644 --- a/src/bin/rhai-run.rs +++ b/src/bin/rhai-run.rs @@ -7,12 +7,11 @@ fn eprint_error(input: &str, mut err: EvalAltResult) { let line = pos.line().unwrap(); let line_no = format!("{line}: "); - eprintln!("{}{}", line_no, lines[line - 1]); + eprintln!("{line_no}{}", lines[line - 1]); eprintln!( - "{:>1$} {2}", + "{:>1$} {err_msg}", "^", line_no.len() + pos.position().unwrap(), - err_msg ); eprintln!(); } @@ -24,7 +23,7 @@ fn eprint_error(input: &str, mut err: EvalAltResult) { if pos.is_none() { // No position - eprintln!("{}", err); + eprintln!("{err}"); } else { // Specific position eprint_line(&lines, pos, &err.to_string()) @@ -37,7 +36,7 @@ fn main() { for filename in env::args().skip(1) { let filename = match Path::new(&filename).canonicalize() { Err(err) => { - eprintln!("Error script file path: {}\n{}", filename, err); + eprintln!("Error script file path: {filename}\n{err}"); exit(1); } Ok(f) => match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap()) @@ -94,7 +93,7 @@ fn main() { let filename = filename.to_string_lossy(); eprintln!("{:=<1$}", "", filename.len()); - eprintln!("{}", filename); + eprintln!("{filename}"); eprintln!("{:=<1$}", "", filename.len()); eprintln!(); diff --git a/src/engine.rs b/src/engine.rs index 8edc62ff..8071b9de 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -85,7 +85,7 @@ pub const OP_INCLUSIVE_RANGE: &str = Token::InclusiveRange.literal_syntax(); /// /// let result = engine.eval::("40 + 2")?; /// -/// println!("Answer: {}", result); // prints 42 +/// println!("Answer: {result}"); // prints 42 /// # Ok(()) /// # } /// ``` @@ -236,18 +236,12 @@ impl Engine { #[cfg(not(feature = "no_std"))] #[cfg(not(target_family = "wasm"))] { - engine.print = Box::new(|s| println!("{}", s)); - engine.debug = Box::new(|s, source, pos| { - source.map_or_else( - || { - if pos.is_none() { - println!("{s}"); - } else { - println!("{pos:?} | {s}"); - } - }, - |source| println!("{source} @ {pos:?} | {s}"), - ) + engine.print = Box::new(|s| println!("{s}")); + engine.debug = Box::new(|s, source, pos| match (source, pos) { + (Some(source), crate::Position::NONE) => println!("{source} | {s}"), + (Some(source), pos) => println!("{source} @ {pos:?} | {s}"), + (None, crate::Position::NONE) => println!("{s}"), + (None, pos) => println!("{pos:?} | {s}"), }); } diff --git a/src/eval/debugger.rs b/src/eval/debugger.rs index 249b0319..63bb3d22 100644 --- a/src/eval/debugger.rs +++ b/src/eval/debugger.rs @@ -148,35 +148,30 @@ impl fmt::Display for BreakPoint { pos, enabled, } => { - if source.is_empty() { - write!(f, "@ {:?}", pos)?; - } else { - write!(f, "{} @ {:?}", source, pos)?; + if !source.is_empty() { + write!(f, "{source} ")?; } + write!(f, "@ {pos:?}")?; if !*enabled { f.write_str(" (disabled)")?; } Ok(()) } - Self::AtFunctionName { - name: fn_name, - enabled, - } => { - write!(f, "{} (...)", fn_name)?; + Self::AtFunctionName { name, enabled } => { + write!(f, "{name} (...)")?; if !*enabled { f.write_str(" (disabled)")?; } Ok(()) } Self::AtFunctionCall { - name: fn_name, + name, args, enabled, } => { write!( f, - "{} ({})", - fn_name, + "{name} ({})", repeat("_").take(*args).collect::>().join(", ") )?; if !*enabled { @@ -185,11 +180,8 @@ impl fmt::Display for BreakPoint { Ok(()) } #[cfg(not(feature = "no_object"))] - Self::AtProperty { - name: prop, - enabled, - } => { - write!(f, ".{}", prop)?; + Self::AtProperty { name, enabled } => { + write!(f, ".{name}")?; if !*enabled { f.write_str(" (disabled)")?; } @@ -251,11 +243,10 @@ impl fmt::Display for CallStackFrame { fp.finish()?; if !self.pos.is_none() { - if self.source.is_empty() { - write!(f, " @ {:?}", self.pos)?; - } else { - write!(f, ": {} @ {:?}", self.source, self.pos)?; + if !self.source.is_empty() { + write!(f, ": {}", self.source)?; } + write!(f, " @ {:?}", self.pos)?; } Ok(()) diff --git a/src/func/callable_function.rs b/src/func/callable_function.rs index e6b85a29..f11d41a7 100644 --- a/src/func/callable_function.rs +++ b/src/func/callable_function.rs @@ -31,10 +31,10 @@ impl fmt::Debug for CallableFunction { #[inline(never)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Pure(..) => write!(f, "NativePureFunction"), - Self::Method(..) => write!(f, "NativeMethod"), - Self::Iterator(..) => write!(f, "NativeIterator"), - Self::Plugin(..) => write!(f, "PluginFunction"), + Self::Pure(..) => f.write_str("NativePureFunction"), + Self::Method(..) => f.write_str("NativeMethod"), + Self::Iterator(..) => f.write_str("NativeIterator"), + Self::Plugin(..) => f.write_str("PluginFunction"), #[cfg(not(feature = "no_function"))] Self::Script(fn_def) => fmt::Debug::fmt(fn_def, f), @@ -45,10 +45,10 @@ impl fmt::Debug for CallableFunction { impl fmt::Display for CallableFunction { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Pure(..) => write!(f, "NativePureFunction"), - Self::Method(..) => write!(f, "NativeMethod"), - Self::Iterator(..) => write!(f, "NativeIterator"), - Self::Plugin(..) => write!(f, "PluginFunction"), + Self::Pure(..) => f.write_str("NativePureFunction"), + Self::Method(..) => f.write_str("NativeMethod"), + Self::Iterator(..) => f.write_str("NativeIterator"), + Self::Plugin(..) => f.write_str("PluginFunction"), #[cfg(not(feature = "no_function"))] Self::Script(s) => fmt::Display::fmt(s, f), diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 0c9b7661..c53b8ea3 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -416,7 +416,7 @@ impl Hash for Dynamic { impl fmt::Display for Dynamic { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.0 { - Union::Unit(..) => write!(f, ""), + Union::Unit(..) => Ok(()), Union::Bool(ref v, ..) => fmt::Display::fmt(v, f), Union::Str(ref v, ..) => fmt::Display::fmt(v, f), Union::Char(ref v, ..) => fmt::Display::fmt(v, f), @@ -527,7 +527,7 @@ impl fmt::Debug for Dynamic { if i > 0 && i % 8 == 0 { f.write_str(" ")?; } - write!(f, "{:02x}", v) + write!(f, "{v:02x}") })?; f.write_str("]") } diff --git a/src/types/parse_error.rs b/src/types/parse_error.rs index 7ac362af..d11a49c6 100644 --- a/src/types/parse_error.rs +++ b/src/types/parse_error.rs @@ -38,11 +38,11 @@ impl Error for LexError {} impl fmt::Display for LexError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::UnexpectedInput(s) => write!(f, "Unexpected '{}'", s), - Self::MalformedEscapeSequence(s) => write!(f, "Invalid escape sequence: '{}'", s), - Self::MalformedNumber(s) => write!(f, "Invalid number: '{}'", s), - Self::MalformedChar(s) => write!(f, "Invalid character: '{}'", s), - Self::MalformedIdentifier(s) => write!(f, "Variable name is not proper: '{}'", s), + Self::UnexpectedInput(s) => write!(f, "Unexpected '{s}'"), + Self::MalformedEscapeSequence(s) => write!(f, "Invalid escape sequence: '{s}'"), + Self::MalformedNumber(s) => write!(f, "Invalid number: '{s}'"), + Self::MalformedChar(s) => write!(f, "Invalid character: '{s}'"), + Self::MalformedIdentifier(s) => write!(f, "Variable name is not proper: '{s}'"), Self::UnterminatedString => f.write_str("Open string is not terminated"), Self::StringTooLong(max) => write!( f, @@ -50,7 +50,7 @@ impl fmt::Display for LexError { max ), Self::ImproperSymbol(s, d) if d.is_empty() => { - write!(f, "Invalid symbol encountered: '{}'", s) + write!(f, "Invalid symbol encountered: '{s}'") } Self::ImproperSymbol(.., d) => f.write_str(d), }