From 814c46996d3c5be45bde7034d77dca6285891d97 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Wed, 22 Jan 2025 02:45:16 +0900 Subject: [PATCH] fix: decl file generator panics --- crates/py2erg/gen_decl.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/crates/py2erg/gen_decl.rs b/crates/py2erg/gen_decl.rs index d9743e0..6bb3049 100644 --- a/crates/py2erg/gen_decl.rs +++ b/crates/py2erg/gen_decl.rs @@ -25,11 +25,11 @@ pub struct DeclFileGenerator { } impl DeclFileGenerator { - pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> Self { + pub fn new(path: &NormalizedPathBuf, status: CheckStatus) -> std::io::Result { let (timestamp, hash) = { - let metadata = std::fs::metadata(path).unwrap(); + let metadata = std::fs::metadata(path)?; let dummy_hash = metadata.len(); - (metadata.modified().unwrap(), dummy_hash) + (metadata.modified()?, dummy_hash) }; let status = PylyzerStatus { status, @@ -38,16 +38,16 @@ impl DeclFileGenerator { hash, }; let code = format!("{status}\n"); - Self { + Ok(Self { filename: path .file_name() - .unwrap() + .unwrap_or_default() .to_string_lossy() .replace(".py", ".d.er"), namespace: "".to_string(), imported: Set::new(), code, - } + }) } pub fn gen_decl_er(mut self, hir: &HIR) -> DeclFile { @@ -209,11 +209,11 @@ impl DeclFileGenerator { } } -fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) { - let decl_gen = DeclFileGenerator::new(path, status); +fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) -> std::io::Result<()> { + let decl_gen = DeclFileGenerator::new(path, status)?; let file = decl_gen.gen_decl_er(hir); let Some(dir) = path.parent().and_then(|p| p.canonicalize().ok()) else { - return; + return Ok(()); }; let cache_dir = dir.join("__pycache__"); if !cache_dir.exists() { @@ -221,19 +221,17 @@ fn dump_decl_er(path: &NormalizedPathBuf, hir: &HIR, status: CheckStatus) { } let path = cache_dir.join(file.filename); if !path.exists() { - let _f = File::create(&path); + File::create(&path)?; } - let Ok(f) = File::options().write(true).open(path) else { - return; - }; + let f = File::options().write(true).open(path)?; let mut f = BufWriter::new(f); - let _ = f.write_all(file.code.as_bytes()); + f.write_all(file.code.as_bytes()) } pub fn dump_decl_package(modules: &SharedModuleCache) { for (path, module) in modules.raw_iter() { if let Some(hir) = module.hir.as_ref() { - dump_decl_er(path, hir, module.status); + let _ = dump_decl_er(path, hir, module.status); } } }