From 8d36fe68e7527d917db4b175b1086c0b840b1240 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 7 Jan 2023 20:14:24 +0900 Subject: [PATCH] Fix nightly clippy warnings --- common/src/boxvec.rs | 4 ++-- common/src/bytes.rs | 2 +- common/src/float_ops.rs | 20 ++++++++++---------- common/src/format.rs | 16 +++++++--------- common/src/lock/cell_lock.rs | 4 ++-- common/src/str.rs | 6 +++--- compiler/ast/src/constant.rs | 6 +++--- compiler/ast/src/unparse.rs | 2 +- derive-impl/src/compile_bytecode.rs | 14 +++++++------- derive-impl/src/pyclass.rs | 17 +++++++++-------- derive-impl/src/pymodule.rs | 8 ++++---- derive-impl/src/util.rs | 11 ++++++----- pylib/build.rs | 7 +++---- vm/src/py_serde.rs | 2 +- 14 files changed, 59 insertions(+), 60 deletions(-) diff --git a/common/src/boxvec.rs b/common/src/boxvec.rs index 417013014..ca81b7510 100644 --- a/common/src/boxvec.rs +++ b/common/src/boxvec.rs @@ -706,12 +706,12 @@ impl std::error::Error for CapacityError {} impl fmt::Display for CapacityError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", CAPERROR) + write!(f, "{CAPERROR}") } } impl fmt::Debug for CapacityError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "capacity error: {}", CAPERROR) + write!(f, "capacity error: {CAPERROR}") } } diff --git a/common/src/bytes.rs b/common/src/bytes.rs index f5229b6e2..a2cf28901 100644 --- a/common/src/bytes.rs +++ b/common/src/bytes.rs @@ -51,7 +51,7 @@ pub fn repr_with(b: &[u8], prefixes: &[&str], suffix: &str) -> String { } res.push(ch); } - _ => write!(res, "\\x{:02x}", ch).unwrap(), + _ => write!(res, "\\x{ch:02x}").unwrap(), } } res.push(quote); diff --git a/common/src/float_ops.rs b/common/src/float_ops.rs index 1d81b820b..a8595aba9 100644 --- a/common/src/float_ops.rs +++ b/common/src/float_ops.rs @@ -126,7 +126,7 @@ fn format_inf(case: Case) -> String { pub fn format_fixed(precision: usize, magnitude: f64, case: Case) -> String { match magnitude { - magnitude if magnitude.is_finite() => format!("{:.*}", precision, magnitude), + magnitude if magnitude.is_finite() => format!("{magnitude:.precision$}"), magnitude if magnitude.is_nan() => format_nan(case), magnitude if magnitude.is_infinite() => format_inf(case), _ => "".to_string(), @@ -138,7 +138,7 @@ pub fn format_fixed(precision: usize, magnitude: f64, case: Case) -> String { pub fn format_exponent(precision: usize, magnitude: f64, case: Case) -> String { match magnitude { magnitude if magnitude.is_finite() => { - let r_exp = format!("{:.*e}", precision, magnitude); + let r_exp = format!("{magnitude:.precision$e}"); let mut parts = r_exp.splitn(2, 'e'); let base = parts.next().unwrap(); let exponent = parts.next().unwrap().parse::().unwrap(); @@ -146,7 +146,7 @@ pub fn format_exponent(precision: usize, magnitude: f64, case: Case) -> String { Case::Lower => 'e', Case::Upper => 'E', }; - format!("{}{}{:+#03}", base, e, exponent) + format!("{base}{e}{exponent:+#03}") } magnitude if magnitude.is_nan() => format_nan(case), magnitude if magnitude.is_infinite() => format_inf(case), @@ -206,12 +206,12 @@ pub fn format_general( format!("{:.*}", precision + 1, base), alternate_form, ); - format!("{}{}{:+#03}", base, e, exponent) + format!("{base}{e}{exponent:+#03}") } else { let precision = (precision as i64) - 1 - exponent; let precision = precision as usize; maybe_remove_trailing_redundant_chars( - format!("{:.*}", precision, magnitude), + format!("{magnitude:.precision$}"), alternate_form, ) } @@ -223,19 +223,19 @@ pub fn format_general( } pub fn to_string(value: f64) -> String { - let lit = format!("{:e}", value); + let lit = format!("{value:e}"); if let Some(position) = lit.find('e') { let significand = &lit[..position]; let exponent = &lit[position + 1..]; let exponent = exponent.parse::().unwrap(); if exponent < 16 && exponent > -5 { if is_integer(value) { - format!("{:.1?}", value) + format!("{value:.1?}") } else { value.to_string() } } else { - format!("{}e{:+#03}", significand, exponent) + format!("{significand}e{exponent:+#03}") } } else { let mut s = value.to_string(); @@ -296,8 +296,8 @@ pub fn to_hex(value: f64) -> String { let (mantissa, exponent, sign) = value.integer_decode(); let sign_fmt = if sign < 0 { "-" } else { "" }; match value { - value if value.is_zero() => format!("{}0x0.0p+0", sign_fmt), - value if value.is_infinite() => format!("{}inf", sign_fmt), + value if value.is_zero() => format!("{sign_fmt}0x0.0p+0"), + value if value.is_infinite() => format!("{sign_fmt}inf"), value if value.is_nan() => "nan".to_owned(), _ => { const BITS: i16 = 52; diff --git a/common/src/format.rs b/common/src/format.rs index 901566741..e6fcf428e 100644 --- a/common/src/format.rs +++ b/common/src/format.rs @@ -323,7 +323,7 @@ impl FormatSpec { let int_digit_cnt = disp_digit_cnt - dec_digit_cnt; let mut result = FormatSpec::separate_integer(magnitude_int_str, inter, sep, int_digit_cnt); if let Some(part) = parts.next() { - result.push_str(&format!(".{}", part)) + result.push_str(&format!(".{part}")) } result } @@ -342,7 +342,7 @@ impl FormatSpec { // separate with 0 padding let sep_cnt = disp_digit_cnt / (inter + 1); let padding = "0".repeat((pad_cnt - sep_cnt) as usize); - let padded_num = format!("{}{}", padding, magnitude_str); + let padded_num = format!("{padding}{magnitude_str}"); FormatSpec::insert_separator(padded_num, inter, sep, sep_cnt) } else { // separate without padding @@ -373,14 +373,14 @@ impl FormatSpec { | FormatType::Number, ) => { let ch = char::from(format_type); - Err(format!("Cannot specify ',' with '{}'.", ch)) + Err(format!("Cannot specify ',' with '{ch}'.")) } ( Some(FormatGrouping::Underscore), FormatType::String | FormatType::Character | FormatType::Number, ) => { let ch = char::from(format_type); - Err(format!("Cannot specify '_' with '{}'.", ch)) + Err(format!("Cannot specify '_' with '{ch}'.")) } _ => Ok(()), } @@ -446,8 +446,7 @@ impl FormatSpec { | Some(FormatType::Character) => { let ch = char::from(self.format_type.as_ref().unwrap()); Err(format!( - "Unknown format code '{}' for object of type 'float'", - ch + "Unknown format code '{ch}' for object of type 'float'", )) } Some(FormatType::Number) => { @@ -597,7 +596,7 @@ impl FormatSpec { FormatSign::MinusOrSpace => " ", }, }; - let sign_prefix = format!("{}{}", sign_str, prefix); + let sign_prefix = format!("{sign_str}{prefix}"); let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, &sign_prefix); self.format_sign_and_align( unsafe { &BorrowedStr::from_ascii_unchecked(magnitude_str.as_bytes()) }, @@ -620,8 +619,7 @@ impl FormatSpec { _ => { let ch = char::from(self.format_type.as_ref().unwrap()); Err(format!( - "Unknown format code '{}' for object of type 'str'", - ch + "Unknown format code '{ch}' for object of type 'str'", )) } } diff --git a/common/src/lock/cell_lock.rs b/common/src/lock/cell_lock.rs index e40c854c5..5c31fcdbb 100644 --- a/common/src/lock/cell_lock.rs +++ b/common/src/lock/cell_lock.rs @@ -196,8 +196,8 @@ unsafe impl RawRwLockRecursive for RawCellRwLock { #[cold] #[inline(never)] -fn deadlock(lockkind: &str, ty: &str) -> ! { - panic!("deadlock: tried to {}lock a Cell{} twice", lockkind, ty) +fn deadlock(lock_kind: &str, ty: &str) -> ! { + panic!("deadlock: tried to {lock_kind}lock a Cell{ty} twice") } pub struct SingleThreadId(()); diff --git a/common/src/str.rs b/common/src/str.rs index e903d0996..9ce22955b 100644 --- a/common/src/str.rs +++ b/common/src/str.rs @@ -192,11 +192,11 @@ pub fn to_ascii(value: &str) -> AsciiString { } else { let c = c as i64; let hex = if c < 0x100 { - format!("\\x{:02x}", c) + format!("\\x{c:02x}") } else if c < 0x10000 { - format!("\\u{:04x}", c) + format!("\\u{c:04x}") } else { - format!("\\U{:08x}", c) + format!("\\U{c:08x}") }; ascii.append(&mut hex.into_bytes()); } diff --git a/compiler/ast/src/constant.rs b/compiler/ast/src/constant.rs index 373f5f97f..adec47a12 100644 --- a/compiler/ast/src/constant.rs +++ b/compiler/ast/src/constant.rs @@ -46,7 +46,7 @@ impl std::fmt::Display for Constant { Constant::Int(i) => i.fmt(f), Constant::Tuple(tup) => { if let [elt] = &**tup { - write!(f, "({},)", elt) + write!(f, "({elt},)") } else { f.write_str("(")?; for (i, elt) in tup.iter().enumerate() { @@ -61,9 +61,9 @@ impl std::fmt::Display for Constant { Constant::Float(fp) => f.pad(&rustpython_common::float_ops::to_string(*fp)), Constant::Complex { real, imag } => { if *real == 0.0 { - write!(f, "{}j", imag) + write!(f, "{imag}j") } else { - write!(f, "({}{:+}j)", real, imag) + write!(f, "({real}{imag:+}j)") } } Constant::Ellipsis => f.pad("..."), diff --git a/compiler/ast/src/unparse.rs b/compiler/ast/src/unparse.rs index 70addae99..551283c0b 100644 --- a/compiler/ast/src/unparse.rs +++ b/compiler/ast/src/unparse.rs @@ -400,7 +400,7 @@ impl<'a> Unparser<'a> { .checked_sub(defaults_start) .and_then(|i| args.kw_defaults.get(i)) { - write!(self, "={}", default)?; + write!(self, "={default}")?; } } if let Some(kwarg) = &args.kwarg { diff --git a/derive-impl/src/compile_bytecode.rs b/derive-impl/src/compile_bytecode.rs index 6f5d3caca..e72ad2bf9 100644 --- a/derive-impl/src/compile_bytecode.rs +++ b/derive-impl/src/compile_bytecode.rs @@ -109,7 +109,7 @@ impl CompilationSource { let source = fs::read_to_string(&path).map_err(|err| { Diagnostic::spans_error( self.span, - format!("Error reading file {:?}: {}", path, err), + format!("Error reading file {path:?}: {err}"), ) })?; self.compile_string(&source, mode, module_name, compiler, || rel_path.display()) @@ -143,15 +143,15 @@ impl CompilationSource { Err(e) }) .map_err(|err| { - Diagnostic::spans_error(self.span, format!("Error listing dir {:?}: {}", path, err)) + Diagnostic::spans_error(self.span, format!("Error listing dir {path:?}: {err}")) })?; for path in paths { let path = path.map_err(|err| { - Diagnostic::spans_error(self.span, format!("Failed to list file: {}", err)) + Diagnostic::spans_error(self.span, format!("Failed to list file: {err}")) })?; let path = path.path(); let file_name = path.file_name().unwrap().to_str().ok_or_else(|| { - Diagnostic::spans_error(self.span, format!("Invalid UTF-8 in file name {:?}", path)) + Diagnostic::spans_error(self.span, format!("Invalid UTF-8 in file name {path:?}")) })?; if path.is_dir() { code_map.extend(self.compile_dir( @@ -159,7 +159,7 @@ impl CompilationSource { if parent.is_empty() { file_name.to_string() } else { - format!("{}.{}", parent, file_name) + format!("{parent}.{file_name}") }, mode, compiler, @@ -172,14 +172,14 @@ impl CompilationSource { } else if parent.is_empty() { stem.to_owned() } else { - format!("{}.{}", parent, stem) + format!("{parent}.{stem}") }; let compile_path = |src_path: &Path| { let source = fs::read_to_string(src_path).map_err(|err| { Diagnostic::spans_error( self.span, - format!("Error reading file {:?}: {}", path, err), + format!("Error reading file {path:?}: {err}"), ) })?; self.compile_string(&source, mode, module_name.clone(), compiler, || { diff --git a/derive-impl/src/pyclass.rs b/derive-impl/src/pyclass.rs index 47ef69006..f1aee1531 100644 --- a/derive-impl/src/pyclass.rs +++ b/derive-impl/src/pyclass.rs @@ -1,7 +1,8 @@ use super::Diagnostic; use crate::util::{ - pyclass_ident_and_attrs, text_signature, ClassItemMeta, ContentItem, ContentItemInner, - ErrorVec, ItemMeta, ItemMetaInner, ItemNursery, SimpleItemMeta, ALL_ALLOWED_NAMES, + format_doc, pyclass_ident_and_attrs, text_signature, ClassItemMeta, ContentItem, + ContentItemInner, ErrorVec, ItemMeta, ItemMetaInner, ItemNursery, SimpleItemMeta, + ALL_ALLOWED_NAMES, }; use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned, ToTokens}; @@ -212,7 +213,7 @@ fn generate_class_def( quote!(None) }; let module_class_name = if let Some(module_name) = module_name { - format!("{}.{}", module_name, name) + format!("{module_name}.{name}") } else { name.to_owned() }; @@ -504,7 +505,7 @@ where let tokens = { let doc = args.attrs.doc().map_or_else(TokenStream::new, |mut doc| { - doc = format!("{}\n--\n\n{}", sig_doc, doc); + doc = format_doc(&sig_doc, &doc); quote!(.with_doc(#doc.to_owned(), ctx)) }); let build_func = match self.inner.attr_name { @@ -597,7 +598,7 @@ where } }; - let pyname = format!("(slot {})", slot_name); + let pyname = format!("(slot {slot_name})"); args.context.extend_slots_items.add_item( ident.clone(), vec![pyname], @@ -914,7 +915,7 @@ impl MethodItemMeta { } else { let name = inner.item_name(); if magic { - format!("__{}__", name) + format!("__{name}__") } else { name } @@ -985,7 +986,7 @@ impl GetSetItemMeta { GetSetItemKind::Delete => extract_prefix_name("del_", "deleter")?, }; if magic { - format!("__{}__", name) + format!("__{name}__") } else { name } @@ -1107,7 +1108,7 @@ impl MemberItemMeta { MemberItemKind::Get => sig_name, MemberItemKind::Set => extract_prefix_name("set_", "setter")?, }; - Ok((if magic { format!("__{}__", name) } else { name }, kind)) + Ok((if magic { format!("__{name}__") } else { name }, kind)) } fn member_kind(&self) -> Result> { diff --git a/derive-impl/src/pymodule.rs b/derive-impl/src/pymodule.rs index f1d3722cf..c041a2d76 100644 --- a/derive-impl/src/pymodule.rs +++ b/derive-impl/src/pymodule.rs @@ -1,8 +1,8 @@ use crate::error::Diagnostic; use crate::util::{ - iter_use_idents, pyclass_ident_and_attrs, text_signature, AttrItemMeta, AttributeExt, - ClassItemMeta, ContentItem, ContentItemInner, ErrorVec, ItemMeta, ItemNursery, SimpleItemMeta, - ALL_ALLOWED_NAMES, + format_doc, iter_use_idents, pyclass_ident_and_attrs, text_signature, AttrItemMeta, + AttributeExt, ClassItemMeta, ContentItem, ContentItemInner, ErrorVec, ItemMeta, ItemNursery, + SimpleItemMeta, ALL_ALLOWED_NAMES, }; use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned, ToTokens}; @@ -328,7 +328,7 @@ impl ModuleItem for FunctionItem { .map(str::to_owned) }); let doc = if let Some(doc) = doc { - format!("{}\n--\n\n{}", sig_doc, doc) + format_doc(&sig_doc, &doc) } else { sig_doc }; diff --git a/derive-impl/src/util.rs b/derive-impl/src/util.rs index 2d9b2f05c..a877fa519 100644 --- a/derive-impl/src/util.rs +++ b/derive-impl/src/util.rs @@ -399,10 +399,7 @@ impl AttributeExt for Attribute { if found.is_some() { return Err(syn::Error::new( item.span(), - format!( - "#[py..({}...)] must be unique but found multiple times", - item_name, - ), + format!("#[py..({item_name}...)] must be unique but found multiple times"), )); } found = Some(i); @@ -545,7 +542,7 @@ where pub(crate) fn text_signature(sig: &Signature, name: &str) -> String { let signature = func_sig(sig); if signature.starts_with("$self") { - format!("{}({})", name, signature) + format!("{name}({signature})") } else { format!("{}({}, {})", name, "$module", signature) } @@ -584,3 +581,7 @@ fn func_sig(sig: &Signature) -> String { .collect::>() .join(", ") } + +pub(crate) fn format_doc(sig: &str, doc: &str) -> String { + format!("{sig}\n--\n\n{doc}") +} diff --git a/pylib/build.rs b/pylib/build.rs index 33e82c756..2678b92dc 100644 --- a/pylib/build.rs +++ b/pylib/build.rs @@ -9,14 +9,13 @@ fn main() { if cfg!(windows) { if let Ok(real_path) = std::fs::read_to_string("Lib") { - println!("rustc-env:win_lib_path={:?}", real_path); + println!("rustc-env:win_lib_path={real_path:?}"); } } } fn rerun_if_changed(pattern: &str) { - let glob = - glob::glob(pattern).unwrap_or_else(|e| panic!("failed to glob {:?}: {}", pattern, e)); + let glob = glob::glob(pattern).unwrap_or_else(|e| panic!("failed to glob {pattern:?}: {e}")); for entry in glob.flatten() { if entry.is_dir() { continue; @@ -25,6 +24,6 @@ fn rerun_if_changed(pattern: &str) { if display.to_string().ends_with(".pyc") { continue; } - println!("cargo:rerun-if-changed={}", display); + println!("cargo:rerun-if-changed={display}"); } } diff --git a/vm/src/py_serde.rs b/vm/src/py_serde.rs index 437f09b42..10a2acc9c 100644 --- a/vm/src/py_serde.rs +++ b/vm/src/py_serde.rs @@ -123,7 +123,7 @@ impl<'de> DeserializeSeed<'de> for PyObjectDeserializer<'de> { where D: serde::Deserializer<'de>, { - deserializer.deserialize_any(self.clone()) + deserializer.deserialize_any(self) } }