diff --git a/src/main.rs b/src/main.rs index e6d5e5684d..075fe966a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,7 +195,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { debug!("You entered {:?}", input); if shell_exec(vm, &input, vars.clone()) { // Line was complete. - rl.add_history_entry(input.trim_end().as_ref()); + rl.add_history_entry(input.trim_end()); input = String::new(); } else { loop { @@ -206,9 +206,9 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { //}; match rl.readline(&ps2) { Ok(line) => { - if line.len() == 0 { + if line.is_empty() { if shell_exec(vm, &input, vars.clone()) { - rl.add_history_entry(input.trim_end().as_ref()); + rl.add_history_entry(input.trim_end()); input = String::new(); break; } diff --git a/vm/src/format.rs b/vm/src/format.rs index 6dfa77c7e3..e89e4f731d 100644 --- a/vm/src/format.rs +++ b/vm/src/format.rs @@ -496,8 +496,12 @@ impl FormatString { None => Err(FormatParseError::UnmatchedBracket), } } +} + +impl FromStr for FormatString { + type Err = FormatParseError; - pub fn from_str(text: &str) -> Result { + fn from_str(text: &str) -> Result { let mut cur_text: &str = text; let mut parts: Vec = Vec::new(); while !cur_text.is_empty() { diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 7d77280985..cf971a38f2 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -197,7 +197,7 @@ impl Frame { } bytecode::Instruction::Rotate { amount } => { // Shuffles top of stack amount down - if amount < &2 { + if *amount < 2 { panic!("Can only rotate two or more values"); } diff --git a/vm/src/import.rs b/vm/src/import.rs index d7393bd674..49fe08d944 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -77,7 +77,7 @@ pub fn import( let import_error = vm.context().exceptions.import_error.clone(); Err(vm.new_exception(import_error, format!("cannot import name '{}'", symbol))) }, - |obj| Ok(obj), + Ok, ) } else { Ok(module) diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index b4afaa6398..24698277ea 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -214,12 +214,10 @@ fn bytearray_istitle(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let current = char::from(*c); let next = if let Some(k) = iter.peek() { char::from(**k) + } else if current.is_uppercase() { + return Ok(vm.new_bool(!prev_cased)); } else { - if current.is_uppercase() { - return Ok(vm.new_bool(!prev_cased)); - } else { - return Ok(vm.new_bool(prev_cased)); - } + return Ok(vm.new_bool(prev_cased)); }; if (is_cased(current) && next.is_uppercase() && !prev_cased) diff --git a/vm/src/obj/objcode.rs b/vm/src/obj/objcode.rs index 2649c11903..1998bfab90 100644 --- a/vm/src/obj/objcode.rs +++ b/vm/src/obj/objcode.rs @@ -14,7 +14,7 @@ pub fn init(context: &PyContext) { context.set_attr(code_type, "__new__", context.new_rustfunc(code_new)); context.set_attr(code_type, "__repr__", context.new_rustfunc(code_repr)); - for (name, f) in vec![ + for (name, f) in &[ ( "co_argcount", code_co_argcount as fn(&mut VirtualMachine, PyFuncArgs) -> PyResult, diff --git a/vm/src/obj/objmap.rs b/vm/src/obj/objmap.rs index 79ee7ddf8a..bde61e3d67 100644 --- a/vm/src/obj/objmap.rs +++ b/vm/src/obj/objmap.rs @@ -14,7 +14,7 @@ fn map_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let function = &args.args[1]; let iterables = &args.args[2..]; let iterators = iterables - .into_iter() + .iter() .map(|iterable| objiter::get_iter(vm, iterable)) .collect::, _>>()?; Ok(PyObject::new( diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index b9801cb304..c95407f62f 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -42,8 +42,8 @@ impl RangeType { #[inline] fn offset(&self, value: &BigInt) -> Option { match self.step.sign() { - Sign::Plus if value >= &self.start && value < &self.end => Some(value - &self.start), - Sign::Minus if value <= &self.start && value > &self.end => Some(&self.start - value), + Sign::Plus if *value >= self.start && *value < self.end => Some(value - &self.start), + Sign::Minus if *value <= self.start && *value > self.end => Some(&self.start - value), _ => None, } } @@ -68,9 +68,10 @@ impl RangeType { #[inline] pub fn count(&self, value: &BigInt) -> usize { - match self.index_of(value).is_some() { - true => 1, - false => 0, + if self.index_of(value).is_some() { + 1 + } else { + 0 } } @@ -145,7 +146,7 @@ pub fn get_value(obj: &PyObjectRef) -> RangeType { } pub fn init(context: &PyContext) { - let ref range_type = context.range_type; + let range_type = &context.range_type; let range_doc = "range(stop) -> range object\n\ range(start, stop[, step]) -> range object\n\n\ @@ -198,7 +199,7 @@ fn range_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { ] ); - let start = if let Some(_) = second { + let start = if second.is_some() { objint::get_value(first) } else { BigInt::zero() diff --git a/vm/src/obj/objsequence.rs b/vm/src/obj/objsequence.rs index 31c1b7b71a..3f6f67bf61 100644 --- a/vm/src/obj/objsequence.rs +++ b/vm/src/obj/objsequence.rs @@ -15,6 +15,7 @@ pub trait PySliceableSequence { fn do_stepped_slice_reverse(&self, range: Range, step: usize) -> Self; fn empty() -> Self; fn len(&self) -> usize; + fn is_empty(&self) -> bool; fn get_pos(&self, p: i32) -> Option { if p < 0 { if -p as usize > self.len() { @@ -127,6 +128,10 @@ impl PySliceableSequence for Vec { fn len(&self) -> usize { self.len() } + + fn is_empty(&self) -> bool { + self.is_empty() + } } pub fn get_item( @@ -288,7 +293,7 @@ pub fn seq_mul(elements: &[PyObjectRef], product: &PyObjectRef) -> Vec PyResult { } fn set_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner( + set_compare_inner( vm, args, &|zelf: usize, other: usize| -> bool { zelf != other }, false, - ); + ) } fn set_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner( + set_compare_inner( vm, args, &|zelf: usize, other: usize| -> bool { zelf < other }, false, - ); + ) } fn set_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner( + set_compare_inner( vm, args, &|zelf: usize, other: usize| -> bool { zelf <= other }, false, - ); + ) } fn set_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner( + set_compare_inner( vm, args, &|zelf: usize, other: usize| -> bool { zelf < other }, true, - ); + ) } fn set_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - return set_compare_inner( + set_compare_inner( vm, args, &|zelf: usize, other: usize| -> bool { zelf <= other }, true, - ); + ) } fn set_compare_inner( diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index f71df5c25f..f533378f32 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -9,6 +9,7 @@ use super::objtype; use num_traits::ToPrimitive; use std::hash::{Hash, Hasher}; use std::ops::Range; +use std::str::FromStr; // rust's builtin to_lowercase isn't sufficient for casefold extern crate caseless; extern crate unicode_segmentation; @@ -1069,6 +1070,10 @@ impl PySliceableSequence for String { fn len(&self) -> usize { to_graphemes(self).len() } + + fn is_empty(&self) -> bool { + self.is_empty() + } } /// Convert a string-able `value` to a vec of graphemes diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 163821bd09..44202e9cc0 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -514,7 +514,7 @@ impl PyContext { // Initialized empty, as calling __hash__ is required for adding each object to the set // which requires a VM context - this is done in the objset code itself. let elements: HashMap = HashMap::new(); - PyObject::new(PyObjectPayload::Set { elements: elements }, self.set_type()) + PyObject::new(PyObjectPayload::Set { elements }, self.set_type()) } pub fn new_dict(&self) -> PyObjectRef { diff --git a/vm/src/stdlib/json.rs b/vm/src/stdlib/json.rs index 581646a03d..9941ac950e 100644 --- a/vm/src/stdlib/json.rs +++ b/vm/src/stdlib/json.rs @@ -189,10 +189,7 @@ fn json_dumps(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { // TODO: Implement non-trivial serialisation case arg_check!(vm, args, required = [(obj, None)]); let res = { - let serializer = PyObjectSerializer { - pyobject: obj, - vm: vm, - }; + let serializer = PyObjectSerializer { pyobject: obj, vm }; serde_json::to_string(&serializer) }; let string = res.map_err(|err| vm.new_type_error(format!("{}", err)))?; @@ -204,7 +201,7 @@ fn json_loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { // TODO: Implement non-trivial deserialisation case arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]); let res = { - let de = PyObjectDeserializer { vm: vm }; + let de = PyObjectDeserializer { vm }; // TODO: Support deserializing string sub-classes de.deserialize(&mut serde_json::Deserializer::from_str(&objstr::get_value( &string, diff --git a/wasm/lib/src/lib.rs b/wasm/lib/src/lib.rs index 7ad3f19560..ff7fe1f531 100644 --- a/wasm/lib/src/lib.rs +++ b/wasm/lib/src/lib.rs @@ -145,7 +145,7 @@ fn eval(vm: &mut VirtualMachine, source: &str, vars: PyObjectRef) -> PyResult { /// - `stdout?`: `(out: string) => void`: A function to replace the native print /// function, by default `console.log`. pub fn eval_py(source: &str, options: Option) -> Result { - let options = options.unwrap_or_else(|| Object::new()); + let options = options.unwrap_or_else(Object::new); let js_vars = { let prop = Reflect::get(&options, &"vars".into())?; if prop.is_undefined() { @@ -199,7 +199,7 @@ pub fn eval_py(source: &str, options: Option) -> Result) -> Result