Skip to content

Commit e7e1d8d

Browse files
authored
Merge pull request #245 from LilyAcorn/rust-importscanner-review-fixes
Rust importscanner review fixes
2 parents 05b0f7d + d8fc3e0 commit e7e1d8d

5 files changed

Lines changed: 41 additions & 47 deletions

File tree

rust/Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ bimap = "0.6.3"
1313
slotmap = "1.0.7"
1414
getset = "0.1.3"
1515
derive-new = "0.7.0"
16-
lazy_static = "1.5.0"
1716
string-interner = "0.18.0"
1817
thiserror = "2.0.11"
1918
itertools = "0.14.0"

rust/src/filesystem.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
use itertools::Itertools;
12
use pyo3::exceptions::{PyFileNotFoundError, PyUnicodeDecodeError};
23
use pyo3::prelude::*;
34
use regex::Regex;
45
use std::collections::HashMap;
56
use std::ffi::OsStr;
67
use std::fs;
78
use std::path::{Path, PathBuf};
9+
use std::sync::LazyLock;
810
use unindent::unindent;
9-
use lazy_static::lazy_static;
1011

1112

12-
lazy_static! {
13-
static ref ENCODING_RE: Regex = Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap();
14-
}
13+
static ENCODING_RE: LazyLock<Regex> = LazyLock::new(|| {
14+
Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap()
15+
});
1516

