From bc35a64e5402566de8e8e9b4f01dfda87851429f Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Sat, 1 Jun 2019 03:19:22 +0300 Subject: [PATCH] fix most of clippy warnings --- parser/src/ast.rs | 1 + vm/src/builtins.rs | 11 +++++------ vm/src/dictdatatype.rs | 2 +- vm/src/frame.rs | 3 +-- vm/src/obj/objbytearray.rs | 2 +- vm/src/obj/objcode.rs | 1 + vm/src/obj/objcomplex.rs | 4 ++-- vm/src/obj/objfloat.rs | 2 +- vm/src/obj/objframe.rs | 1 + vm/src/obj/objint.rs | 10 +++++++--- vm/src/obj/objstr.rs | 14 +++++++------- vm/src/obj/objtuple.rs | 2 +- vm/src/pyhash.rs | 6 +++--- vm/src/pyobject.rs | 1 + vm/src/stdlib/ast.rs | 2 +- vm/src/stdlib/itertools.rs | 25 ++++++++++++++----------- vm/src/stdlib/os.rs | 13 ++++++++----- vm/src/stdlib/socket.rs | 4 ++-- vm/src/symboltable.rs | 14 +++++++++----- vm/src/vm.rs | 2 +- 20 files changed, 68 insertions(+), 52 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 1b985db2d..941e04131 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -44,6 +44,7 @@ pub struct Located { pub type LocatedStatement = Located; /// Abstract syntax tree nodes for python statements. +#[allow(clippy::large_enum_variant)] #[derive(Debug, PartialEq)] pub enum Statement { Break, diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index d467201a1..9afcd0e2c 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -612,13 +612,12 @@ impl Printer for std::io::StdoutLock<'_> { } } -pub fn builtin_exit(object: OptionalArg, vm: &VirtualMachine) -> PyResult<()> { - match object { - OptionalArg::Present(object) => match i32::try_from_object(&vm, object.clone()) { +pub fn builtin_exit(exit_code_arg: OptionalArg, vm: &VirtualMachine) -> PyResult<()> { + if let OptionalArg::Present(exit_code_obj) = exit_code_arg { + match i32::try_from_object(&vm, exit_code_obj.clone()) { Ok(code) => std::process::exit(code), - _ => println!("{}", vm.to_str(&object)?.as_str()), - }, - _ => {} + _ => println!("{}", vm.to_str(&exit_code_obj)?.as_str()), + } } std::process::exit(0); } diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 788587266..ca20f2012 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -193,7 +193,7 @@ impl Dict { Box::new( self.entries .iter() - .filter_map(|v| v.as_ref().map_or(None, |v| Some(v.key.clone()))), + .filter_map(|v| v.as_ref().and_then(|v| Some(v.key.clone()))), ) } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index fef36bfec..adc7bc46a 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -173,8 +173,7 @@ impl NameProtocol for Scope { fn store_cell(&self, vm: &VirtualMachine, name: &str, value: PyObjectRef) { self.locals .iter() - .skip(1) - .next() + .nth(1) .expect("no outer scope for non-local") .set_item(name, value, vm) .unwrap(); diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 2c72a3f46..51d608b09 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -340,7 +340,7 @@ impl PyByteArrayRef { let pos = bytes .iter() .position(|b| *b == x) - .ok_or(vm.new_value_error("value not found in bytearray".to_string()))?; + .ok_or_else(|| vm.new_value_error("value not found in bytearray".to_string()))?; bytes.remove(pos); diff --git a/vm/src/obj/objcode.rs b/vm/src/obj/objcode.rs index 60549cd3a..51a73d614 100644 --- a/vm/src/obj/objcode.rs +++ b/vm/src/obj/objcode.rs @@ -34,6 +34,7 @@ impl PyValue for PyCode { } impl PyCodeRef { + #[allow(clippy::new_ret_no_self)] fn new(_cls: PyClassRef, vm: &VirtualMachine) -> PyResult { Err(vm.new_type_error("Cannot directly create code object".to_string())) } diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index a349354a2..7a6df8705 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -126,12 +126,12 @@ impl PyComplex { #[pymethod(name = "__float__")] fn float(&self, vm: &VirtualMachine) -> PyResult { - return Err(vm.new_type_error(String::from("Can't convert complex to float"))); + Err(vm.new_type_error(String::from("Can't convert complex to float"))) } #[pymethod(name = "__int__")] fn int(&self, vm: &VirtualMachine) -> PyResult { - return Err(vm.new_type_error(String::from("Can't convert complex to int"))); + Err(vm.new_type_error(String::from("Can't convert complex to int"))) } #[pymethod(name = "__mul__")] diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index e300cf2ac..2d6287d7b 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -541,7 +541,7 @@ pub fn ufrexp(value: f64) -> (f64, i32) { } else { let bits = value.to_bits(); let exponent: i32 = ((bits >> 52) & 0x7ff) as i32 - 1022; - let mantissa_bits = bits & (0x000fffffffffffff) | (1022 << 52); + let mantissa_bits = bits & (0x000f_ffff_ffff_ffff) | (1022 << 52); (f64::from_bits(mantissa_bits), exponent) } } diff --git a/vm/src/obj/objframe.rs b/vm/src/obj/objframe.rs index b4ed15f74..6669e3bb9 100644 --- a/vm/src/obj/objframe.rs +++ b/vm/src/obj/objframe.rs @@ -18,6 +18,7 @@ pub fn init(context: &PyContext) { } impl FrameRef { + #[allow(clippy::new_ret_no_self)] fn new(_class: FrameRef, vm: &VirtualMachine) -> PyResult { Err(vm.new_type_error("Cannot directly create frame object".to_string())) } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index f8af3553f..2b703d30e 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -112,8 +112,9 @@ impl_try_from_object_int!( (u64, to_u64), ); +#[allow(clippy::collapsible_if)] fn inner_pow(int1: &PyInt, int2: &PyInt, vm: &VirtualMachine) -> PyResult { - Ok(if int2.value.is_negative() { + let result = if int2.value.is_negative() { let v1 = int1.float(vm)?; let v2 = int2.float(vm)?; vm.ctx.new_float(v1.pow(v2)) @@ -133,7 +134,8 @@ fn inner_pow(int1: &PyInt, int2: &PyInt, vm: &VirtualMachine) -> PyResult { // practically, exp over u64 is not possible to calculate anyway vm.ctx.not_implemented() } - }) + }; + Ok(result) } #[pyimpl] @@ -494,6 +496,7 @@ impl PyInt { } #[pymethod] + #[allow(clippy::match_bool)] fn from_bytes( bytes: PyByteInner, byteorder: PyStringRef, @@ -529,6 +532,7 @@ impl PyInt { Ok(x) } #[pymethod] + #[allow(clippy::match_bool)] fn to_bytes( &self, length: PyIntRef, @@ -547,7 +551,7 @@ impl PyInt { ); } } - if value.sign() == Sign::Minus && signed == false { + if value.sign() == Sign::Minus && !signed { return Err(vm.new_overflow_error("can't convert negative int to unsigned".to_string())); } let byte_len; diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index b0d19e9c3..40d191296 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -863,7 +863,7 @@ impl PyString { None => false, }; // a string is not an identifier if it has whitespace or starts with a number - is_identifier_start && chars.all(|c| UnicodeXID::is_xid_continue(c)) + is_identifier_start && chars.all(UnicodeXID::is_xid_continue) } // https://docs.python.org/3/library/stdtypes.html#str.translate @@ -876,18 +876,18 @@ impl PyString { match table.get_item(c as u32, vm) { Ok(value) => { if let Some(text) = value.payload::() { - translated.extend(text.value.chars()); - } else if let Some(_) = value.payload::() { - // Do Nothing + translated.push_str(&text.value); } else if let Some(bigint) = value.payload::() { match bigint.as_bigint().to_u32().and_then(std::char::from_u32) { Some(ch) => translated.push(ch as char), None => { - return Err(vm.new_value_error(format!( - "character mapping must be in range(0x110000)" - ))); + return Err(vm.new_value_error( + "character mapping must be in range(0x110000)".to_owned(), + )); } } + } else if let Some(_) = value.payload::() { + // Do Nothing } else { return Err(vm.new_type_error( "character mapping must return integer, None or str".to_owned(), diff --git a/vm/src/obj/objtuple.rs b/vm/src/obj/objtuple.rs index 03596fbd2..40a5749ee 100644 --- a/vm/src/obj/objtuple.rs +++ b/vm/src/obj/objtuple.rs @@ -27,7 +27,7 @@ impl fmt::Debug for PyTuple { impl From> for PyTuple { fn from(elements: Vec) -> Self { - PyTuple { elements: elements } + PyTuple { elements } } } diff --git a/vm/src/pyhash.rs b/vm/src/pyhash.rs index e717daa39..8bbd82168 100644 --- a/vm/src/pyhash.rs +++ b/vm/src/pyhash.rs @@ -9,11 +9,11 @@ pub type PyHash = i64; pub type PyUHash = u64; /// Prime multiplier used in string and various other hashes. -pub const MULTIPLIER: PyHash = 1000003; // 0xf4243 +pub const MULTIPLIER: PyHash = 1_000_003; // 0xf4243 /// Numeric hashes are based on reduction modulo the prime 2**_BITS - 1 pub const BITS: usize = 61; pub const MODULUS: PyUHash = (1 << BITS) - 1; -pub const INF: PyHash = 314159; +pub const INF: PyHash = 314_159; pub const NAN: PyHash = 0; pub const IMAG: PyHash = MULTIPLIER; @@ -42,7 +42,7 @@ pub fn hash_float(value: f64) -> PyHash { let mut x: PyUHash = 0; while m != 0.0 { x = ((x << 28) & MODULUS) | x >> (BITS - 28); - m *= 268435456.0; // 2**28 + m *= 268_435_456.0; // 2**28 e -= 28; let y = m as PyUHash; // pull out integer part m -= y as f64; diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index d88991b9e..70c8a84b4 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1238,6 +1238,7 @@ impl PyObject where T: Sized + PyObjectPayload, { + #[allow(clippy::new_ret_no_self)] pub fn new(payload: T, typ: PyClassRef, dict: Option) -> PyObjectRef { PyObject { typ, dict, payload }.into_ref() } diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 2ac36c81e..2b79dd706 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -457,7 +457,7 @@ fn parameter_to_ast(vm: &VirtualMachine, parameter: &ast::Parameter) -> PyResult fn map_ast( f: fn(vm: &VirtualMachine, &T) -> PyResult, vm: &VirtualMachine, - items: &Vec, + items: &[T], ) -> PyResult { let list: PyResult> = items.iter().map(|x| Ok(f(vm, x)?.into_object())).collect(); diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index a8c333701..732020480 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -31,6 +31,7 @@ impl PyValue for PyItertoolsChain { #[pyimpl] impl PyItertoolsChain { #[pymethod(name = "__new__")] + #[allow(clippy::new_ret_no_self)] fn new(_cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult { Ok(PyItertoolsChain { iterables: args.args, @@ -88,6 +89,7 @@ impl PyValue for PyItertoolsCount { #[pyimpl] impl PyItertoolsCount { #[pymethod(name = "__new__")] + #[allow(clippy::new_ret_no_self)] fn new( _cls: PyClassRef, start: OptionalArg, @@ -105,7 +107,7 @@ impl PyItertoolsCount { Ok(PyItertoolsCount { cur: RefCell::new(start), - step: step, + step, } .into_ref(vm) .into_object()) @@ -140,6 +142,7 @@ impl PyValue for PyItertoolsRepeat { #[pyimpl] impl PyItertoolsRepeat { #[pymethod(name = "__new__")] + #[allow(clippy::new_ret_no_self)] fn new( _cls: PyClassRef, object: PyObjectRef, @@ -153,7 +156,7 @@ impl PyItertoolsRepeat { Ok(PyItertoolsRepeat { object: object.clone(), - times: times, + times, } .into_ref(vm) .into_object()) @@ -198,6 +201,7 @@ impl PyValue for PyItertoolsStarmap { #[pyimpl] impl PyItertoolsStarmap { #[pymethod(name = "__new__")] + #[allow(clippy::new_ret_no_self)] fn new( _cls: PyClassRef, function: PyObjectRef, @@ -206,12 +210,9 @@ impl PyItertoolsStarmap { ) -> PyResult { let iter = get_iter(vm, &iterable)?; - Ok(PyItertoolsStarmap { - function: function, - iter: iter, - } - .into_ref(vm) - .into_object()) + Ok(PyItertoolsStarmap { function, iter } + .into_ref(vm) + .into_object()) } #[pymethod(name = "__next__")] @@ -244,6 +245,7 @@ impl PyValue for PyItertoolsTakewhile { #[pyimpl] impl PyItertoolsTakewhile { #[pymethod(name = "__new__")] + #[allow(clippy::new_ret_no_self)] fn new( _cls: PyClassRef, predicate: PyObjectRef, @@ -253,7 +255,7 @@ impl PyItertoolsTakewhile { let iter = get_iter(vm, &iterable)?; Ok(PyItertoolsTakewhile { - predicate: predicate, + predicate, iterable: iter, stop_flag: RefCell::new(false), } @@ -314,6 +316,7 @@ fn pyobject_to_opt_usize(obj: PyObjectRef, vm: &VirtualMachine) -> Option #[pyimpl] impl PyItertoolsIslice { #[pymethod(name = "__new__")] + #[allow(clippy::new_ret_no_self)] fn new(_cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult { let (iter, start, stop, step) = match args.args.len() { 0 | 1 => { @@ -377,8 +380,8 @@ impl PyItertoolsIslice { iterable: iter, cur: RefCell::new(0), next: RefCell::new(start), - stop: stop, - step: step, + stop, + step, } .into_ref(vm) .into_object()) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index f5a6ced96..0893c1e6d 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -259,6 +259,7 @@ impl DirEntryRef { self.entry.path().to_str().unwrap().to_string() } + #[allow(clippy::match_bool)] fn perform_on_metadata( self, follow_symlinks: FollowSymlinks, @@ -434,6 +435,7 @@ fn to_seconds_from_nanos(secs: i64, nanos: i64) -> f64 { #[cfg(unix)] macro_rules! os_unix_stat_inner { ( $path:expr, $follow_symlinks:expr, $vm:expr ) => {{ + #[allow(clippy::match_bool)] fn get_stats(path: &str, follow_symlinks: bool) -> io::Result { let meta = match follow_symlinks { true => fs::metadata(path)?, @@ -685,12 +687,13 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { where F: IntoPyNativeFunc, { + let func_obj = vm.ctx.new_rustfunc(func); Self { - name: name, - func_obj: vm.ctx.new_rustfunc(func), - fd: fd, - dir_fd: dir_fd, - follow_symlinks: follow_symlinks, + name, + func_obj, + fd, + dir_fd, + follow_symlinks, } } } diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 4f7cab5a1..68934bd01 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -218,7 +218,7 @@ impl SocketRef { } } - fn listen(self, _num: PyIntRef, _vm: &VirtualMachine) -> () {} + fn listen(self, _num: PyIntRef, _vm: &VirtualMachine) {} fn accept(self, vm: &VirtualMachine) -> PyResult { let ret = match self.con.borrow_mut().as_mut() { @@ -310,7 +310,7 @@ impl SocketRef { } } - fn close(self, _vm: &VirtualMachine) -> () { + fn close(self, _vm: &VirtualMachine) { self.con.borrow_mut().take(); } diff --git a/vm/src/symboltable.rs b/vm/src/symboltable.rs index 24407c751..9687b0ec7 100644 --- a/vm/src/symboltable.rs +++ b/vm/src/symboltable.rs @@ -115,6 +115,7 @@ fn analyze_symbol_table( Ok(()) } +#[allow(clippy::single_match)] fn analyze_symbol( symbol_name: &str, symbol_role: &SymbolRole, @@ -298,13 +299,15 @@ impl SymbolTableBuilder { ast::Statement::Import { import_parts } => { for part in import_parts { if let Some(alias) = &part.alias { + // `import mymodule as myalias` + // `from mymodule import myimportname as myalias` self.register_name(alias, SymbolRole::Assigned)?; + } else if let Some(symbol) = &part.symbol { + // `from mymodule import myimport` + self.register_name(symbol, SymbolRole::Assigned)?; } else { - if let Some(symbol) = &part.symbol { - self.register_name(symbol, SymbolRole::Assigned)?; - } else { - self.register_name(&part.module, SymbolRole::Assigned)?; - } + // `import module` + self.register_name(&part.module, SymbolRole::Assigned)?; } } } @@ -540,6 +543,7 @@ impl SymbolTableBuilder { Ok(()) } + #[allow(clippy::single_match)] fn register_name(&mut self, name: &str, role: SymbolRole) -> SymbolTableResult { let scope_depth = self.scopes.len(); let current_scope = self.scopes.last_mut().unwrap(); diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 7c2e80462..dfe89e640 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -951,7 +951,7 @@ impl VirtualMachine { } } - pub fn push_exception(&self, exc: PyObjectRef) -> () { + pub fn push_exception(&self, exc: PyObjectRef) { self.exceptions.borrow_mut().push(exc) }