diff --git a/compiler/erg_common/build.rs b/compiler/erg_common/build.rs index 3feb4b37..17f6ada5 100644 --- a/compiler/erg_common/build.rs +++ b/compiler/erg_common/build.rs @@ -11,7 +11,7 @@ fn main() -> std::io::Result<()> { let git_hash_short = String::from_utf8_lossy(&output.stdout); let now = datetime::now(); #[allow(deprecated)] - let erg_path = std::env::home_dir().unwrap().join(".erg"); + let erg_path = std::env::home_dir().unwrap_or_default().join(".erg"); println!("cargo:rustc-env=GIT_HASH_SHORT={git_hash_short}"); println!("cargo:rustc-env=BUILD_DATE={now}"); println!("cargo:rustc-env=CARGO_ERG_PATH={}", erg_path.display()); diff --git a/compiler/erg_common/macros.rs b/compiler/erg_common/macros.rs index d52b4840..16a6aa62 100644 --- a/compiler/erg_common/macros.rs +++ b/compiler/erg_common/macros.rs @@ -269,7 +269,7 @@ macro_rules! fn_name { fn type_name_of(_: T) -> &'static str { std::any::type_name::() } - let name = type_name_of(dummy).rsplit("::").nth(1).unwrap(); + let name = type_name_of(dummy).rsplit("::").nth(1).unwrap_or("?"); &name[..] }}; } diff --git a/compiler/erg_common/python_util.rs b/compiler/erg_common/python_util.rs index 6bb144b0..2fd67493 100644 --- a/compiler/erg_common/python_util.rs +++ b/compiler/erg_common/python_util.rs @@ -624,9 +624,9 @@ impl std::str::FromStr for PythonVersion { fn from_str(s: &str) -> Result { let mut iter = s.split('.'); - let major = iter.next().unwrap().parse::().unwrap_or(3); - let minor = iter.next().and_then(|i| i.parse::().ok()); - let micro = iter.next().and_then(|i| i.parse::().ok()); + let major = iter.next().and_then(|i| i.parse().ok()).unwrap_or(3); + let minor = iter.next().and_then(|i| i.parse().ok()); + let micro = iter.next().and_then(|i| i.parse().ok()); Ok(Self::new(major, minor, micro)) } } @@ -652,9 +652,9 @@ pub fn get_python_version(py_command: &str) -> PythonVersion { }; let s_version = String::from_utf8(out.stdout).unwrap(); let mut iter = s_version.split(' '); - let major = iter.next().unwrap().parse().unwrap(); - let minor = iter.next().and_then(|i| i.parse::().ok()); - let micro = iter.next().and_then(|i| i.trim_end().parse::().ok()); + let major = iter.next().and_then(|i| i.parse().ok()).unwrap_or(3); + let minor = iter.next().and_then(|i| i.parse().ok()); + let micro = iter.next().and_then(|i| i.trim_end().parse().ok()); PythonVersion { major, minor, diff --git a/compiler/erg_common/serialize.rs b/compiler/erg_common/serialize.rs index 79f0d195..cc449e66 100644 --- a/compiler/erg_common/serialize.rs +++ b/compiler/erg_common/serialize.rs @@ -40,8 +40,8 @@ pub const fn get_ver_from_magic_num(magic_num: u32) -> PythonVersion { pub fn get_timestamp_bytes() -> [u8; 4] { let secs = SystemTime::now() .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs() as u32; + .map(|dur| dur.as_secs() as u32) + .unwrap_or(0); secs.to_le_bytes() } diff --git a/compiler/erg_common/style.rs b/compiler/erg_common/style.rs index 6b6d623f..d0bbf710 100644 --- a/compiler/erg_common/style.rs +++ b/compiler/erg_common/style.rs @@ -417,7 +417,7 @@ impl StyledStrings { /// /// ``` pub fn push_str(&mut self, s: &str) { - if self.is_same_color(Color::Gray) { + if self.color_is(Color::Gray) { self.texts.last_mut().unwrap().text.push_str(s); } else { self.texts.push(StyledString::new(s, None, None)); @@ -438,7 +438,7 @@ impl StyledStrings { /// println!("{}", texts); /// ``` pub fn push_str_with_color<'a, S: Into>>(&mut self, s: S, color: Color) { - if self.is_same_color(color) { + if self.color_is(color) { let text = s.into(); self.texts.last_mut().unwrap().text.push_str(&text); } else { @@ -465,7 +465,7 @@ impl StyledStrings { color: Color, attr: Attribute, ) { - if self.is_same_color(color) && self.is_same_attribute(attr) { + if self.color_is(color) && self.attr_is(attr) { let text = s.into(); self.texts.last_mut().unwrap().text.push_str(&text); } else { @@ -482,14 +482,14 @@ impl StyledStrings { self.texts.iter().all(|s| s.is_empty()) } - fn is_same_color(&self, color: Color) -> bool { + fn color_is(&self, color: Color) -> bool { if let Some(text) = self.texts.last() { return text.color == Some(color); } false } - fn is_same_attribute(&self, attr: Attribute) -> bool { + fn attr_is(&self, attr: Attribute) -> bool { if let Some(text) = self.texts.last() { if let Some(text_attr) = text.attribute { return text_attr == attr;