fix(rust): adapt to new nightly lint

(cherry picked from commit 11071ed682)
This commit is contained in:
WillLillis 2025-03-03 01:37:48 -05:00 committed by Will Lillis
parent 4adcebe284
commit c313be63b2
7 changed files with 37 additions and 25 deletions

View file

@ -112,7 +112,7 @@ fn main() {
parse(path, max_path_length, |source| { parse(path, max_path_length, |source| {
Query::new(&language, str::from_utf8(source).unwrap()) Query::new(&language, str::from_utf8(source).unwrap())
.with_context(|| format!("Query file path: {path:?}")) .with_context(|| format!("Query file path: {}", path.display()))
.expect("Failed to parse query"); .expect("Failed to parse query");
}); });
} }
@ -201,7 +201,7 @@ fn parse(path: &Path, max_path_length: usize, mut action: impl FnMut(&[u8])) ->
); );
let source_code = fs::read(path) let source_code = fs::read(path)
.with_context(|| format!("Failed to read {path:?}")) .with_context(|| format!("Failed to read {}", path.display()))
.unwrap(); .unwrap();
let time = Instant::now(); let time = Instant::now();
for _ in 0..*REPETITION_COUNT { for _ in 0..*REPETITION_COUNT {
@ -221,6 +221,6 @@ fn get_language(path: &Path) -> Language {
let src_path = GRAMMARS_DIR.join(path).join("src"); let src_path = GRAMMARS_DIR.join(path).join("src");
TEST_LOADER TEST_LOADER
.load_language_at_path(CompileConfig::new(&src_path, None, None)) .load_language_at_path(CompileConfig::new(&src_path, None, None))
.with_context(|| format!("Failed to load language at path {src_path:?}")) .with_context(|| format!("Failed to load language at path {}", src_path.display()))
.unwrap() .unwrap()
} }

View file

@ -183,7 +183,8 @@ pub fn generate_parser_in_directory(
if grammar_path.file_name().unwrap() != "grammar.json" { if grammar_path.file_name().unwrap() != "grammar.json" {
fs::write(src_path.join("grammar.json"), &grammar_json).map_err(|e| { fs::write(src_path.join("grammar.json"), &grammar_json).map_err(|e| {
GenerateError::IO(format!( GenerateError::IO(format!(
"Failed to write grammar.json to {src_path:?} -- {e}" "Failed to write grammar.json to {} -- {e}",
src_path.display()
)) ))
})?; })?;
} }

View file

@ -561,8 +561,8 @@ impl Loader {
// If multiple language configurations match, then determine which // If multiple language configurations match, then determine which
// one to use by applying the configurations' content regexes. // one to use by applying the configurations' content regexes.
else { else {
let file_contents = let file_contents = fs::read(path)
fs::read(path).with_context(|| format!("Failed to read path {path:?}"))?; .with_context(|| format!("Failed to read path {}", path.display()))?;
let file_contents = String::from_utf8_lossy(&file_contents); let file_contents = String::from_utf8_lossy(&file_contents);
let mut best_score = -2isize; let mut best_score = -2isize;
let mut best_configuration_id = None; let mut best_configuration_id = None;
@ -780,8 +780,8 @@ impl Loader {
if recompile { if recompile {
fs::create_dir_all(lock_path.parent().unwrap()).with_context(|| { fs::create_dir_all(lock_path.parent().unwrap()).with_context(|| {
format!( format!(
"Failed to create directory {:?}", "Failed to create directory {}",
lock_path.parent().unwrap() lock_path.parent().unwrap().display()
) )
})?; })?;
let lock_file = fs::OpenOptions::new() let lock_file = fs::OpenOptions::new()
@ -799,7 +799,7 @@ impl Loader {
} }
let library = unsafe { Library::new(&output_path) } let library = unsafe { Library::new(&output_path) }
.with_context(|| format!("Error opening dynamic library {output_path:?}"))?; .with_context(|| format!("Error opening dynamic library {}", output_path.display()))?;
let language = unsafe { let language = unsafe {
let language_fn = library let language_fn = library
.get::<Symbol<unsafe extern "C" fn() -> Language>>(language_fn_name.as_bytes()) .get::<Symbol<unsafe extern "C" fn() -> Language>>(language_fn_name.as_bytes())
@ -1564,7 +1564,7 @@ impl LanguageConfiguration<'_> {
error.row = source[range.start..offset_within_section] error.row = source[range.start..offset_within_section]
.matches('\n') .matches('\n')
.count(); .count();
Error::from(error).context(format!("Error in query file {path:?}")) Error::from(error).context(format!("Error in query file {}", path.display()))
} }
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
@ -1581,7 +1581,7 @@ impl LanguageConfiguration<'_> {
let abs_path = self.root_path.join(path); let abs_path = self.root_path.join(path);
let prev_query_len = query.len(); let prev_query_len = query.len();
query += &fs::read_to_string(&abs_path) query += &fs::read_to_string(&abs_path)
.with_context(|| format!("Failed to read query file {path:?}"))?; .with_context(|| format!("Failed to read query file {}", path.display()))?;
path_ranges.push((path.clone(), prev_query_len..query.len())); path_ranges.push((path.clone(), prev_query_len..query.len()));
} }
} else { } else {
@ -1599,7 +1599,7 @@ impl LanguageConfiguration<'_> {
let path = queries_path.join(default_path); let path = queries_path.join(default_path);
if path.exists() { if path.exists() {
query = fs::read_to_string(&path) query = fs::read_to_string(&path)
.with_context(|| format!("Failed to read query file {path:?}"))?; .with_context(|| format!("Failed to read query file {}", path.display()))?;
path_ranges.push((PathBuf::from(default_path), 0..query.len())); path_ranges.push((PathBuf::from(default_path), 0..query.len()));
} }
} }
@ -1612,8 +1612,8 @@ fn needs_recompile(lib_path: &Path, paths_to_check: &[PathBuf]) -> Result<bool>
if !lib_path.exists() { if !lib_path.exists() {
return Ok(true); return Ok(true);
} }
let lib_mtime = let lib_mtime = mtime(lib_path)
mtime(lib_path).with_context(|| format!("Failed to read mtime of {lib_path:?}"))?; .with_context(|| format!("Failed to read mtime of {}", lib_path.display()))?;
for path in paths_to_check { for path in paths_to_check {
if mtime(path)? > lib_mtime { if mtime(path)? > lib_mtime {
return Ok(true); return Ok(true);

View file

@ -89,8 +89,8 @@ pub fn get_input(
let Some(path_str) = path.to_str() else { let Some(path_str) = path.to_str() else {
bail!("Invalid path: {}", path.display()); bail!("Invalid path: {}", path.display());
}; };
let paths = let paths = glob(path_str)
glob(path_str).with_context(|| format!("Invalid glob pattern {path:?}"))?; .with_context(|| format!("Invalid glob pattern {}", path.display()))?;
for path in paths { for path in paths {
incorporate_path(path?, positive); incorporate_path(path?, positive);
} }

View file

@ -34,7 +34,7 @@ pub fn query_file_at_path(
let mut stdout = stdout.lock(); let mut stdout = stdout.lock();
let query_source = fs::read_to_string(query_path) let query_source = fs::read_to_string(query_path)
.with_context(|| format!("Error reading query file {query_path:?}"))?; .with_context(|| format!("Error reading query file {}", query_path.display()))?;
let query = Query::new(language, &query_source).with_context(|| "Query compilation failed")?; let query = Query::new(language, &query_source).with_context(|| "Query compilation failed")?;
let mut query_cursor = QueryCursor::new(); let mut query_cursor = QueryCursor::new();
@ -55,7 +55,7 @@ pub fn query_file_at_path(
} }
let source_code = let source_code =
fs::read(path).with_context(|| format!("Error reading source file {path:?}"))?; fs::read(path).with_context(|| format!("Error reading source file {}", path.display()))?;
let tree = parser.parse(&source_code, None).unwrap(); let tree = parser.parse(&source_code, None).unwrap();
let start = Instant::now(); let start = Instant::now();

View file

@ -23,10 +23,18 @@ pub fn load_language_wasm_file(language_dir: &Path) -> Result<(String, Vec<u8>)>
pub fn get_grammar_name(language_dir: &Path) -> Result<String> { pub fn get_grammar_name(language_dir: &Path) -> Result<String> {
let src_dir = language_dir.join("src"); let src_dir = language_dir.join("src");
let grammar_json_path = src_dir.join("grammar.json"); let grammar_json_path = src_dir.join("grammar.json");
let grammar_json = fs::read_to_string(&grammar_json_path) let grammar_json = fs::read_to_string(&grammar_json_path).with_context(|| {
.with_context(|| format!("Failed to read grammar file {grammar_json_path:?}"))?; format!(
let grammar: GrammarJSON = serde_json::from_str(&grammar_json) "Failed to read grammar file {}",
.with_context(|| format!("Failed to parse grammar file {grammar_json_path:?}"))?; grammar_json_path.display()
)
})?;
let grammar: GrammarJSON = serde_json::from_str(&grammar_json).with_context(|| {
format!(
"Failed to parse grammar file {}",
grammar_json_path.display()
)
})?;
Ok(grammar.name) Ok(grammar.name)
} }

View file

@ -112,7 +112,10 @@ fn generate_bindings(out_dir: &std::path::Path) {
.expect("Failed to generate bindings"); .expect("Failed to generate bindings");
let bindings_rs = out_dir.join("bindings.rs"); let bindings_rs = out_dir.join("bindings.rs");
bindings bindings.write_to_file(&bindings_rs).unwrap_or_else(|_| {
.write_to_file(&bindings_rs) panic!(
.unwrap_or_else(|_| panic!("Failed to write bindings into path: {bindings_rs:?}")); "Failed to write bindings into path: {}",
bindings_rs.display()
)
});
} }