From 2f02b30b6ef9915f7228930f4b45bde5149bbbaa Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 21 Aug 2022 17:35:44 +0800 Subject: [PATCH] Use String::new() for empty strings. --- src/api/json.rs | 11 ++++------- src/bin/rhai-dbg.rs | 4 ++-- src/bin/rhai-repl.rs | 4 ++-- src/module/mod.rs | 2 +- src/parser.rs | 11 ++++++----- tests/assignments.rs | 18 +++++++++--------- tests/custom_syntax.rs | 2 +- 7 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/api/json.rs b/src/api/json.rs index ae00bbed..034062bf 100644 --- a/src/api/json.rs +++ b/src/api/json.rs @@ -72,16 +72,13 @@ impl Engine { Token::LeftBrace => Token::MapStart, // Disallowed syntax t @ (Token::Unit | Token::MapStart) => Token::LexError( - LexError::ImproperSymbol( - t.literal_syntax().to_string(), - "".to_string(), - ) - .into(), + LexError::ImproperSymbol(t.literal_syntax().to_string(), String::new()) + .into(), ), Token::InterpolatedString(..) => Token::LexError( LexError::ImproperSymbol( "interpolated string".to_string(), - "".to_string(), + String::new(), ) .into(), ), @@ -93,7 +90,7 @@ impl Engine { Some(&|token, _, _| { match token { Token::Reserved(s) if &*s == "null" => Token::LexError( - LexError::ImproperSymbol("null".to_string(), "".to_string()).into(), + LexError::ImproperSymbol("null".to_string(), String::new()).into(), ), // `{` => `#{` Token::LeftBrace => Token::MapStart, diff --git a/src/bin/rhai-dbg.rs b/src/bin/rhai-dbg.rs index 30d521fa..a83b680c 100644 --- a/src/bin/rhai-dbg.rs +++ b/src/bin/rhai-dbg.rs @@ -90,12 +90,12 @@ fn print_error(input: &str, mut err: EvalAltResult) { let line_no = if lines.len() > 1 { if pos.is_none() { - "".to_string() + String::new() } else { format!("{}: ", pos.line().unwrap()) } } else { - "".to_string() + String::new() }; // Print error position diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 932bcf30..00e01e8b 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -15,12 +15,12 @@ fn print_error(input: &str, mut err: EvalAltResult) { let line_no = if lines.len() > 1 { if pos.is_none() { - "".to_string() + String::new() } else { format!("{}: ", pos.line().unwrap()) } } else { - "".to_string() + String::new() }; // Print error position diff --git a/src/module/mod.rs b/src/module/mod.rs index fb3e47ab..3afd1d18 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -2016,7 +2016,7 @@ impl Module { if let Some(fn_ptr) = value.downcast_ref::() { if ast.iter_fn_def().any(|f| f.name == fn_ptr.fn_name()) { return Err(crate::ERR::ErrorMismatchDataType( - "".to_string(), + String::new(), if fn_ptr.is_anonymous() { format!("cannot export closure in variable {_name}") } else { diff --git a/src/parser.rs b/src/parser.rs index 754513c8..48a8a97b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2051,12 +2051,12 @@ impl Engine { Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into())) } // expr[???] = rhs, expr.??? = rhs - ref expr => Err(PERR::AssignmentToInvalidLHS("".to_string()) + ref expr => Err(PERR::AssignmentToInvalidLHS(String::new()) .into_err(expr.position())), } } Some(err_pos) => { - Err(PERR::AssignmentToInvalidLHS("".to_string()).into_err(err_pos)) + Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(err_pos)) } } } @@ -2067,7 +2067,7 @@ impl Engine { ) .into_err(op_pos)), // expr = rhs - _ => Err(PERR::AssignmentToInvalidLHS("".to_string()).into_err(lhs.position())), + _ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())), } } @@ -3665,8 +3665,9 @@ impl Engine { (Token::Pipe, ..) => break, (Token::Identifier(s), pos) => { if params_list.iter().any(|p| p.as_str() == &*s) { - return Err(PERR::FnDuplicatedParam("".to_string(), s.to_string()) - .into_err(pos)); + return Err( + PERR::FnDuplicatedParam(String::new(), s.to_string()).into_err(pos) + ); } let s = state.get_interned_string(s); state.stack.push(s.clone(), ()); diff --git a/tests/assignments.rs b/tests/assignments.rs index 1be3901b..e2a90d4f 100644 --- a/tests/assignments.rs +++ b/tests/assignments.rs @@ -25,28 +25,28 @@ fn test_assignments_bad_lhs() -> Result<(), Box> { .compile("(x+y) = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); assert_eq!( *engine .compile("foo(x) = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); assert_eq!( *engine .compile("true = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToConstant("".to_string()) + ParseErrorType::AssignmentToConstant(String::new()) ); assert_eq!( *engine .compile("123 = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToConstant("".to_string()) + ParseErrorType::AssignmentToConstant(String::new()) ); #[cfg(not(feature = "no_object"))] @@ -56,21 +56,21 @@ fn test_assignments_bad_lhs() -> Result<(), Box> { .compile("x.foo() = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); assert_eq!( *engine .compile("x.foo().x.y = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); assert_eq!( *engine .compile("x.y.z.foo() = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); #[cfg(not(feature = "no_index"))] assert_eq!( @@ -78,7 +78,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box> { .compile("x.foo()[0] = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); #[cfg(not(feature = "no_index"))] assert_eq!( @@ -86,7 +86,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box> { .compile("x[y].z.foo() = 42;") .expect_err("should error") .err_type(), - ParseErrorType::AssignmentToInvalidLHS("".to_string()) + ParseErrorType::AssignmentToInvalidLHS(String::new()) ); } diff --git a/tests/custom_syntax.rs b/tests/custom_syntax.rs index 0f7992f4..6c70dcd8 100644 --- a/tests/custom_syntax.rs +++ b/tests/custom_syntax.rs @@ -303,7 +303,7 @@ fn test_custom_syntax_raw() -> Result<(), Box> { .compile("hello hey") .expect_err("should error") .err_type(), - ParseErrorType::BadInput(LexError::ImproperSymbol("hey".to_string(), "".to_string())) + ParseErrorType::BadInput(LexError::ImproperSymbol("hey".to_string(), String::new())) ); Ok(())