From 90efa346081013ab76697859790ee53304dcef0a Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Fri, 27 Sep 2024 15:42:38 -0400 Subject: [PATCH] chore: clippy fixes --- cli/src/generate/grammars.rs | 1 + cli/src/generate/mod.rs | 2 +- cli/src/generate/nfa.rs | 1 + cli/src/generate/rules.rs | 1 + cli/src/generate/tables.rs | 1 + cli/src/highlight.rs | 2 +- cli/src/main.rs | 60 +++++++++++++++---------------- cli/src/query_testing.rs | 2 ++ cli/src/test.rs | 4 +-- cli/src/tests/helpers/fixtures.rs | 8 ++--- cli/src/tests/parser_test.rs | 4 +-- cli/src/tests/query_test.rs | 4 +-- 12 files changed, 47 insertions(+), 43 deletions(-) diff --git a/cli/src/generate/grammars.rs b/cli/src/generate/grammars.rs index 1f3b9070..fab07afb 100644 --- a/cli/src/generate/grammars.rs +++ b/cli/src/generate/grammars.rs @@ -108,6 +108,7 @@ pub struct SyntaxGrammar { #[cfg(test)] impl ProductionStep { + #[must_use] pub const fn new(symbol: Symbol) -> Self { Self { symbol, diff --git a/cli/src/generate/mod.rs b/cli/src/generate/mod.rs index 88652574..f3b09339 100644 --- a/cli/src/generate/mod.rs +++ b/cli/src/generate/mod.rs @@ -67,7 +67,7 @@ pub fn generate_parser_in_directory( let grammar_path = grammar_path .map(PathBuf::from) - .unwrap_or(repo_path.join("grammar.js")); + .unwrap_or_else(|| repo_path.join("grammar.js")); // Read the grammar file. let grammar_json = load_grammar_file(&grammar_path, js_runtime)?; diff --git a/cli/src/generate/nfa.rs b/cli/src/generate/nfa.rs index 6f0e1ee8..1fc86b35 100644 --- a/cli/src/generate/nfa.rs +++ b/cli/src/generate/nfa.rs @@ -426,6 +426,7 @@ impl fmt::Debug for CharacterSet { } impl Nfa { + #[must_use] pub const fn new() -> Self { Self { states: Vec::new() } } diff --git a/cli/src/generate/rules.rs b/cli/src/generate/rules.rs index d8124ef4..7657df88 100644 --- a/cli/src/generate/rules.rs +++ b/cli/src/generate/rules.rs @@ -272,6 +272,7 @@ impl From for Rule { } impl TokenSet { + #[must_use] pub const fn new() -> Self { Self { terminal_bits: SmallBitVec::new(), diff --git a/cli/src/generate/tables.rs b/cli/src/generate/tables.rs index 541a301c..940fd31a 100644 --- a/cli/src/generate/tables.rs +++ b/cli/src/generate/tables.rs @@ -92,6 +92,7 @@ pub struct LexTable { } impl ParseTableEntry { + #[must_use] pub const fn new() -> Self { Self { reusable: true, diff --git a/cli/src/highlight.rs b/cli/src/highlight.rs index dc7216a9..41e07850 100644 --- a/cli/src/highlight.rs +++ b/cli/src/highlight.rs @@ -300,7 +300,7 @@ fn write_color(buffer: &mut String, color: Color) { _ => unreachable!(), }, Color::Ansi256(Ansi256Color(n)) => { - write!(buffer, "color: {}", CSS_STYLES_BY_COLOR_ID[n as usize]).unwrap() + write!(buffer, "color: {}", CSS_STYLES_BY_COLOR_ID[n as usize]).unwrap(); } Color::Rgb(RgbColor(r, g, b)) => write!(buffer, "color: #{r:02x}{g:02x}{b:02x}").unwrap(), } diff --git a/cli/src/main.rs b/cli/src/main.rs index 41cc15d7..206494ba 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -409,7 +409,7 @@ struct Complete { } impl InitConfig { - fn run(self) -> Result<()> { + fn run() -> Result<()> { if let Ok(Some(config_path)) = Config::find_config_file() { return Err(anyhow!( "Remove your existing config file first: {}", @@ -429,7 +429,7 @@ impl InitConfig { } impl Init { - fn run(self, current_dir: PathBuf) -> Result<()> { + fn run(current_dir: &Path) -> Result<()> { if let Some(dir_name) = current_dir .file_name() .map(|x| x.to_string_lossy().to_ascii_lowercase()) @@ -438,7 +438,7 @@ impl Init { .strip_prefix("tree-sitter-") .or_else(|| Some(dir_name.as_ref())) { - generate_grammar_files(¤t_dir, language_name)?; + generate_grammar_files(current_dir, language_name)?; } } @@ -447,7 +447,7 @@ impl Init { } impl Generate { - fn run(self, mut loader: loader::Loader, current_dir: PathBuf) -> Result<()> { + fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { if self.log { logger::init(); } @@ -462,7 +462,7 @@ impl Generate { } }); generate::generate_parser_in_directory( - ¤t_dir, + current_dir, self.grammar_path.as_deref(), abi_version, self.report_states_for_rule.as_deref(), @@ -473,14 +473,14 @@ impl Generate { loader = loader::Loader::with_parser_lib_path(PathBuf::from(path)); } loader.debug_build(self.debug_build); - loader.languages_at_path(¤t_dir)?; + loader.languages_at_path(current_dir)?; } Ok(()) } } impl Build { - fn run(self, mut loader: loader::Loader, current_dir: PathBuf) -> Result<()> { + fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { let grammar_path = current_dir.join(self.path.as_deref().unwrap_or_default()); if self.wasm { @@ -491,7 +491,7 @@ impl Build { &loader, Some(&root_path), &grammar_path, - ¤t_dir, + current_dir, output_path, self.docker, )?; @@ -537,7 +537,7 @@ impl Build { } impl Parse { - fn run(self, mut loader: loader::Loader, current_dir: PathBuf) -> Result<()> { + fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { let config = Config::load(self.config_path)?; let color = env::var("NO_COLOR").map_or(true, |v| v != "1"); let output = if self.output_dot { @@ -581,7 +581,7 @@ impl Parse { let (paths, language) = if let Some(target_test) = self.test_number { let (test_path, language_names) = test::get_tmp_test_file(target_test, color)?; - let languages = loader.languages_at_path(¤t_dir)?; + let languages = loader.languages_at_path(current_dir)?; let language = languages .iter() .find(|(_, n)| language_names.contains(&Box::from(n.as_str()))) @@ -606,7 +606,7 @@ impl Parse { let language = if let Some(ref language) = language { language.clone() } else { - loader.select_language(path, ¤t_dir, self.scope.as_deref())? + loader.select_language(path, current_dir, self.scope.as_deref())? }; parser .set_language(&language) @@ -660,7 +660,7 @@ impl Parse { } impl Test { - fn run(self, mut loader: loader::Loader, current_dir: PathBuf) -> Result<()> { + fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { let config = Config::load(self.config_path)?; let color = env::var("NO_COLOR").map_or(true, |v| v != "1"); @@ -678,7 +678,7 @@ impl Test { loader.use_wasm(&engine); } - let languages = loader.languages_at_path(¤t_dir)?; + let languages = loader.languages_at_path(current_dir)?; let language = &languages .first() .ok_or_else(|| anyhow!("No language found"))? @@ -782,11 +782,11 @@ impl Test { } impl Fuzz { - fn run(self, mut loader: loader::Loader, current_dir: PathBuf) -> Result<()> { + fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { loader.sanitize_build(true); loader.force_rebuild(self.rebuild); - let languages = loader.languages_at_path(¤t_dir)?; + let languages = loader.languages_at_path(current_dir)?; let (language, language_name) = &languages .first() .ok_or_else(|| anyhow!("No language found"))?; @@ -806,7 +806,7 @@ impl Fuzz { language, language_name, *START_SEED, - ¤t_dir, + current_dir, &mut fuzz_options, ); Ok(()) @@ -814,13 +814,13 @@ impl Fuzz { } impl Query { - fn run(self, mut loader: loader::Loader, current_dir: PathBuf) -> Result<()> { + fn run(self, mut loader: loader::Loader, current_dir: &Path) -> Result<()> { let config = Config::load(self.config_path)?; let paths = collect_paths(self.paths_file.as_deref(), self.paths)?; let loader_config = config.get()?; loader.find_all_languages(&loader_config)?; let language = - loader.select_language(Path::new(&paths[0]), ¤t_dir, self.scope.as_deref())?; + loader.select_language(Path::new(&paths[0]), current_dir, self.scope.as_deref())?; let query_path = Path::new(&self.query_path); let byte_range = self.byte_range.as_ref().and_then(|range| { @@ -980,10 +980,10 @@ impl Tags { } impl Playground { - fn run(self, current_dir: PathBuf) -> Result<()> { + fn run(self, current_dir: &Path) -> Result<()> { let open_in_browser = !self.quiet; - let grammar_path = self.grammar_path.map_or(current_dir, PathBuf::from); - playground::serve(&grammar_path, open_in_browser)?; + let grammar_path = self.grammar_path.as_deref().map_or(current_dir, Path::new); + playground::serve(grammar_path, open_in_browser)?; Ok(()) } } @@ -1072,17 +1072,17 @@ fn run() -> Result<()> { let loader = loader::Loader::new()?; match command { - Commands::InitConfig(init_config) => init_config.run()?, - Commands::Init(init) => init.run(current_dir)?, - Commands::Generate(generate_options) => generate_options.run(loader, current_dir)?, - Commands::Build(build_options) => build_options.run(loader, current_dir)?, - Commands::Parse(parse_options) => parse_options.run(loader, current_dir)?, - Commands::Test(test_options) => test_options.run(loader, current_dir)?, - Commands::Fuzz(fuzz_options) => fuzz_options.run(loader, current_dir)?, - Commands::Query(query_options) => query_options.run(loader, current_dir)?, + Commands::InitConfig(_) => InitConfig::run()?, + Commands::Init(_) => Init::run(¤t_dir)?, + Commands::Generate(generate_options) => generate_options.run(loader, ¤t_dir)?, + Commands::Build(build_options) => build_options.run(loader, ¤t_dir)?, + Commands::Parse(parse_options) => parse_options.run(loader, ¤t_dir)?, + Commands::Test(test_options) => test_options.run(loader, ¤t_dir)?, + Commands::Fuzz(fuzz_options) => fuzz_options.run(loader, ¤t_dir)?, + Commands::Query(query_options) => query_options.run(loader, ¤t_dir)?, Commands::Highlight(highlight_options) => highlight_options.run(loader)?, Commands::Tags(tags_options) => tags_options.run(loader)?, - Commands::Playground(playground_options) => playground_options.run(current_dir)?, + Commands::Playground(playground_options) => playground_options.run(¤t_dir)?, Commands::DumpLanguages(dump_options) => dump_options.run(loader)?, Commands::Complete(complete_options) => complete_options.run(&mut cli), } diff --git a/cli/src/query_testing.rs b/cli/src/query_testing.rs index 2791d815..81ea18fb 100644 --- a/cli/src/query_testing.rs +++ b/cli/src/query_testing.rs @@ -23,11 +23,13 @@ impl std::fmt::Display for Utf8Point { } impl Utf8Point { + #[must_use] pub const fn new(row: usize, column: usize) -> Self { Self { row, column } } } +#[must_use] pub fn to_utf8_point(point: Point, source: &[u8]) -> Utf8Point { if point.column == 0 { return Utf8Point::new(point.row, 0); diff --git a/cli/src/test.rs b/cli/src/test.rs index dbff512e..ffabcc2c 100644 --- a/cli/src/test.rs +++ b/cli/src/test.rs @@ -1060,12 +1060,12 @@ abc r"(source_file (ERROR (UNEXPECTED 'f') (UNEXPECTED '+')))", 0 ), - r#" + r" (source_file (ERROR (UNEXPECTED 'f') (UNEXPECTED '+'))) -"# +" .trim() ); } diff --git a/cli/src/tests/helpers/fixtures.rs b/cli/src/tests/helpers/fixtures.rs index 8ecaa38e..594c2575 100644 --- a/cli/src/tests/helpers/fixtures.rs +++ b/cli/src/tests/helpers/fixtures.rs @@ -110,18 +110,16 @@ pub fn get_test_language(name: &str, parser_code: &str, path: Option<&Path>) -> let header_path = src_dir.join("tree_sitter"); fs::create_dir_all(&header_path).unwrap(); - [ + for (file, content) in [ ("alloc.h", ALLOC_HEADER), ("array.h", ARRAY_HEADER), ("parser.h", tree_sitter::PARSER_HEADER), - ] - .iter() - .for_each(|(file, content)| { + ] { let file = header_path.join(file); fs::write(&file, content) .with_context(|| format!("Failed to write {:?}", file.file_name().unwrap())) .unwrap(); - }); + } let paths_to_check = if let Some(scanner_path) = &scanner_path { vec![parser_path, scanner_path.clone()] diff --git a/cli/src/tests/parser_test.rs b/cli/src/tests/parser_test.rs index 7a857002..14cc9e3c 100644 --- a/cli/src/tests/parser_test.rs +++ b/cli/src/tests/parser_test.rs @@ -1391,7 +1391,7 @@ fn test_grammars_that_can_hang_on_eof() { #[test] fn test_parse_stack_recursive_merge_error_cost_calculation_bug() { - let source_code = r#" + let source_code = r" fn main() { if n == 1 { } else if n == 2 { @@ -1404,7 +1404,7 @@ let y = if x == 5 { 10 } else { 15 }; if foo && bar {} if foo && bar || baz {} -"#; +"; let mut parser = Parser::new(); parser.set_language(&get_language("rust")).unwrap(); diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index d404d19a..4f5ddd75 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -3918,7 +3918,7 @@ fn test_query_random() { .matches( &query, test_tree.root_node(), - (include_str!("parser_test.rs")).as_bytes(), + include_bytes!("parser_test.rs").as_ref(), ) .map(|mat| Match { last_node: None, @@ -5137,7 +5137,7 @@ fn test_query_wildcard_with_immediate_first_child() { fn test_query_on_empty_source_code() { let language = get_language("javascript"); let source_code = ""; - let query = r#"(program) @program"#; + let query = "(program) @program"; let query = Query::new(&language, query).unwrap(); assert_query_matches( &language,