1617
pub trait FileSystem: Send + Sync {
17-
fn sep(&self) -> String;
18+
fn sep(&self) -> &str;
1819

1920
fn join(&self, components: Vec<String>) -> String;
2021

@@ -37,16 +38,16 @@ pub struct PyRealBasicFileSystem {
3738
}
3839

3940
impl FileSystem for RealBasicFileSystem {
40-
fn sep(&self) -> String {
41-
std::path::MAIN_SEPARATOR.to_string()
41+
fn sep(&self) -> &str {
42+
std::path::MAIN_SEPARATOR_STR
4243
}
4344

4445
fn join(&self, components: Vec<String>) -> String {
4546
let mut path = PathBuf::new();
4647
for component in components {
4748
path.push(component);
4849
}
49-
path.to_str().unwrap().to_string()
50+
path.to_str().expect("Path components should be valid unicode").to_string()
5051
}
5152

5253
fn split(&self, file_name: &str) -> (String, String) {
@@ -65,8 +66,8 @@ impl FileSystem for RealBasicFileSystem {
6566
};
6667

6768
(
68-
head.to_str().unwrap().to_string(),
69-
tail.to_str().unwrap().to_string(),
69+
head.to_str().expect("Path components should be valid unicode").to_string(),
70+
tail.to_str().expect("Path components should be valid unicode").to_string(),
7071
)
7172
}
7273

@@ -136,7 +137,7 @@ impl PyRealBasicFileSystem {
136137
}
137138

138139
#[getter]
139-
fn sep(&self) -> String {
140+
fn sep(&self) -> &str {
140141
self.inner.sep()
141142
}
142143

@@ -191,17 +192,16 @@ impl FakeBasicFileSystem {
191192
}
192193

193194
impl FileSystem for FakeBasicFileSystem {
194-
fn sep(&self) -> String {
195-
"/".to_string()
195+
fn sep(&self) -> &str {
196+
"/"
196197
}
197198

198199
fn join(&self, components: Vec<String>) -> String {
199200
let sep = self.sep();
200201
components
201202
.into_iter()
202-
.map(|c| c.trim_end_matches(&sep).to_string())
203-
.collect::<Vec<String>>()
204-
.join(&sep)
203+
.map(|c| c.trim_end_matches(sep).to_string())
204+
.join(sep)
205205
}
206206

207207
fn split(&self, file_name: &str) -> (String, String) {
@@ -217,8 +217,8 @@ impl FileSystem for FakeBasicFileSystem {
217217
tail = path.file_name().unwrap_or(OsStr::new(""));
218218
}
219219
(
220-
head.to_str().unwrap().to_string(),
221-
tail.to_str().unwrap().to_string(),
220+
head.to_str().expect("Path components should be valid unicode").to_string(),
221+
tail.to_str().expect("Path components should be valid unicode").to_string(),
222222
)
223223
}
224224

@@ -230,7 +230,7 @@ impl FileSystem for FakeBasicFileSystem {
230230
fn read(&self, file_name: &str) -> PyResult<String> {
231231
match self.contents.get(file_name) {
232232
Some(file_name) => Ok(file_name.clone()),
233-
None => Err(PyFileNotFoundError::new_err("")),
233+
None => Err(PyFileNotFoundError::new_err(format!("No such file: {file_name}"))),
234234
}
235235
}
236236
}
@@ -246,7 +246,7 @@ impl PyFakeBasicFileSystem {
246246
}
247247

248248
#[getter]
249-
fn sep(&self) -> String {
249+
fn sep(&self) -> &str {
250250
self.inner.sep()
251251
}
252252

@@ -288,7 +288,8 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap<St
288288
let buffer = file_system_string.replace("\r\n", "\n");
289289
let lines: Vec<&str> = buffer.split('\n').collect();
290290

291-
for line_raw in lines.clone() {
291+
let first_line_starts_with_slash = lines[0].trim().starts_with('/');
292+
for line_raw in lines {
292293
let line = line_raw.trim_end(); // Remove trailing whitespace
293294
if line.is_empty() {
294295
continue; // Skip empty lines
@@ -331,7 +332,7 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap<St
331332
let mut joined = path_stack.join("/");
332333
// If the original root started with a slash, ensure the final path does too.
333334
// But be careful not to double-slash if a component is e.g. "/root"
334-
if lines[0].trim().starts_with('/') && !joined.starts_with('/') {
335+
if first_line_starts_with_slash && !joined.starts_with('/') {
335336
joined = format!("/{joined}");
336337
}
337338
joined
@@ -353,7 +354,7 @@ pub fn parse_indented_file_system_string(file_system_string: &str) -> HashMap<St
353354
// Edge case: If the very first line was a file and it ended up on the stack, it needs to be processed.
354355
// This handles single-file inputs like "myfile.txt"
355356
if !path_stack.is_empty()
356-
&& !path_stack.last().unwrap().ends_with('/')
357+
&& !path_stack.last().expect("path_stack should be non-empty").ends_with('/')
357358
&& !file_paths_map.contains_key(&path_stack.join("/"))
358359
{
359360
file_paths_map.insert(path_stack.join("/"), String::new());

rust/src/graph/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use bimap::BiMap;
22
use derive_new::new;
33
use getset::{CopyGetters, Getters};
4-
use lazy_static::lazy_static;
54
use rustc_hash::{FxHashMap, FxHashSet};
65
use slotmap::{SecondaryMap, SlotMap, new_key_type};
7-
use std::sync::RwLock;
6+
use std::sync::{LazyLock, RwLock};
87
use string_interner::backend::StringBackend;
98
use string_interner::{DefaultSymbol, StringInterner};
109

@@ -16,15 +15,18 @@ pub mod import_chain_queries;
1615

1716
pub(crate) mod pathfinding;
1817

19-
lazy_static! {
20-
static ref MODULE_NAMES: RwLock<StringInterner<StringBackend>> =
21-
RwLock::new(StringInterner::default());
22-
static ref IMPORT_LINE_CONTENTS: RwLock<StringInterner<StringBackend>> =
23-
RwLock::new(StringInterner::default());
24-
static ref EMPTY_MODULE_TOKENS: FxHashSet<ModuleToken> = FxHashSet::default();
25-
static ref EMPTY_IMPORT_DETAILS: FxHashSet<ImportDetails> = FxHashSet::default();
26-
static ref EMPTY_IMPORTS: FxHashSet<(ModuleToken, ModuleToken)> = FxHashSet::default();
27-
}
18+
static MODULE_NAMES: LazyLock<RwLock<StringInterner<StringBackend>>> = LazyLock::new(|| {
19+
RwLock::new(StringInterner::default())
20+
});
21+
static IMPORT_LINE_CONTENTS: LazyLock<RwLock<StringInterner<StringBackend>>> = LazyLock::new(|| {
22+
RwLock::new(StringInterner::default())
23+
});
24+
static EMPTY_MODULE_TOKENS: LazyLock<FxHashSet<ModuleToken>> = LazyLock::new(|| {
25+
FxHashSet::default()
26+
});
27+
static EMPTY_IMPORT_DETAILS: LazyLock<FxHashSet<ImportDetails>> = LazyLock::new(|| {
28+
FxHashSet::default()
29+
});
2830

2931
new_key_type! { pub struct ModuleToken; }
3032

rust/src/module_expressions.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use crate::errors::{GrimpError, GrimpResult};
22
use const_format::formatcp;
33
use itertools::Itertools;
4-
use lazy_static::lazy_static;
54
use regex::Regex;
65
use std::fmt::Display;
76
use std::str::FromStr;
7+
use std::sync::LazyLock;
88

9-
lazy_static! {
10-
static ref MODULE_EXPRESSION_PATTERN: Regex =
11-
Regex::new(r"^(\w+|\*{1,2})(\.(\w+|\*{1,2}))*$").unwrap();
12-
}
9+
static MODULE_EXPRESSION_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
10+
Regex::new(r"^(\w+|\*{1,2})(\.(\w+|\*{1,2}))*$").unwrap()
11+
});
1312

1413
/// A module expression is used to refer to sets of modules.
1514
///

0 commit comments

Comments
 (0)