diff --git a/cli/loader/build.rs b/cli/loader/build.rs index 979d8cf4..714a662b 100644 --- a/cli/loader/build.rs +++ b/cli/loader/build.rs @@ -5,8 +5,5 @@ fn main() { ); let emscripten_version = std::fs::read_to_string("emscripten-version").unwrap(); - println!( - "cargo:rustc-env={}={}", - "EMSCRIPTEN_VERSION", emscripten_version, - ); + println!("cargo:rustc-env=EMSCRIPTEN_VERSION={emscripten_version}"); } diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index 53af492e..475b6ea2 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -39,9 +39,8 @@ where D: Deserializer<'de>, { let paths = Vec::::deserialize(deserializer)?; - let home = match dirs::home_dir() { - Some(home) => home, - None => return Ok(paths), + let Some(home) = dirs::home_dir() else { + return Ok(paths); }; let standardized = paths .into_iter() @@ -61,9 +60,10 @@ fn standardize_path(path: PathBuf, home: &Path) -> PathBuf { } impl Config { - pub fn initial() -> Config { + #[must_use] + pub fn initial() -> Self { let home_dir = dirs::home_dir().expect("Cannot determine home directory"); - Config { + Self { parser_directories: vec![ home_dir.join("github"), home_dir.join("src"), @@ -77,7 +77,7 @@ impl Config { const DYLIB_EXTENSION: &str = "so"; #[cfg(windows)] -const DYLIB_EXTENSION: &'static str = "dll"; +const DYLIB_EXTENSION: &str = "dll"; const BUILD_TARGET: &str = env!("BUILD_TARGET"); @@ -122,15 +122,16 @@ impl Loader { let parser_lib_path = match env::var("TREE_SITTER_LIBDIR") { Ok(path) => PathBuf::from(path), _ => dirs::cache_dir() - .ok_or(anyhow!("Cannot determine cache directory"))? + .ok_or_else(|| anyhow!("Cannot determine cache directory"))? .join("tree-sitter") .join("lib"), }; Ok(Self::with_parser_lib_path(parser_lib_path)) } + #[must_use] pub fn with_parser_lib_path(parser_lib_path: PathBuf) -> Self { - Loader { + Self { parser_lib_path, languages_by_id: Vec::new(), language_configurations: Vec::new(), @@ -152,6 +153,7 @@ impl Loader { highlights.extend(names.iter().cloned()); } + #[must_use] pub fn highlight_names(&self) -> Vec { self.highlight_names.lock().unwrap().clone() } @@ -188,7 +190,7 @@ impl Loader { .iter() .map(|c| c.language_id) .collect::>(); - language_ids.sort(); + language_ids.sort_unstable(); language_ids.dedup(); language_ids .into_iter() @@ -199,6 +201,7 @@ impl Loader { } } + #[must_use] pub fn get_all_language_configurations(&self) -> Vec<(&LanguageConfiguration, &Path)> { self.language_configurations .iter() @@ -239,17 +242,14 @@ impl Loader { if let Some(configuration_ids) = configuration_ids { if !configuration_ids.is_empty() { - let configuration; - - // If there is only one language configuration, then use it. - if configuration_ids.len() == 1 { - configuration = &self.language_configurations[configuration_ids[0]]; + let configuration = if configuration_ids.len() == 1 { + &self.language_configurations[configuration_ids[0]] } // If multiple language configurations match, then determine which // one to use by applying the configurations' content regexes. else { - let file_contents = fs::read(path) - .with_context(|| format!("Failed to read path {:?}", path))?; + let file_contents = + fs::read(path).with_context(|| format!("Failed to read path {path:?}"))?; let file_contents = String::from_utf8_lossy(&file_contents); let mut best_score = -2isize; let mut best_configuration_id = None; @@ -279,8 +279,8 @@ impl Loader { } } - configuration = &self.language_configurations[best_configuration_id.unwrap()]; - } + &self.language_configurations[best_configuration_id.unwrap()] + }; let language = self.language_for_id(configuration.language_id)?; return Ok(Some((language, configuration))); @@ -364,7 +364,7 @@ impl Loader { library_path.set_extension(DYLIB_EXTENSION); let parser_path = src_path.join("parser.c"); - let scanner_path = self.get_scanner_path(&src_path); + let scanner_path = self.get_scanner_path(src_path); #[cfg(feature = "wasm")] if self.wasm_store.lock().unwrap().is_some() { @@ -382,7 +382,7 @@ impl Loader { src_path, scanner_path .as_ref() - .and_then(|p| p.strip_prefix(&src_path).ok()), + .and_then(|p| p.strip_prefix(src_path).ok()), &library_path, false, )?; @@ -403,15 +403,15 @@ impl Loader { } let library = unsafe { Library::new(&library_path) } - .with_context(|| format!("Error opening dynamic library {:?}", &library_path))?; + .with_context(|| format!("Error opening dynamic library {library_path:?}"))?; let language = unsafe { let language_fn: Symbol Language> = library .get(language_fn_name.as_bytes()) - .with_context(|| format!("Failed to load symbol {}", language_fn_name))?; + .with_context(|| format!("Failed to load symbol {language_fn_name}"))?; language_fn() }; mem::forget(library); - return Ok(language); + Ok(language) } } @@ -437,10 +437,12 @@ impl Loader { } if compiler.is_like_msvc() { - command.args(&["/nologo", "/LD"]); - header_paths.iter().for_each(|path| { + command.args(["/nologo", "/LD"]); + + for path in header_paths { command.arg(format!("/I{}", path.to_string_lossy())); - }); + } + if self.debug_build { command.arg("/Od"); } else { @@ -459,11 +461,11 @@ impl Loader { .arg("-fno-exceptions") .arg("-g") .arg("-o") - .arg(&library_path); + .arg(library_path); - header_paths.iter().for_each(|path| { + for path in header_paths { command.arg(format!("-I{}", path.to_string_lossy())); - }); + } if !cfg!(windows) { command.arg("-fPIC"); @@ -505,7 +507,7 @@ impl Loader { let command = Command::new("nm") .arg("-W") .arg("-U") - .arg(&library_path) + .arg(library_path) .output(); if let Ok(output) = command { if output.status.success() { @@ -688,6 +690,7 @@ impl Loader { Ok(()) } + #[must_use] pub fn highlight_config_for_injection_string<'a>( &'a self, string: &str, @@ -695,10 +698,7 @@ impl Loader { ) -> Option<&'a HighlightConfiguration> { match self.language_configuration_for_injection_string(string) { Err(e) => { - eprintln!( - "Failed to load language for injection string '{}': {}", - string, e - ); + eprintln!("Failed to load language for injection string '{string}': {e}",); None } Ok(None) => None, @@ -706,8 +706,7 @@ impl Loader { match configuration.highlight_config(language, apply_all_captures, None) { Err(e) => { eprintln!( - "Failed to load property sheet for injection string '{}': {}", - string, e + "Failed to load property sheet for injection string '{string}': {e}", ); None } @@ -735,9 +734,9 @@ impl Loader { impl PathsJSON { fn into_vec(self) -> Option> { match self { - PathsJSON::Empty => None, - PathsJSON::Single(s) => Some(vec![s]), - PathsJSON::Multiple(s) => Some(s), + Self::Empty => None, + Self::Single(s) => Some(vec![s]), + Self::Multiple(s) => Some(s), } } } @@ -779,7 +778,7 @@ impl Loader { let initial_language_configuration_count = self.language_configurations.len(); - if let Ok(package_json_contents) = fs::read_to_string(&parser_path.join("package.json")) { + if let Ok(package_json_contents) = fs::read_to_string(parser_path.join("package.json")) { let package_json = serde_json::from_str::(&package_json_contents); if let Ok(package_json) = package_json { let language_count = self.languages_by_id.len(); @@ -939,6 +938,7 @@ impl Loader { *self.wasm_store.lock().unwrap() = Some(tree_sitter::WasmStore::new(engine).unwrap()) } + #[must_use] pub fn get_scanner_path(&self, src_path: &Path) -> Option { let mut path = src_path.join("scanner.c"); for extension in ["c", "cc", "cpp"] { @@ -1054,11 +1054,12 @@ impl<'a> LanguageConfiguration<'a> { if self.use_all_highlight_names { for capture_name in result.query.capture_names() { if !all_highlight_names.iter().any(|x| x == capture_name) { - all_highlight_names.push(capture_name.to_string()); + all_highlight_names.push((*capture_name).to_string()); } } } result.configure(all_highlight_names.as_slice()); + drop(all_highlight_names); Ok(Some(result)) } }) @@ -1113,13 +1114,13 @@ impl<'a> LanguageConfiguration<'a> { let (path, range) = ranges .iter() .find(|(_, range)| range.contains(&offset_within_section)) - .unwrap_or(ranges.last().unwrap()); + .unwrap_or_else(|| ranges.last().unwrap()); error.offset = offset_within_section - range.start; error.row = source[range.start..offset_within_section] .chars() .filter(|c| *c == '\n') .count(); - Error::from(error).context(format!("Error in query file {:?}", path)) + Error::from(error).context(format!("Error in query file {path:?}")) } fn read_queries( @@ -1134,7 +1135,7 @@ impl<'a> LanguageConfiguration<'a> { let abs_path = self.root_path.join(path); let prev_query_len = query.len(); 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:?}"))?; path_ranges.push((path.clone(), prev_query_len..query.len())); } } else { @@ -1142,7 +1143,7 @@ impl<'a> LanguageConfiguration<'a> { let path = queries_path.join(default_path); if path.exists() { query = fs::read_to_string(&path) - .with_context(|| format!("Failed to read query file {:?}", path))?; + .with_context(|| format!("Failed to read query file {path:?}"))?; path_ranges.push((default_path.to_string(), 0..query.len())); } }