diff --git a/src/lib.rs b/src/lib.rs index 262904c1cbb..69b612d932d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -197,12 +197,12 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> { } let res = match run_mode { RunMode::Command(command) => { - debug!("Running command {}", command); + debug!("Running command {command}"); vm.run_code_string(scope.clone(), &command, "".to_owned()) .map(drop) } RunMode::Module(module) => { - debug!("Running module {}", module); + debug!("Running module {module}"); vm.run_module(&module) } RunMode::InstallPip(installer) => install_pip(installer, scope.clone(), vm), diff --git a/src/shell.rs b/src/shell.rs index 801a989cf21..4222f962719 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -152,7 +152,8 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> { continuing_line = false; let result = match repl.readline(prompt) { ReadlineResult::Line(line) => { - debug!("You entered {:?}", line); + #[cfg(debug_assertions)] + debug!("You entered {line:?}"); repl.add_history_entry(line.trim_end()).unwrap(); diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index 1c884770350..c7cd3a129a0 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -756,8 +756,7 @@ impl ToPyException for Base64DecodeError { InvalidLastSymbol(_, PAD) => "Excess data after padding".to_owned(), InvalidLastSymbol(length, _) => { format!( - "Invalid base64-encoded string: number of data characters {} cannot be 1 more than a multiple of 4", - length + "Invalid base64-encoded string: number of data characters {length} cannot be 1 more than a multiple of 4" ) } // TODO: clean up errors diff --git a/stdlib/src/csv.rs b/stdlib/src/csv.rs index 730d3b2feb6..a7d333d6213 100644 --- a/stdlib/src/csv.rs +++ b/stdlib/src/csv.rs @@ -346,13 +346,9 @@ mod _csv { if !rest.args.is_empty() { let arg_len = rest.args.len(); if arg_len != 1 { - return Err(vm.new_type_error( - format!( - "field_size_limit() takes at most 1 argument ({} given)", - arg_len - ) - .to_string(), - )); + return Err(vm.new_type_error(format!( + "field_size_limit() takes at most 1 argument ({arg_len} given)" + ))); } let Ok(new_size) = rest.args.first().unwrap().try_int(vm) else { return Err(vm.new_type_error("limit must be an integer".to_string())); @@ -701,7 +697,7 @@ mod _csv { if let Some(dialect) = g.get(name) { Ok(self.update_py_dialect(*dialect)) } else { - Err(new_csv_error(vm, format!("{} is not registered.", name))) + Err(new_csv_error(vm, format!("{name} is not registered."))) } // TODO // Maybe need to update the obj from HashMap diff --git a/vm/src/buffer.rs b/vm/src/buffer.rs index a07048757a2..0bab22c10a8 100644 --- a/vm/src/buffer.rs +++ b/vm/src/buffer.rs @@ -363,7 +363,7 @@ impl FormatSpec { // Loop over all opcodes: for code in &self.codes { buffer = &mut buffer[code.pre_padding..]; - debug!("code: {:?}", code); + debug!("code: {code:?}"); match code.code { FormatType::Str => { let (buf, rest) = buffer.split_at_mut(code.repeat); @@ -407,7 +407,7 @@ impl FormatSpec { let mut items = Vec::with_capacity(self.arg_count); for code in &self.codes { data = &data[code.pre_padding..]; - debug!("unpack code: {:?}", code); + debug!("unpack code: {code:?}"); match code.code { FormatType::Pad => { data = &data[code.repeat..]; diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 02324704b31..9eecb552251 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -66,10 +66,9 @@ impl PyObjectRef { warnings::warn( vm.ctx.exceptions.deprecation_warning, format!( - "__complex__ returned non-complex (type {}). \ + "__complex__ returned non-complex (type {ret_class}). \ The ability to return an instance of a strict subclass of complex \ - is deprecated, and may be removed in a future version of Python.", - ret_class + is deprecated, and may be removed in a future version of Python." ), 1, vm, diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index be143275428..5bf47f9fd3a 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -53,14 +53,12 @@ impl Constructor for PyBaseObject { 0 => {} 1 => { return Err(vm.new_type_error(format!( - "class {} without an implementation for abstract method '{}'", - name, methods + "class {name} without an implementation for abstract method '{methods}'" ))); } 2.. => { return Err(vm.new_type_error(format!( - "class {} without an implementation for abstract methods '{}'", - name, methods + "class {name} without an implementation for abstract methods '{methods}'" ))); } // TODO: remove `allow` when redox build doesn't complain about it diff --git a/vm/src/builtins/property.rs b/vm/src/builtins/property.rs index 5bfae5a081c..1ea0c61d142 100644 --- a/vm/src/builtins/property.rs +++ b/vm/src/builtins/property.rs @@ -132,8 +132,7 @@ impl PyProperty { let func_args_len = func_args.args.len(); let (_owner, name): (PyObjectRef, PyObjectRef) = func_args.bind(vm).map_err(|_e| { vm.new_type_error(format!( - "__set_name__() takes 2 positional arguments but {} were given", - func_args_len + "__set_name__() takes 2 positional arguments but {func_args_len} were given" )) })?; diff --git a/vm/src/builtins/super.rs b/vm/src/builtins/super.rs index 442d162c783..e2c5aa1a39d 100644 --- a/vm/src/builtins/super.rs +++ b/vm/src/builtins/super.rs @@ -1,3 +1,4 @@ +// cspell:ignore cmeth /*! Python `super` class. See also [CPython source code.](https://github.com/python/cpython/blob/50b48572d9a90c5bb36e2bef6179548ea927a35a/Objects/typeobject.c#L7663) @@ -125,8 +126,8 @@ impl Initializer for PySuper { (typ, obj) }; - let mut inner = PySuperInner::new(typ, obj, vm)?; - std::mem::swap(&mut inner, &mut zelf.inner.write()); + let inner = PySuperInner::new(typ, obj, vm)?; + *zelf.inner.write() = inner; Ok(()) } diff --git a/vm/src/codecs.rs b/vm/src/codecs.rs index 8d002916a6f..6d7e009c4a5 100644 --- a/vm/src/codecs.rs +++ b/vm/src/codecs.rs @@ -419,7 +419,7 @@ impl StandardEncoding { match encoding { "be" => Some(Self::Utf32Be), "le" => Some(Self::Utf32Le), - _ => return None, + _ => None, } } else { None @@ -1116,7 +1116,7 @@ fn replace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(PyObjectRe let replace = replacement_char.repeat(range.end - range.start); Ok((replace.to_pyobject(vm), range.end)) } else { - return Err(bad_err_type(err, vm)); + Err(bad_err_type(err, vm)) } } diff --git a/vm/src/eval.rs b/vm/src/eval.rs index 4c48efc7004..be09b3e4cc9 100644 --- a/vm/src/eval.rs +++ b/vm/src/eval.rs @@ -3,7 +3,7 @@ use crate::{PyResult, VirtualMachine, compiler, scope::Scope}; pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult { match vm.compile(source, compiler::Mode::Eval, source_path.to_owned()) { Ok(bytecode) => { - debug!("Code object: {:?}", bytecode); + debug!("Code object: {bytecode:?}"); vm.run_code_obj(bytecode, scope) } Err(err) => Err(vm.new_syntax_error(&err, Some(source))), diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 60b6d867043..42bd8c75da4 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -206,7 +206,7 @@ impl VirtualMachine { lineno )?; } else if let Some(filename) = maybe_filename { - filename_suffix = format!(" ({})", filename); + filename_suffix = format!(" ({filename})"); } if let Some(text) = maybe_text { @@ -215,7 +215,7 @@ impl VirtualMachine { let l_text = r_text.trim_start_matches([' ', '\n', '\x0c']); // \x0c is \f let spaces = (r_text.len() - l_text.len()) as isize; - writeln!(output, " {}", l_text)?; + writeln!(output, " {l_text}")?; let maybe_offset: Option = getattr("offset").and_then(|obj| obj.try_to_value::(vm).ok()); @@ -1615,7 +1615,7 @@ pub(super) mod types { format!("{} ({}, line {})", msg, basename(filename.as_str()), lineno) } (Some(lineno), None) => { - format!("{} (line {})", msg, lineno) + format!("{msg} (line {lineno})") } (None, Some(filename)) => { format!("{} ({})", msg, basename(filename.as_str())) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index dbe5cb077ab..54f740e8836 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -1667,7 +1667,8 @@ impl ExecutingFrame<'_> { .topmost_exception() .ok_or_else(|| vm.new_runtime_error("No active exception to reraise".to_owned()))?, }; - debug!("Exception raised: {:?} with cause: {:?}", exception, cause); + #[cfg(debug_assertions)] + debug!("Exception raised: {exception:?} with cause: {cause:?}"); if let Some(cause) = cause { exception.set_cause(cause); } diff --git a/vm/src/protocol/number.rs b/vm/src/protocol/number.rs index dd039c27335..94c4f397960 100644 --- a/vm/src/protocol/number.rs +++ b/vm/src/protocol/number.rs @@ -51,8 +51,7 @@ impl PyObject { Err(err) => return err, }; vm.new_value_error(format!( - "invalid literal for int() with base {}: {}", - base, repr, + "invalid literal for int() with base {base}: {repr}", )) })?; Ok(PyInt::from(i).into_ref(&vm.ctx)) @@ -475,10 +474,9 @@ impl PyNumber<'_> { warnings::warn( vm.ctx.exceptions.deprecation_warning, format!( - "__int__ returned non-int (type {}). \ + "__int__ returned non-int (type {ret_class}). \ The ability to return an instance of a strict subclass of int \ - is deprecated, and may be removed in a future version of Python.", - ret_class + is deprecated, and may be removed in a future version of Python." ), 1, vm, @@ -509,10 +507,9 @@ impl PyNumber<'_> { warnings::warn( vm.ctx.exceptions.deprecation_warning, format!( - "__index__ returned non-int (type {}). \ + "__index__ returned non-int (type {ret_class}). \ The ability to return an instance of a strict subclass of int \ - is deprecated, and may be removed in a future version of Python.", - ret_class + is deprecated, and may be removed in a future version of Python." ), 1, vm, @@ -543,10 +540,9 @@ impl PyNumber<'_> { warnings::warn( vm.ctx.exceptions.deprecation_warning, format!( - "__float__ returned non-float (type {}). \ + "__float__ returned non-float (type {ret_class}). \ The ability to return an instance of a strict subclass of float \ - is deprecated, and may be removed in a future version of Python.", - ret_class + is deprecated, and may be removed in a future version of Python." ), 1, vm, diff --git a/vm/src/stdlib/ctypes/base.rs b/vm/src/stdlib/ctypes/base.rs index 6cc19be3dfd..8d7fd6849d2 100644 --- a/vm/src/stdlib/ctypes/base.rs +++ b/vm/src/stdlib/ctypes/base.rs @@ -251,7 +251,7 @@ impl PyCSimple { #[pyclassmethod] fn repeat(cls: PyTypeRef, n: isize, vm: &VirtualMachine) -> PyResult { if n < 0 { - return Err(vm.new_value_error(format!("Array length must be >= 0, not {}", n))); + return Err(vm.new_value_error(format!("Array length must be >= 0, not {n}"))); } Ok(PyCArrayType { inner: PyCArray { diff --git a/vm/src/stdlib/ctypes/function.rs b/vm/src/stdlib/ctypes/function.rs index 21043da27d6..dc9ff6051b6 100644 --- a/vm/src/stdlib/ctypes/function.rs +++ b/vm/src/stdlib/ctypes/function.rs @@ -13,6 +13,7 @@ use libffi::middle::{Arg, Cif, CodePtr, Type}; use libloading::Symbol; use num_traits::ToPrimitive; use rustpython_common::lock::PyRwLock; +use std::ffi::CString; use std::fmt::Debug; // https://github.com/python/cpython/blob/4f8bb3947cfbc20f970ff9d9531e1132a9e95396/Modules/_ctypes/callproc.c#L15 @@ -77,10 +78,11 @@ impl Function { } }) .collect::>>()?; - let terminated = format!("{}\0", function); + let c_function_name = CString::new(function) + .map_err(|_| vm.new_value_error("Function name contains null bytes".to_string()))?; let pointer: Symbol<'_, FP> = unsafe { library - .get(terminated.as_bytes()) + .get(c_function_name.as_bytes()) .map_err(|err| err.to_string()) .map_err(|err| vm.new_attribute_error(err))? }; diff --git a/vm/src/stdlib/ctypes/structure.rs b/vm/src/stdlib/ctypes/structure.rs index 10c1fa3df81..0d1b4057038 100644 --- a/vm/src/stdlib/ctypes/structure.rs +++ b/vm/src/stdlib/ctypes/structure.rs @@ -53,7 +53,7 @@ impl GetAttr for PyCStructure { let data = zelf.data.read(); match data.get(&name) { Some(value) => Ok(value.clone()), - None => Err(vm.new_attribute_error(format!("No attribute named {}", name))), + None => Err(vm.new_attribute_error(format!("No attribute named {name}"))), } } } diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index e4339ae61ae..91efb4481b1 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -1175,7 +1175,7 @@ mod _io { vm.call_method(self.raw.as_ref().unwrap(), "readinto", (mem_obj.clone(),)); mem_obj.release(); - std::mem::swap(v, &mut read_buf.take()); + *v = read_buf.take(); res? } diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index d75629745c9..0808c79b4ca 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -1989,7 +1989,7 @@ pub mod module { let pathname = vm.ctx.new_dict(); for variant in PathconfVar::iter() { // get the name of variant as a string to use as the dictionary key - let key = vm.ctx.new_str(format!("{:?}", variant)); + let key = vm.ctx.new_str(format!("{variant:?}")); // get the enum from the string and convert it to an integer for the dictionary value let value = vm.ctx.new_int(variant as u8); pathname @@ -2185,7 +2185,7 @@ pub mod module { let names = vm.ctx.new_dict(); for variant in SysconfVar::iter() { // get the name of variant as a string to use as the dictionary key - let key = vm.ctx.new_str(format!("{:?}", variant)); + let key = vm.ctx.new_str(format!("{variant:?}")); // get the enum from the string and convert it to an integer for the dictionary value let value = vm.ctx.new_int(variant as u8); names diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index 3a66f7f80d9..c66968c7a12 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -317,9 +317,9 @@ mod sys { let mut source = String::new(); handle .read_to_string(&mut source) - .map_err(|e| vm.new_os_error(format!("Error reading from stdin: {}", e)))?; + .map_err(|e| vm.new_os_error(format!("Error reading from stdin: {e}")))?; vm.compile(&source, crate::compiler::Mode::Single, "".to_owned()) - .map_err(|e| vm.new_os_error(format!("Error running stdin: {}", e)))?; + .map_err(|e| vm.new_os_error(format!("Error running stdin: {e}")))?; Ok(()) } @@ -723,7 +723,7 @@ mod sys { vm.state.int_max_str_digits.store(maxdigits); Ok(()) } else { - let error = format!("maxdigits must be 0 or larger than {:?}", threshold); + let error = format!("maxdigits must be 0 or larger than {threshold:?}"); Err(vm.new_value_error(error)) } } diff --git a/vm/src/vm/compile.rs b/vm/src/vm/compile.rs index f5145e4739f..ee813bf7b46 100644 --- a/vm/src/vm/compile.rs +++ b/vm/src/vm/compile.rs @@ -49,7 +49,7 @@ impl VirtualMachine { self.run_code_string(scope, &source, path.to_owned())?; } Err(err) => { - error!("Failed reading file '{}': {}", path, err); + error!("Failed reading file '{path}': {err}"); // TODO: Need to change to ExitCode or Termination std::process::exit(1); }