Use String::new() for empty strings.

This commit is contained in:
Stephen Chung 2022-08-21 17:35:44 +08:00
parent 248888ce0b
commit 2f02b30b6e
7 changed files with 25 additions and 27 deletions

View File

@ -72,16 +72,13 @@ impl Engine {
Token::LeftBrace => Token::MapStart, Token::LeftBrace => Token::MapStart,
// Disallowed syntax // Disallowed syntax
t @ (Token::Unit | Token::MapStart) => Token::LexError( t @ (Token::Unit | Token::MapStart) => Token::LexError(
LexError::ImproperSymbol( LexError::ImproperSymbol(t.literal_syntax().to_string(), String::new())
t.literal_syntax().to_string(),
"".to_string(),
)
.into(), .into(),
), ),
Token::InterpolatedString(..) => Token::LexError( Token::InterpolatedString(..) => Token::LexError(
LexError::ImproperSymbol( LexError::ImproperSymbol(
"interpolated string".to_string(), "interpolated string".to_string(),
"".to_string(), String::new(),
) )
.into(), .into(),
), ),
@ -93,7 +90,7 @@ impl Engine {
Some(&|token, _, _| { Some(&|token, _, _| {
match token { match token {
Token::Reserved(s) if &*s == "null" => Token::LexError( 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, Token::LeftBrace => Token::MapStart,

View File

@ -90,12 +90,12 @@ fn print_error(input: &str, mut err: EvalAltResult) {
let line_no = if lines.len() > 1 { let line_no = if lines.len() > 1 {
if pos.is_none() { if pos.is_none() {
"".to_string() String::new()
} else { } else {
format!("{}: ", pos.line().unwrap()) format!("{}: ", pos.line().unwrap())
} }
} else { } else {
"".to_string() String::new()
}; };
// Print error position // Print error position

View File

@ -15,12 +15,12 @@ fn print_error(input: &str, mut err: EvalAltResult) {
let line_no = if lines.len() > 1 { let line_no = if lines.len() > 1 {
if pos.is_none() { if pos.is_none() {
"".to_string() String::new()
} else { } else {
format!("{}: ", pos.line().unwrap()) format!("{}: ", pos.line().unwrap())
} }
} else { } else {
"".to_string() String::new()
}; };
// Print error position // Print error position

View File

@ -2016,7 +2016,7 @@ impl Module {
if let Some(fn_ptr) = value.downcast_ref::<crate::FnPtr>() { if let Some(fn_ptr) = value.downcast_ref::<crate::FnPtr>() {
if ast.iter_fn_def().any(|f| f.name == fn_ptr.fn_name()) { if ast.iter_fn_def().any(|f| f.name == fn_ptr.fn_name()) {
return Err(crate::ERR::ErrorMismatchDataType( return Err(crate::ERR::ErrorMismatchDataType(
"".to_string(), String::new(),
if fn_ptr.is_anonymous() { if fn_ptr.is_anonymous() {
format!("cannot export closure in variable {_name}") format!("cannot export closure in variable {_name}")
} else { } else {

View File

@ -2051,12 +2051,12 @@ impl Engine {
Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into())) Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into()))
} }
// expr[???] = rhs, expr.??? = rhs // expr[???] = rhs, expr.??? = rhs
ref expr => Err(PERR::AssignmentToInvalidLHS("".to_string()) ref expr => Err(PERR::AssignmentToInvalidLHS(String::new())
.into_err(expr.position())), .into_err(expr.position())),
} }
} }
Some(err_pos) => { 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)), .into_err(op_pos)),
// expr = rhs // 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::Pipe, ..) => break,
(Token::Identifier(s), pos) => { (Token::Identifier(s), pos) => {
if params_list.iter().any(|p| p.as_str() == &*s) { if params_list.iter().any(|p| p.as_str() == &*s) {
return Err(PERR::FnDuplicatedParam("".to_string(), s.to_string()) return Err(
.into_err(pos)); PERR::FnDuplicatedParam(String::new(), s.to_string()).into_err(pos)
);
} }
let s = state.get_interned_string(s); let s = state.get_interned_string(s);
state.stack.push(s.clone(), ()); state.stack.push(s.clone(), ());

View File

@ -25,28 +25,28 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
.compile("(x+y) = 42;") .compile("(x+y) = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
assert_eq!( assert_eq!(
*engine *engine
.compile("foo(x) = 42;") .compile("foo(x) = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
assert_eq!( assert_eq!(
*engine *engine
.compile("true = 42;") .compile("true = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToConstant("".to_string()) ParseErrorType::AssignmentToConstant(String::new())
); );
assert_eq!( assert_eq!(
*engine *engine
.compile("123 = 42;") .compile("123 = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToConstant("".to_string()) ParseErrorType::AssignmentToConstant(String::new())
); );
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
@ -56,21 +56,21 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
.compile("x.foo() = 42;") .compile("x.foo() = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
assert_eq!( assert_eq!(
*engine *engine
.compile("x.foo().x.y = 42;") .compile("x.foo().x.y = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
assert_eq!( assert_eq!(
*engine *engine
.compile("x.y.z.foo() = 42;") .compile("x.y.z.foo() = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
assert_eq!( assert_eq!(
@ -78,7 +78,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
.compile("x.foo()[0] = 42;") .compile("x.foo()[0] = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
assert_eq!( assert_eq!(
@ -86,7 +86,7 @@ fn test_assignments_bad_lhs() -> Result<(), Box<EvalAltResult>> {
.compile("x[y].z.foo() = 42;") .compile("x[y].z.foo() = 42;")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::AssignmentToInvalidLHS("".to_string()) ParseErrorType::AssignmentToInvalidLHS(String::new())
); );
} }

View File

@ -303,7 +303,7 @@ fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
.compile("hello hey") .compile("hello hey")
.expect_err("should error") .expect_err("should error")
.err_type(), .err_type(),
ParseErrorType::BadInput(LexError::ImproperSymbol("hey".to_string(), "".to_string())) ParseErrorType::BadInput(LexError::ImproperSymbol("hey".to_string(), String::new()))
); );
Ok(()) Ok(())