From 07264a5f67d4077d6c94dc8afd64e379e19e1329 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sun, 19 Feb 2023 02:14:01 +0100 Subject: [PATCH] fix a lot of warnings --- src/dyn_fns.rs | 15 +++++++------ src/lexer.rs | 34 ++++++++++++++--------------- src/runtime.rs | 58 +++++++++++++++++++++++++++++++------------------- src/std_fns.rs | 11 +++++----- 4 files changed, 67 insertions(+), 51 deletions(-) diff --git a/src/dyn_fns.rs b/src/dyn_fns.rs index f64c1f8..800e5b6 100644 --- a/src/dyn_fns.rs +++ b/src/dyn_fns.rs @@ -141,14 +141,14 @@ pub fn dyn_call(stack: &mut Stack) -> OError { }; let mut words = Vec::new(); let mut ra = 0; - while s.starts_with("&") { + while s.starts_with('&') { ra += 1; s = s[1..].to_owned(); } - if s.ends_with(";") { + if s.ends_with(';') { words.push(Word::Call(s[..s.len() - 1].to_owned(), true, ra)); } else { - words.push(Word::Call(s.to_owned(), false, ra)); + words.push(Word::Call(s, false, ra)); } Words { words }.exec(stack)?; Ok(()) @@ -160,14 +160,14 @@ pub fn dyn_objcall(stack: &mut Stack) -> OError { }; let mut words = Vec::new(); let mut ra = 0; - while s.starts_with("&") { + while s.starts_with('&') { ra += 1; s = s[1..].to_owned(); } - if s.ends_with(";") { + if s.ends_with(';') { words.push(Word::ObjCall(s[..s.len() - 1].to_owned(), true, ra)); } else { - words.push(Word::ObjCall(s.to_owned(), false, ra)); + words.push(Word::ObjCall(s, false, ra)); } Words { words }.exec(stack)?; Ok(()) @@ -240,7 +240,8 @@ pub fn dyn_readf(stack: &mut Stack) -> OError { } pub fn register(r: &mut Stack, o: Arc) { - let fns: [(&str, fn(&mut Stack) -> OError, u32); 14] = [ + type Fn = fn(&mut Stack) -> OError; + let fns: [(&str, Fn, u32); 14] = [ ("dyn-__dump", dyn_dump, 0), ("dyn-def", dyn_def, 0), ("dyn-func", dyn_func, 0), diff --git a/src/lexer.rs b/src/lexer.rs index c15b71d..5a2b51c 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -69,7 +69,7 @@ fn read_block( })))) } "construct" => { - let name = (&str_words[i + 1]).to_owned(); + let name = str_words[i + 1].to_owned(); assert_eq!( str_words[i + 2], "{", @@ -78,7 +78,7 @@ fn read_block( let mut fields = Vec::new(); i += 3; while str_words[i] != ";" && str_words[i] != "}" { - fields.push((&str_words[i]).to_owned()); + fields.push(str_words[i].to_owned()); i += 1; } let mut methods = Vec::new(); @@ -86,7 +86,7 @@ fn read_block( if str_words[i] == ";" { i += 1; while str_words[i] != "}" { - let name = (&str_words[i]).to_owned(); + let name = str_words[i].to_owned(); if name == "construct" { has_construct = true; } @@ -137,7 +137,7 @@ fn read_block( let mut vars = Vec::new(); i += 1; while &str_words[i] != ";" { - vars.push((&str_words[i]).to_owned()); + vars.push(str_words[i].to_owned()); i += 1; } words.push(Word::Key(Keyword::With(vars))); @@ -145,37 +145,37 @@ fn read_block( "}" => { break; } - x if x.starts_with("\"") => { + x if x.starts_with('\"') => { words.push(Word::Const(Value::Str(x[1..].to_owned()))); } - x if x.chars().all(|c| c.is_numeric() || c == '_') && !x.starts_with("_") => { + x if x.chars().all(|c| c.is_numeric() || c == '_') && !x.starts_with('_') => { words.push(Word::Const(Value::Mega(x.parse().unwrap()))); } x if x.chars().all(|c| c.is_numeric() || c == '.' || c == '_') - && !x.starts_with("_") => + && !x.starts_with('_') => { words.push(Word::Const(Value::Double(x.parse().unwrap()))); } x => { - let mut word = x.split(":").next().unwrap(); + let mut word = x.split(':').next().unwrap(); let mut ra = 0; - while word.starts_with("&") { + while word.starts_with('&') { ra += 1; word = &word[1..]; } - if word.ends_with(";") { - words.push(Word::Call(word[..word.len() - 1].to_owned(), true, ra)); + if let Some(word) = word.strip_suffix(';') { + words.push(Word::Call(word.to_owned(), true, ra)); } else { words.push(Word::Call(word.to_owned(), false, ra)); } - for mut word in x.split(":").skip(1) { + for mut word in x.split(':').skip(1) { let mut ra = 0; - while word.starts_with("&") { + while word.starts_with('&') { ra += 1; word = &word[1..]; } - if word.ends_with(";") { - words.push(Word::ObjCall(word[..word.len() - 1].to_owned(), true, ra)); + if let Some(word) = word.strip_suffix(';') { + words.push(Word::ObjCall(word.to_owned(), true, ra)); } else { words.push(Word::ObjCall(word.to_owned(), false, ra)); } @@ -234,7 +234,7 @@ fn parse_line(line: &str) -> Vec { continue; } if c == ' ' { - if s == "" { + if s.is_empty() { continue; } words.push(s); @@ -246,7 +246,7 @@ fn parse_line(line: &str) -> Vec { was_in_string = false; s += String::from(c).as_str(); } - if s != "" { + if !s.is_empty() { words.push(s); } words diff --git a/src/runtime.rs b/src/runtime.rs index a7817d5..6a260e8 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -31,6 +31,12 @@ pub struct Runtime { types_by_id: HashMap, } +impl Default for Runtime { + fn default() -> Self { + Self::new() + } +} + impl Runtime { pub fn new() -> Self { let mut rt = Runtime { @@ -38,15 +44,15 @@ impl Runtime { types_by_name: HashMap::new(), types_by_id: HashMap::new(), }; - let _ = rt.make_type("null".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("int".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("long".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("mega".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("float".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("double".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("func".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("array".to_owned(), |t| Ok(t)); // infallible - let _ = rt.make_type("str".to_owned(), |t| Ok(t)); // infallible + let _ = rt.make_type("null".to_owned(), Ok); // infallible + let _ = rt.make_type("int".to_owned(), Ok); // infallible + let _ = rt.make_type("long".to_owned(), Ok); // infallible + let _ = rt.make_type("mega".to_owned(), Ok); // infallible + let _ = rt.make_type("float".to_owned(), Ok); // infallible + let _ = rt.make_type("double".to_owned(), Ok); // infallible + let _ = rt.make_type("func".to_owned(), Ok); // infallible + let _ = rt.make_type("array".to_owned(), Ok); // infallible + let _ = rt.make_type("str".to_owned(), Ok); // infallible rt } @@ -102,7 +108,7 @@ impl Display for Frame { f.write_str("\nVars: \n")?; for (name, object) in self.variables.lock_ro().iter() { f.write_str(" ")?; - f.write_str(&name)?; + f.write_str(name)?; f.write_str(": ")?; std::fmt::Display::fmt(&object.lock_ro(), f)?; f.write_str("\n")?; @@ -201,6 +207,12 @@ impl Display for Stack { } } +impl Default for Stack { + fn default() -> Self { + Self::new() + } +} + impl Stack { pub fn new() -> Self { let o = Arc::new(Frame::root()); @@ -210,7 +222,7 @@ impl Stack { }; dyn_fns::register(&mut r, o.clone()); - std_fns::register(&mut r, o.clone()); + std_fns::register(&mut r, o); r } @@ -263,7 +275,7 @@ impl Stack { ret_count: 1, origin: frame.clone(), to_call: FuncImpl::NativeDyn(Arc::new(Box::new(move |stack| { - stack.push(tmpframe.get_var(tmpname.clone(), &stack)?); + stack.push(tmpframe.get_var(tmpname.clone(), stack)?); Ok(()) }))), cname: Some("RUNTIME".to_owned()), @@ -271,14 +283,14 @@ impl Stack { ); let tmpname = name.clone(); let tmpframe = frame.clone(); - frame.clone().functions.lock().insert( + frame.functions.lock().insert( "=".to_owned() + &name, Arc::new(Func { ret_count: 0, origin: frame.clone(), to_call: FuncImpl::NativeDyn(Arc::new(Box::new(move |stack| { let v = stack.pop(); - tmpframe.set_var(tmpname.clone(), v, &stack) + tmpframe.set_var(tmpname.clone(), v, stack) }))), cname: Some("RUNTIME".to_owned()), }), @@ -422,6 +434,7 @@ pub struct Words { } #[derive(Clone)] +#[allow(clippy::type_complexity)] pub enum FuncImpl { Native(fn(&mut Stack) -> OError), NativeDyn(Arc OError>>), @@ -574,7 +587,7 @@ impl Display for Object { self.native.fmt(f)?; f.write_str(") { ")?; for (k, v) in &self.property_map { - f.write_str(&k)?; + f.write_str(k)?; f.write_str(": ")?; std::fmt::Display::fmt(&v.lock_ro(), f)?; } @@ -608,7 +621,7 @@ impl Object { Value::Double(_) => true, Value::Func(_) => true, Value::Array(_) => true, - Value::Str(x) => x == "", + Value::Str(x) => x.is_empty(), } } } @@ -655,7 +668,7 @@ impl Words { for word in self.words.clone() { match word { Word::Key(x) => match x { - Keyword::Dump => println!("{}", stack), + Keyword::Dump => println!("{stack}"), Keyword::Def(x) => stack.define_var(x), Keyword::Func(name, rem, words) => stack.define_func( name, @@ -708,12 +721,13 @@ impl Words { RUNTIME.with(move |rt| { let mut rt = rt.borrow_mut(); let rt = rt.as_mut().expect("no runtime (use .set())"); - Ok( rt.get_type_by_name(tb.clone()) - .ok_or_else(|| rstack.error(ErrorKind::TypeNotFound(tb.clone())))? - .lock() - .parents - .push(rt.get_type_by_name(ta).ok_or_else(|| rstack.error(ErrorKind::TypeNotFound(tb)))?)) + .ok_or_else(|| rstack.error(ErrorKind::TypeNotFound(tb.clone())))? + .lock() + .parents + .push(rt.get_type_by_name(ta).ok_or_else(|| rstack.error(ErrorKind::TypeNotFound(tb)))?); + Ok( + ()) })?; } Keyword::While(cond, blk) => loop { diff --git a/src/std_fns.rs b/src/std_fns.rs index a4832b0..314f8c2 100644 --- a/src/std_fns.rs +++ b/src/std_fns.rs @@ -218,7 +218,7 @@ pub fn to_long(stack: &mut Stack) -> OError { Value::Long(match o { Value::Null => type_err!(stack, "null", "long"), Value::Int(x) => x as i64, - Value::Long(x) => x as i64, + Value::Long(x) => x, Value::Mega(x) => x as i64, Value::Float(x) => x as i64, Value::Double(x) => x as i64, @@ -240,7 +240,7 @@ pub fn to_mega(stack: &mut Stack) -> OError { Value::Null => type_err!(stack, "null", "mega"), Value::Int(x) => x as i128, Value::Long(x) => x as i128, - Value::Mega(x) => x as i128, + Value::Mega(x) => x, Value::Float(x) => x as i128, Value::Double(x) => x as i128, Value::Func(_) => type_err!(stack, "func", "mega"), @@ -262,7 +262,7 @@ pub fn to_float(stack: &mut Stack) -> OError { Value::Int(x) => x as f32, Value::Long(x) => x as f32, Value::Mega(x) => x as f32, - Value::Float(x) => x as f32, + Value::Float(x) => x, Value::Double(x) => x as f32, Value::Func(_) => type_err!(stack, "func", "float"), Value::Array(_) => type_err!(stack, "array", "float"), @@ -284,7 +284,7 @@ pub fn to_double(stack: &mut Stack) -> OError { Value::Long(x) => x as f64, Value::Mega(x) => x as f64, Value::Float(x) => x as f64, - Value::Double(x) => x as f64, + Value::Double(x) => x, Value::Func(_) => type_err!(stack, "func", "double"), Value::Array(_) => type_err!(stack, "array", "double"), Value::Str(x) => x @@ -373,7 +373,8 @@ pub fn exit(stack: &mut Stack) -> OError { } pub fn register(r: &mut Stack, o: Arc) { - let fns: [(&str, fn(&mut Stack) -> OError, u32); 28] = [ + type Fn = fn(&mut Stack) -> OError; + let fns: [(&str, Fn, u32); 28] = [ ("pop", pop, 0), ("dup", dup, 2), ("clone", clone, 1),