Skip to content

Commit

Permalink
chore: clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Sep 27, 2024
1 parent b2359e4 commit 90efa34
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 43 deletions.
1 change: 1 addition & 0 deletions cli/src/generate/grammars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub struct SyntaxGrammar {

#[cfg(test)]
impl ProductionStep {
#[must_use]
pub const fn new(symbol: Symbol) -> Self {
Self {
symbol,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
1 change: 1 addition & 0 deletions cli/src/generate/nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl fmt::Debug for CharacterSet {
}

impl Nfa {
#[must_use]
pub const fn new() -> Self {
Self { states: Vec::new() }
}
Expand Down
1 change: 1 addition & 0 deletions cli/src/generate/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl From<Symbol> for Rule {
}

impl TokenSet {
#[must_use]
pub const fn new() -> Self {
Self {
terminal_bits: SmallBitVec::new(),
Expand Down
1 change: 1 addition & 0 deletions cli/src/generate/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub struct LexTable {
}

impl ParseTableEntry {
#[must_use]
pub const fn new() -> Self {
Self {
reusable: true,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
60 changes: 30 additions & 30 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}",
Expand All @@ -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())
Expand All @@ -438,7 +438,7 @@ impl Init {
.strip_prefix("tree-sitter-")
.or_else(|| Some(dir_name.as_ref()))
{
generate_grammar_files(&current_dir, language_name)?;
generate_grammar_files(current_dir, language_name)?;
}
}

Expand All @@ -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();
}
Expand All @@ -462,7 +462,7 @@ impl Generate {
}
});
generate::generate_parser_in_directory(
&current_dir,
current_dir,
self.grammar_path.as_deref(),
abi_version,
self.report_states_for_rule.as_deref(),
Expand All @@ -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(&current_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 {
Expand All @@ -491,7 +491,7 @@ impl Build {
&loader,
Some(&root_path),
&grammar_path,
&current_dir,
current_dir,
output_path,
self.docker,
)?;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(&current_dir)?;
let languages = loader.languages_at_path(current_dir)?;
let language = languages
.iter()
.find(|(_, n)| language_names.contains(&Box::from(n.as_str())))
Expand All @@ -606,7 +606,7 @@ impl Parse {
let language = if let Some(ref language) = language {
language.clone()
} else {
loader.select_language(path, &current_dir, self.scope.as_deref())?
loader.select_language(path, current_dir, self.scope.as_deref())?
};
parser
.set_language(&language)
Expand Down Expand Up @@ -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");

Expand All @@ -678,7 +678,7 @@ impl Test {
loader.use_wasm(&engine);
}

let languages = loader.languages_at_path(&current_dir)?;
let languages = loader.languages_at_path(current_dir)?;
let language = &languages
.first()
.ok_or_else(|| anyhow!("No language found"))?
Expand Down Expand Up @@ -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(&current_dir)?;
let languages = loader.languages_at_path(current_dir)?;
let (language, language_name) = &languages
.first()
.ok_or_else(|| anyhow!("No language found"))?;
Expand All @@ -806,21 +806,21 @@ impl Fuzz {
language,
language_name,
*START_SEED,
&current_dir,
current_dir,
&mut fuzz_options,
);
Ok(())
}
}

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]), &current_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| {
Expand Down Expand Up @@ -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(())
}
}
Expand Down Expand Up @@ -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(&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::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(&current_dir)?,
Commands::DumpLanguages(dump_options) => dump_options.run(loader)?,
Commands::Complete(complete_options) => complete_options.run(&mut cli),
}
Expand Down
2 changes: 2 additions & 0 deletions cli/src/query_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,12 +1060,12 @@ abc
r"(source_file (ERROR (UNEXPECTED 'f') (UNEXPECTED '+')))",
0
),
r#"
r"
(source_file
(ERROR
(UNEXPECTED 'f')
(UNEXPECTED '+')))
"#
"
.trim()
);
}
Expand Down
8 changes: 3 additions & 5 deletions cli/src/tests/helpers/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down
4 changes: 2 additions & 2 deletions cli/src/tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions cli/src/tests/query_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 90efa34

Please sign in to comment.