From be40a5d7b9b40f9cd85b91db92ce0d452c6ed855 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sat, 2 Jan 2021 19:30:57 -0600 Subject: [PATCH] Fix clippy lints for Rust 1.49 --- bytecode/src/lib.rs | 2 +- parser/src/lexer.rs | 2 +- src/lib.rs | 21 ++++++----- vm/src/anystr.rs | 64 ++++++++++++--------------------- vm/src/builtins/pystr.rs | 20 ++++++----- vm/src/builtins/pytype.rs | 2 +- vm/src/builtins/traceback.rs | 2 +- vm/src/bytesinner.rs | 18 +++++----- vm/src/function.rs | 4 +-- vm/src/pyobject.rs | 8 +++-- vm/src/stdlib/itertools.rs | 5 +-- vm/src/stdlib/json/machinery.rs | 1 + vm/src/stdlib/tokenize.rs | 7 ++-- 13 files changed, 69 insertions(+), 87 deletions(-) diff --git a/bytecode/src/lib.rs b/bytecode/src/lib.rs index c201e151f..e66d7e1ee 100644 --- a/bytecode/src/lib.rs +++ b/bytecode/src/lib.rs @@ -657,7 +657,7 @@ impl CodeObject { Ok(()) } - pub fn display_expand_codeobjects<'a>(&'a self) -> impl fmt::Display + 'a { + pub fn display_expand_codeobjects(&self) -> impl fmt::Display + '_ { struct Display<'a, C: Constant>(&'a CodeObject); impl fmt::Display for Display<'_, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index a5d5c3ce1..e199447a8 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -116,7 +116,7 @@ pub fn get_keywords() -> HashMap { pub type Spanned = (Location, Tok, Location); pub type LexResult = Result; -pub fn make_tokenizer<'a>(source: &'a str) -> impl Iterator + 'a { +pub fn make_tokenizer(source: &str) -> impl Iterator + '_ { let nlh = NewlineHandler::new(source.chars()); Lexer::new(nlh) } diff --git a/src/lib.rs b/src/lib.rs index 492043ee5..246f434f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -297,15 +297,18 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> { /// Create settings by examining command line arguments and environment /// variables. fn create_settings(matches: &ArgMatches) -> PySettings { - let mut settings = PySettings::default(); - settings.isolated = matches.is_present("isolate"); - settings.ignore_environment = matches.is_present("ignore-environment"); + let mut settings = PySettings { + isolated: matches.is_present("isolate"), + ignore_environment: matches.is_present("ignore-environment"), + interactive: !matches.is_present("c") + && !matches.is_present("m") + && (!matches.is_present("script") || matches.is_present("inspect")), + bytes_warning: matches.occurrences_of("bytes-warning"), + no_site: matches.is_present("no-site"), + ..Default::default() + }; let ignore_environment = settings.ignore_environment || settings.isolated; - settings.interactive = !matches.is_present("c") - && !matches.is_present("m") - && (!matches.is_present("script") || matches.is_present("inspect")); - // add the current directory to sys.path settings.path_list.push("".to_owned()); @@ -352,10 +355,6 @@ fn create_settings(matches: &ArgMatches) -> PySettings { } } - settings.bytes_warning = matches.occurrences_of("bytes-warning"); - - settings.no_site = matches.is_present("no-site"); - if matches.is_present("no-user-site") || matches.is_present("isolate") || (!ignore_environment && env::var_os("PYTHONNOUSERSITE").is_some()) diff --git a/vm/src/anystr.rs b/vm/src/anystr.rs index 5316c5d27..f44f2da2e 100644 --- a/vm/src/anystr.rs +++ b/vm/src/anystr.rs @@ -9,26 +9,15 @@ use num_traits::{cast::ToPrimitive, sign::Signed}; use std::str::FromStr; #[derive(FromArgs)] -pub struct SplitArgs<'a, T, S, E> -where - T: TryFromObject + AnyStrWrapper, - S: ?Sized + AnyStr<'a, E>, - E: Copy, -{ +pub struct SplitArgs<'s, T: TryFromObject + AnyStrWrapper<'s>> { #[pyarg(any, default)] sep: Option, #[pyarg(any, default = "-1")] maxsplit: isize, - _phantom1: std::marker::PhantomData<&'a S>, - _phantom2: std::marker::PhantomData, + _phantom: std::marker::PhantomData<&'s ()>, } -impl<'a, T, S, E> SplitArgs<'a, T, S, E> -where - T: TryFromObject + AnyStrWrapper, - S: ?Sized + AnyStr<'a, E>, - E: Copy, -{ +impl<'s, T: TryFromObject + AnyStrWrapper<'s>> SplitArgs<'s, T> { pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Option, isize)> { let sep = if let Some(s) = self.sep { let sep = s.as_ref(); @@ -124,11 +113,9 @@ impl StringRange for std::ops::Range { } } -pub trait AnyStrWrapper -where - S: ?Sized, -{ - fn as_ref(&self) -> &S; +pub trait AnyStrWrapper<'s> { + type Str: ?Sized + AnyStr<'s>; + fn as_ref(&self) -> &Self::Str; } pub trait AnyStrContainer @@ -140,28 +127,23 @@ where fn push_str(&mut self, s: &S); } -pub trait AnyStr<'s, E> -where - E: Copy, - Self: 's, - Self::Container: AnyStrContainer + std::iter::Extend, - Self::CharIter: 's + std::iter::Iterator, - Self::ElementIter: 's + std::iter::Iterator, -{ - type Container; - type CharIter; - type ElementIter; +// TODO: GATs for `'s` once stabilized +pub trait AnyStr<'s>: 's { + type Char: Copy; + type Container: AnyStrContainer + Extend; + type CharIter: Iterator + 's; + type ElementIter: Iterator + 's; - fn element_bytes_len(c: E) -> usize; + fn element_bytes_len(c: Self::Char) -> usize; fn to_container(&self) -> Self::Container; fn as_bytes(&self) -> &[u8]; fn as_utf8_str(&self) -> Result<&str, std::str::Utf8Error>; fn chars(&'s self) -> Self::CharIter; fn elements(&'s self) -> Self::ElementIter; - fn get_bytes<'a>(&'a self, range: std::ops::Range) -> &'a Self; + fn get_bytes(&self, range: std::ops::Range) -> &Self; // FIXME: get_chars is expensive for str - fn get_chars<'a>(&'a self, range: std::ops::Range) -> &'a Self; + fn get_chars(&self, range: std::ops::Range) -> &Self; fn bytes_len(&self) -> usize; // fn chars_len(&self) -> usize; // cannot access to cache here fn is_empty(&self) -> bool; @@ -175,14 +157,14 @@ where fn py_split( &self, - args: SplitArgs<'s, T, Self, E>, + args: SplitArgs<'s, T>, vm: &VirtualMachine, split: SP, splitn: SN, splitw: SW, ) -> PyResult> where - T: TryFromObject + AnyStrWrapper, + T: TryFromObject + AnyStrWrapper<'s, Str = Self>, SP: Fn(&Self, &Self, &VirtualMachine) -> Vec, SN: Fn(&Self, &Self, usize, &VirtualMachine) -> Vec, SW: Fn(&Self, isize, &VirtualMachine) -> Vec, @@ -249,7 +231,7 @@ where func_default: FD, ) -> &'a Self where - S: AnyStrWrapper, + S: AnyStrWrapper<'s, Str = Self>, FC: Fn(&'a Self, &Self) -> &'a Self, FD: Fn(&'a Self) -> &'a Self, { @@ -286,7 +268,7 @@ where } } - fn py_pad(&self, left: usize, right: usize, fillchar: E) -> Self::Container { + fn py_pad(&self, left: usize, right: usize, fillchar: Self::Char) -> Self::Container { let mut u = Self::Container::with_capacity( (left + right) * Self::element_bytes_len(fillchar) + self.bytes_len(), ); @@ -296,23 +278,23 @@ where u } - fn py_center(&self, width: usize, fillchar: E, len: usize) -> Self::Container { + fn py_center(&self, width: usize, fillchar: Self::Char, len: usize) -> Self::Container { let marg = width - len; let left = marg / 2 + (marg & width & 1); self.py_pad(left, marg - left, fillchar) } - fn py_ljust(&self, width: usize, fillchar: E, len: usize) -> Self::Container { + fn py_ljust(&self, width: usize, fillchar: Self::Char, len: usize) -> Self::Container { self.py_pad(0, width - len, fillchar) } - fn py_rjust(&self, width: usize, fillchar: E, len: usize) -> Self::Container { + fn py_rjust(&self, width: usize, fillchar: Self::Char, len: usize) -> Self::Container { self.py_pad(width - len, 0, fillchar) } fn py_join<'a>( &self, - mut iter: PyIterator<'a, impl AnyStrWrapper + TryFromObject>, + mut iter: PyIterator<'a, impl AnyStrWrapper<'s, Str = Self> + TryFromObject>, ) -> PyResult { let mut joined = if let Some(elem) = iter.next() { elem?.as_ref().to_container() diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index a57a45975..58e90a9ed 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -876,7 +876,7 @@ impl PyStr { fillchar: OptionalArg, vm: &VirtualMachine, ) -> PyResult { - self._pad(width, fillchar, AnyStr::::py_center, vm) + self._pad(width, fillchar, AnyStr::py_center, vm) } #[pymethod] @@ -886,7 +886,7 @@ impl PyStr { fillchar: OptionalArg, vm: &VirtualMachine, ) -> PyResult { - self._pad(width, fillchar, AnyStr::::py_ljust, vm) + self._pad(width, fillchar, AnyStr::py_ljust, vm) } #[pymethod] @@ -896,7 +896,7 @@ impl PyStr { fillchar: OptionalArg, vm: &VirtualMachine, ) -> PyResult { - self._pad(width, fillchar, AnyStr::::py_rjust, vm) + self._pad(width, fillchar, AnyStr::py_rjust, vm) } #[pymethod] @@ -904,7 +904,7 @@ impl PyStr { let tab_stop = args.tabsize(); let mut expanded_str = String::with_capacity(self.value.len()); let mut tab_size = tab_stop; - let mut col_count = 0 as usize; + let mut col_count = 0usize; for ch in self.value.chars() { match ch { '\t' => { @@ -1157,7 +1157,7 @@ impl TryFromObject for std::ffi::OsString { } } -type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef, str, char>; +type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef>; #[derive(FromArgs)] pub struct FindArgs { @@ -1338,7 +1338,8 @@ mod tests { } } -impl AnyStrWrapper for PyStrRef { +impl<'s> AnyStrWrapper<'s> for PyStrRef { + type Str = str; fn as_ref(&self) -> &str { &*self.value } @@ -1358,7 +1359,8 @@ impl AnyStrContainer for String { } } -impl<'s> AnyStr<'s, char> for str { +impl<'s> AnyStr<'s> for str { + type Char = char; type Container = String; type CharIter = std::str::Chars<'s>; type ElementIter = std::str::Chars<'s>; @@ -1387,11 +1389,11 @@ impl<'s> AnyStr<'s, char> for str { str::chars(self) } - fn get_bytes<'a>(&'a self, range: std::ops::Range) -> &'a Self { + fn get_bytes(&self, range: std::ops::Range) -> &Self { &self[range] } - fn get_chars<'a>(&'a self, range: std::ops::Range) -> &'a Self { + fn get_chars(&self, range: std::ops::Range) -> &Self { rustpython_common::str::get_chars(self, range) } diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index bed1d5aae..2e39a2bd8 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -864,7 +864,7 @@ fn calculate_meta_class( Ok(winner) } -fn best_base<'a>(bases: &'a [PyTypeRef], vm: &VirtualMachine) -> PyResult { +fn best_base(bases: &[PyTypeRef], vm: &VirtualMachine) -> PyResult { // let mut base = None; // let mut winner = None; diff --git a/vm/src/builtins/traceback.rs b/vm/src/builtins/traceback.rs index 10cb0aefb..fa7d4a8f1 100644 --- a/vm/src/builtins/traceback.rs +++ b/vm/src/builtins/traceback.rs @@ -53,7 +53,7 @@ impl PyTraceback { } impl PyTracebackRef { - pub fn iter<'a>(&'a self) -> impl Iterator + 'a { + pub fn iter(&self) -> impl Iterator { std::iter::successors(Some(self.clone()), |tb| tb.next.clone()) } } diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 90668eb99..9e065bad4 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -227,7 +227,7 @@ impl ByteInnerTranslateOptions { } } -pub type ByteInnerSplitOptions<'a> = anystr::SplitArgs<'a, PyBytesInner, [u8], u8>; +pub type ByteInnerSplitOptions<'a> = anystr::SplitArgs<'a, PyBytesInner>; #[allow(clippy::len_without_is_empty)] impl PyBytesInner { @@ -468,7 +468,7 @@ impl PyBytesInner { options: ByteInnerPaddingOptions, vm: &VirtualMachine, ) -> PyResult> { - self._pad(options, AnyStr::::py_center, vm) + self._pad(options, AnyStr::py_center, vm) } pub fn ljust( @@ -476,7 +476,7 @@ impl PyBytesInner { options: ByteInnerPaddingOptions, vm: &VirtualMachine, ) -> PyResult> { - self._pad(options, AnyStr::::py_ljust, vm) + self._pad(options, AnyStr::py_ljust, vm) } pub fn rjust( @@ -484,7 +484,7 @@ impl PyBytesInner { options: ByteInnerPaddingOptions, vm: &VirtualMachine, ) -> PyResult> { - self._pad(options, AnyStr::::py_rjust, vm) + self._pad(options, AnyStr::py_rjust, vm) } pub fn count(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult { @@ -938,7 +938,8 @@ pub trait ByteOr: ToPrimitive { impl ByteOr for BigInt {} -impl AnyStrWrapper<[u8]> for PyBytesInner { +impl<'s> AnyStrWrapper<'s> for PyBytesInner { + type Str = [u8]; fn as_ref(&self) -> &[u8] { &self.elements } @@ -960,7 +961,8 @@ impl AnyStrContainer<[u8]> for Vec { const ASCII_WHITESPACES: [u8; 6] = [0x20, 0x09, 0x0a, 0x0c, 0x0d, 0x0b]; -impl<'s> AnyStr<'s, u8> for [u8] { +impl<'s> AnyStr<'s> for [u8] { + type Char = u8; type Container = Vec; type CharIter = bstr::Chars<'s>; type ElementIter = std::iter::Copied>; @@ -989,11 +991,11 @@ impl<'s> AnyStr<'s, u8> for [u8] { self.iter().copied() } - fn get_bytes<'a>(&'a self, range: std::ops::Range) -> &'a Self { + fn get_bytes(&self, range: std::ops::Range) -> &Self { &self[range] } - fn get_chars<'a>(&'a self, range: std::ops::Range) -> &'a Self { + fn get_chars(&self, range: std::ops::Range) -> &Self { &self[range] } diff --git a/vm/src/function.rs b/vm/src/function.rs index 14465b10a..dbd83cd33 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -178,9 +178,7 @@ impl FuncArgs { self.kwargs.swap_remove(name) } - pub fn remaining_keywords<'a>( - &'a mut self, - ) -> impl Iterator + 'a { + pub fn remaining_keywords(&mut self) -> impl Iterator + '_ { self.kwargs.drain(..) } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 4a50ceae1..4a89cb16b 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1104,9 +1104,11 @@ pub trait PyClassImpl: PyClassDef { fn extend_slots(slots: &mut PyTypeSlots); fn make_slots() -> PyTypeSlots { - let mut slots = PyTypeSlots::default(); - slots.flags = Self::TP_FLAGS; - slots.name = PyRwLock::new(Some(Self::TP_NAME.to_owned())); + let mut slots = PyTypeSlots { + flags: Self::TP_FLAGS, + name: PyRwLock::new(Some(Self::TP_NAME.to_owned())), + ..Default::default() + }; Self::extend_slots(&mut slots); slots } diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 0014c82d0..48b73adb1 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -984,10 +984,7 @@ mod decl { args: ProductArgs, vm: &VirtualMachine, ) -> PyResult> { - let repeat = match args.repeat.into_option() { - Some(i) => i, - None => 1, - }; + let repeat = args.repeat.unwrap_or(1); let mut pools = Vec::new(); for arg in iterables.into_iter() { diff --git a/vm/src/stdlib/json/machinery.rs b/vm/src/stdlib/json/machinery.rs index f64fc482e..fe0051a87 100644 --- a/vm/src/stdlib/json/machinery.rs +++ b/vm/src/stdlib/json/machinery.rs @@ -39,6 +39,7 @@ static ESCAPE_CHARS: [&str; 0x20] = [ // And which one need to be escaped (1) // The characters that need escaping are 0x00 to 0x1F, 0x22 ("), 0x5C (\), 0x7F (DEL) // Non-ASCII unicode characters can be safely included in a JSON string +#[allow(clippy::unusual_byte_groupings)] // it's groups of 16, come on clippy static NEEDS_ESCAPING_BITSET: [u64; 4] = [ //fedcba9876543210_fedcba9876543210_fedcba9876543210_fedcba9876543210 0b0000000000000000_0000000000000100_1111111111111111_1111111111111111, // 3_2_1_0 diff --git a/vm/src/stdlib/tokenize.rs b/vm/src/stdlib/tokenize.rs index b6bdcf2be..3e1a97a01 100644 --- a/vm/src/stdlib/tokenize.rs +++ b/vm/src/stdlib/tokenize.rs @@ -6,8 +6,6 @@ pub(crate) use decl::make_module; // TODO: create main function when called with -m #[pymodule(name = "tokenize")] mod decl { - use std::iter::FromIterator; - use crate::builtins::pystr::PyStrRef; use crate::pyobject::{BorrowValue, PyResult}; use crate::vm::VirtualMachine; @@ -20,8 +18,9 @@ mod decl { // TODO: implement generator when the time has come. let lexer1 = lexer::make_tokenizer(source); - let tokens = lexer1.map(|st| vm.ctx.new_str(format!("{:?}", st.unwrap().1))); - let tokens = Vec::from_iter(tokens); + let tokens = lexer1 + .map(|st| vm.ctx.new_str(format!("{:?}", st.unwrap().1))) + .collect(); Ok(vm.ctx.new_list(tokens)) } }