Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply trivial clippy fixes #22

Merged
merged 4 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions compiler/erg_common/codeobj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,33 @@ pub enum CodeObjFlags {
Illegal = 0x0000,
}

impl Into<CodeObjFlags> for u32 {
fn into(self) -> CodeObjFlags {
match self {
0x0001 => CodeObjFlags::Optimized,
0x0002 => CodeObjFlags::NewLocals,
0x0004 => CodeObjFlags::VarArgs,
0x0008 => CodeObjFlags::VarKeywords,
0x0010 => CodeObjFlags::Nested,
0x0020 => CodeObjFlags::Generator,
0x0040 => CodeObjFlags::NoFree,
0x0080 => CodeObjFlags::Coroutine,
0x0100 => CodeObjFlags::IterableCoroutine,
0x0200 => CodeObjFlags::AsyncGenerator,
impl From<u32> for CodeObjFlags {
fn from(flags: u32) -> Self {
match flags {
0x0001 => Self::Optimized,
0x0002 => Self::NewLocals,
0x0004 => Self::VarArgs,
0x0008 => Self::VarKeywords,
0x0010 => Self::Nested,
0x0020 => Self::Generator,
0x0040 => Self::NoFree,
0x0080 => Self::Coroutine,
0x0100 => Self::IterableCoroutine,
0x0200 => Self::AsyncGenerator,
// CO_GENERATOR_ALLOWED,
0x2000 => CodeObjFlags::FutureDivision,
0x4000 => CodeObjFlags::FutureAbsoluteImport,
0x8000 => CodeObjFlags::FutureWithStatement,
0x1_0000 => CodeObjFlags::FuturePrintFunction,
0x2_0000 => CodeObjFlags::FutureUnicodeLiterals,
0x4_0000 => CodeObjFlags::FutureBarryAsBDFL,
0x8_0000 => CodeObjFlags::FutureGeneratorStop,
0x10_0000 => CodeObjFlags::FutureAnnotations,
0x2000 => Self::FutureDivision,
0x4000 => Self::FutureAbsoluteImport,
0x8000 => Self::FutureWithStatement,
0x1_0000 => Self::FuturePrintFunction,
0x2_0000 => Self::FutureUnicodeLiterals,
0x4_0000 => Self::FutureBarryAsBDFL,
0x8_0000 => Self::FutureGeneratorStop,
0x10_0000 => Self::FutureAnnotations,
// EVM flags
0x1000_0000 => CodeObjFlags::EvmDynParam,
0x2000_0000 => CodeObjFlags::EvmDynamic,
0x4000_0000 => CodeObjFlags::EvmNoGC,
_ => CodeObjFlags::Illegal,
0x1000_0000 => Self::EvmDynParam,
0x2000_0000 => Self::EvmDynamic,
0x4000_0000 => Self::EvmNoGC,
_ => Self::Illegal,
}
}
}
Expand Down Expand Up @@ -336,31 +336,31 @@ impl CodeObj {
fn tables_info(&self) -> String {
let mut tables = "".to_string();
if !self.consts.is_empty() {
tables += &format!("Constants:\n");
tables += "Constants:\n";
}
for (i, obj) in self.consts.iter().enumerate() {
tables += &format!(" {}: {}\n", i, obj);
}
if !self.names.is_empty() {
tables += &format!("Names:\n");
tables += "Names:\n";
}
for (i, name) in self.names.iter().enumerate() {
tables += &format!(" {}: {}\n", i, name);
}
if !self.varnames.is_empty() {
tables += &format!("Varnames:\n");
tables += "Varnames:\n";
}
for (i, varname) in self.varnames.iter().enumerate() {
tables += &format!(" {}: {}\n", i, varname);
}
if !self.cellvars.is_empty() {
tables += &format!("Cellvars:\n");
tables += "Cellvars:\n";
}
for (i, cellvar) in self.cellvars.iter().enumerate() {
tables += &format!(" {}: {}\n", i, cellvar);
}
if !self.freevars.is_empty() {
tables += &format!("Freevars:\n");
tables += "Freevars:\n";
}
for (i, freevar) in self.freevars.iter().enumerate() {
tables += &format!(" {}: {}\n", i, freevar);
Expand Down
4 changes: 2 additions & 2 deletions compiler/erg_common/dict.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Borrow;
use std::collections::hash_map::{IntoIter, IntoValues, Iter, IterMut, Keys, Values, ValuesMut};
use std::fmt;
use std::fmt::{self, Write};
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;

Expand Down Expand Up @@ -42,7 +42,7 @@ impl<K: fmt::Display, V: fmt::Display> fmt::Display for Dict<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = "".to_string();
for (k, v) in self.dict.iter() {
s += &format!("{k}: {v}, ");
write!(s, "{k}: {v}, ")?;
}
s.pop();
s.pop();
Expand Down
8 changes: 4 additions & 4 deletions compiler/erg_common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ impl ErrorCore {
}
}

pub const VBAR_UNICODE: &'static str = "│";
pub const VBAR_BREAK_UNICODE: &'static str = "·";
pub const VBAR_UNICODE: &str = "│";
pub const VBAR_BREAK_UNICODE: &str = "·";

/// format:
/// ```console
Expand Down Expand Up @@ -399,9 +399,9 @@ pub trait ErrorDisplay {
let kind = self.core().kind as u8;
let (color, err_or_warn) = if kind < 100 {
(RED, "Error")
} else if 100 <= kind && kind < 150 {
} else if (100..150).contains(&kind) {
(YELLOW, "Warning")
} else if 150 <= kind && kind < 200 {
} else if (150..200).contains(&kind) {
(DEEP_RED, "Error")
} else {
("", "Exception")
Expand Down
10 changes: 2 additions & 8 deletions compiler/erg_common/fxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub type FxHashSet<T> = HashSet<T, BuildHasherDefault<FxHasher>>;
/// out-performs an FNV-based hash within rustc itself -- the collision rate is
/// similar or slightly worse than FNV, but the speed of the hash function
/// itself is much higher because it works on up to 8 bytes at a time.
#[derive(Default)]
pub struct FxHasher {
hash: usize,
}
Expand All @@ -61,13 +62,6 @@ const K: usize = 0x9e3779b9;
#[cfg(target_pointer_width = "64")]
const K: usize = 0x517cc1b727220a95;

impl Default for FxHasher {
#[inline]
fn default() -> FxHasher {
FxHasher { hash: 0 }
}
}

impl FxHasher {
#[inline]
fn add_to_hash(&mut self, i: usize) {
Expand Down Expand Up @@ -97,7 +91,7 @@ impl Hasher for FxHasher {
hash.add_to_hash(u16::from_ne_bytes(bytes[..2].try_into().unwrap()) as usize);
bytes = &bytes[2..];
}
if (size_of::<usize>() > 1) && bytes.len() >= 1 {
if (size_of::<usize>() > 1) && !bytes.is_empty() {
hash.add_to_hash(bytes[0] as usize);
}
self.hash = hash.hash;
Expand Down
4 changes: 4 additions & 0 deletions compiler/erg_common/lazy_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ where
self.buffer.len()
}

pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}

pub fn get_next(&mut self) -> bool {
if self.done {
return false;
Expand Down
8 changes: 4 additions & 4 deletions compiler/erg_common/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ pub fn read_file(mut f: std::fs::File) -> std::io::Result<String> {
Ok(s)
}

pub fn fmt_vec<T: fmt::Display>(v: &Vec<T>) -> String {
pub fn fmt_vec<T: fmt::Display>(v: &[T]) -> String {
fmt_iter(v.iter())
}

pub fn fmt_slice<T: fmt::Display>(v: &[T]) -> String {
fmt_iter(v.iter())
}

pub fn fmt_vec_split_with<T: fmt::Display>(v: &Vec<T>, splitter: &str) -> String {
pub fn fmt_vec_split_with<T: fmt::Display>(v: &[T], splitter: &str) -> String {
fmt_iter_split_with(v.iter(), splitter)
}

Expand Down Expand Up @@ -102,13 +102,13 @@ pub fn get_hash<T: std::hash::Hash>(t: &T) -> usize {
/// \r\n (Windows), \r (old MacOS) -> \n (Unix)
#[inline]
pub fn normalize_newline(src: &str) -> String {
src.replace("\r\n", "\n").replace("\r", "\n")
src.replace("\r\n", "\n").replace('\r', "\n")
}

/// cut \n
#[inline]
pub fn chomp(src: &str) -> String {
normalize_newline(src).replace("\n", "")
normalize_newline(src).replace('\n', "")
}

pub fn try_map<T, U, E, F, I>(i: I, f: F) -> Result<Vec<U>, E>
Expand Down
2 changes: 1 addition & 1 deletion compiler/erg_common/python_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn which_python() -> String {
if res.is_empty() {
panic!("python not found");
}
res.to_string()
res
}

pub fn detect_magic_number() -> u32 {
Expand Down
68 changes: 34 additions & 34 deletions compiler/erg_common/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,30 @@ pub fn get_timestamp_bytes() -> [u8; 4] {
pub enum DataTypePrefix {
/* sized objects */
Illegal = 0,
Int32 = 'i' as u8, // 0x69
Int64 = 'I' as u8, // 0x49
Float = 'f' as u8, // 0x66 (float32, not generated anymore?)
BinFloat = 'g' as u8, // 0x67 (float64)
Complex = 'x' as u8, // 0x78
BinComplex = 'y' as u8, // 0x79
True = 'T' as u8, // 0x54
False = 'F' as u8, // 0x46
None = 'N' as u8, // 0x4E
StopIter = 'S' as u8, // 0x53
Ref = 'r' as u8,
Int32 = b'i', // 0x69
Int64 = b'I', // 0x49
Float = b'f', // 0x66 (float32, not generated anymore?)
BinFloat = b'g', // 0x67 (float64)
Complex = b'x', // 0x78
BinComplex = b'y', // 0x79
True = b'T', // 0x54
False = b'F', // 0x46
None = b'N', // 0x4E
StopIter = b'S', // 0x53
Ref = b'r',
/* unsized objects (ref counted) */
Long = 'l' as u8, // 0x6C + len: u32 + payload: 2*len+3byte (~ -2^31-1 && 2^31 ~)
Str = 's' as u8, // 0x73 + len: u32 + payload
ShortAscii = 'z' as u8, // 0x7A + len: u8 + payload
ShortAsciiInterned = 'Z' as u8, // 0x5A + len: u8 + payload
Unicode = 'u' as u8, // 0x75 + len: u32 + payload
Interned = 't' as u8, // 0x74 + len + payload
SmallTuple = ')' as u8, // 0x29 + len: u8 + payload
Tuple = '(' as u8, // 0x28 + len: u32 + payload
Code = 'c' as u8, // 0x63
Long = b'l', // 0x6C + len: u32 + payload: 2*len+3byte (~ -2^31-1 && 2^31 ~)
Str = b's', // 0x73 + len: u32 + payload
ShortAscii = b'z', // 0x7A + len: u8 + payload
ShortAsciiInterned = b'Z', // 0x5A + len: u8 + payload
Unicode = b'u', // 0x75 + len: u32 + payload
Interned = b't', // 0x74 + len + payload
SmallTuple = b')', // 0x29 + len: u8 + payload
Tuple = b'(', // 0x28 + len: u32 + payload
Code = b'c', // 0x63
/* Erg specific prefix */
Builtin = 'b' as u8, // 0x62 + str
Nat = 'n' as u8,
Builtin = b'b', // 0x62 + str
Nat = b'n',
}

impl_display_from_debug!(DataTypePrefix);
Expand Down Expand Up @@ -103,19 +103,19 @@ impl From<u8> for DataTypePrefix {

impl DataTypePrefix {
pub const fn is_sized(&self) -> bool {
match self {
matches!(
self,
Self::Long
| Self::Str
| Self::ShortAscii
| Self::ShortAsciiInterned
| Self::Unicode
| Self::Interned
| Self::SmallTuple
| Self::Tuple
| Self::Code
| Self::Builtin => false,
_ => true,
}
| Self::Str
| Self::ShortAscii
| Self::ShortAsciiInterned
| Self::Unicode
| Self::Interned
| Self::SmallTuple
| Self::Tuple
| Self::Code
| Self::Builtin
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/erg_common/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ impl<T: Hash + Eq + Clone> Set<T> {
pub fn union(&self, other: &Set<T>) -> Set<T> {
let u = self.elems.union(&other.elems);
Self {
elems: u.into_iter().map(|x| x.clone()).collect(),
elems: u.into_iter().cloned().collect(),
}
}

#[inline]
pub fn intersection(&self, other: &Set<T>) -> Set<T> {
let u = self.elems.intersection(&other.elems);
Self {
elems: u.into_iter().map(|x| x.clone()).collect(),
elems: u.into_iter().cloned().collect(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/erg_common/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Add<&str> for Str {
impl Hash for Str {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Str::Rc(s) => (&s[..]).hash(state),
Str::Rc(s) => (s[..]).hash(state),
Str::Static(s) => s.hash(state),
}
}
Expand All @@ -59,14 +59,14 @@ impl From<&'static str> for Str {
impl From<&String> for Str {
#[inline]
fn from(s: &String) -> Self {
Str::Rc((&s[..]).into())
Str::Rc((s[..]).into())
}
}

impl From<String> for Str {
#[inline]
fn from(s: String) -> Self {
Str::Rc((&s[..]).into())
Str::Rc((s[..]).into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/erg_common/tsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn reorder_by_key<T: Eq, U>(mut g: Graph<T, U>, idx: Vec<T>) -> Graph<T, U> {

fn dfs<T: Eq + Hash + Clone, U>(g: &Graph<T, U>, v: T, used: &mut Set<T>, idx: &mut Vec<T>) -> Result<(), ()> {
used.insert(v.clone());
for node_id in g.iter().find(|n| &n.id == &v).unwrap().depends_on.iter() {
for node_id in g.iter().find(|n| n.id == v).unwrap().depends_on.iter() {
// detecting cycles
if used.contains(node_id) && !idx.contains(node_id) {
return Err(());
Expand Down
Loading