Rust/Ruby/Python: apply clippy lints

This commit is contained in:
Paolo Tranquilli
2025-02-25 13:21:28 +01:00
parent 6089a75262
commit 1bcc6ddb32
11 changed files with 48 additions and 44 deletions

View File

@@ -19,7 +19,7 @@ use tree_sitter_graph::Variables;
use tree_sitter_graph::ast::File;
use tree_sitter_graph::functions::Functions;
const BUILD_VERSION: &'static str = env!("CARGO_PKG_VERSION");
const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
pub mod extra_functions {
use tree_sitter_graph::functions::{Function, Parameters};
@@ -331,7 +331,7 @@ pub mod extra_functions {
None => {
return Err(ExecutionError::FunctionFailed(
"unnamed-child-index".into(),
format!("Cannot call child-index on the root node"),
"Cannot call child-index on the root node".to_string(),
));
}
};
@@ -342,7 +342,7 @@ pub mod extra_functions {
.ok_or_else(|| {
ExecutionError::FunctionFailed(
"unnamed-child-index".into(),
format!("Called child-index on a non-named child"),
"Called child-index on a non-named child".to_string(),
)
})?;
Ok(Value::Integer(index as u32))
@@ -400,7 +400,7 @@ pub mod extra_functions {
let parent = node.parent().ok_or_else(|| {
ExecutionError::FunctionFailed(
"get-parent".into(),
format!("Cannot call get-parent on the root node"),
"Cannot call get-parent on the root node".to_string(),
)
})?;
Ok(Value::SyntaxNode(graph.add_syntax_node(parent)))

View File

@@ -5,7 +5,7 @@ fn main() {
let src_dir = Path::new("src");
let mut c_config = cc::Build::new();
c_config.include(&src_dir);
c_config.include(src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
@@ -17,7 +17,7 @@ fn main() {
let mut cpp_config = cc::Build::new();
cpp_config.cpp(true);
cpp_config.include(&src_dir);
cpp_config.include(src_dir);
cpp_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable");

View File

@@ -43,18 +43,18 @@ pub fn language() -> Language {
}
/// The source of the Python tree-sitter grammar description.
pub const GRAMMAR: &'static str = include_str!("../../grammar.js");
pub const GRAMMAR: &str = include_str!("../../grammar.js");
/// The syntax highlighting query for this language.
pub const HIGHLIGHT_QUERY: &'static str = include_str!("../../queries/highlights.scm");
pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
/// The content of the [`node-types.json`][] file for this grammar.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
/// The symbol tagging query for this language.
pub const TAGGING_QUERY: &'static str = include_str!("../../queries/tags.scm");
pub const TAGGING_QUERY: &str = include_str!("../../queries/tags.scm");
#[cfg(test)]
mod tests {

View File

@@ -99,7 +99,7 @@ pub fn run(options: Options) -> std::io::Result<()> {
let mut needs_conversion = false;
let code_ranges;
let mut trap_writer = trap::Writer::new();
if path.extension().map_or(false, |x| x == "erb") {
if path.extension().is_some_and(|x| x == "erb") {
tracing::info!("scanning: {}", path.display());
extractor::extract(
&erb,
@@ -371,59 +371,59 @@ fn test_scan_coding_comment() {
assert_eq!(result, Some("utf-8".into()));
let text = "#coding:utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "# foo\n# encoding: utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, None);
let text = "# encoding: latin1 encoding: utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("latin1".into()));
let text = "# encoding: nonsense";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("nonsense".into()));
let text = "# coding = utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "# CODING = utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "# CoDiNg = utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "# blah blahblahcoding = utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
// unicode BOM is ignored
let text = "\u{FEFF}# encoding: utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "\u{FEFF} # encoding: utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "#! /usr/bin/env ruby\n # encoding: utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = "\u{FEFF}#! /usr/bin/env ruby\n # encoding: utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
// A #! must be the first thing on a line, otherwise it's a normal comment
let text = " #! /usr/bin/env ruby encoding = utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, Some("utf-8".into()));
let text = " #! /usr/bin/env ruby \n # encoding = utf-8";
let result = scan_coding_comment(&text.as_bytes());
let result = scan_coding_comment(text.as_bytes());
assert_eq!(result, None);
}

View File

@@ -124,7 +124,7 @@ impl LogWriter {
match std::fs::OpenOptions::new()
.create(true)
.append(true)
.write(true)
.open(path)
{
Err(e) => {

View File

@@ -48,7 +48,7 @@ pub enum DbColumnType {
String,
}
impl<'a> fmt::Display for Case<'a> {
impl fmt::Display for Case<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "case @{}.{} of", &self.name, &self.column)?;
let mut sep = " ";
@@ -60,7 +60,7 @@ impl<'a> fmt::Display for Case<'a> {
}
}
impl<'a> fmt::Display for Table<'a> {
impl fmt::Display for Table<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(keyset) = &self.keysets {
write!(f, "#keyset[")?;
@@ -102,7 +102,7 @@ impl<'a> fmt::Display for Table<'a> {
}
}
impl<'a> fmt::Display for Union<'a> {
impl fmt::Display for Union<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{} = ", self.name)?;
let mut first = true;

View File

@@ -8,7 +8,7 @@ pub enum TopLevel<'a> {
Module(Module<'a>),
}
impl<'a> fmt::Display for TopLevel<'a> {
impl fmt::Display for TopLevel<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TopLevel::Import(imp) => write!(f, "{}", imp),
@@ -24,7 +24,7 @@ pub struct Import<'a> {
pub alias: Option<&'a str>,
}
impl<'a> fmt::Display for Import<'a> {
impl fmt::Display for Import<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "import {}", &self.module)?;
if let Some(name) = &self.alias {
@@ -43,7 +43,7 @@ pub struct Class<'a> {
pub predicates: Vec<Predicate<'a>>,
}
impl<'a> fmt::Display for Class<'a> {
impl fmt::Display for Class<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(qldoc) = &self.qldoc {
write!(f, "/** {} */", qldoc)?;
@@ -93,7 +93,7 @@ pub struct Module<'a> {
pub body: Vec<TopLevel<'a>>,
}
impl<'a> fmt::Display for Module<'a> {
impl fmt::Display for Module<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(qldoc) = &self.qldoc {
write!(f, "/** {} */", qldoc)?;
@@ -122,7 +122,7 @@ pub enum Type<'a> {
Normal(&'a str),
}
impl<'a> fmt::Display for Type<'a> {
impl fmt::Display for Type<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Type::Int => write!(f, "int"),
@@ -152,7 +152,7 @@ pub enum Expression<'a> {
},
}
impl<'a> fmt::Display for Expression<'a> {
impl fmt::Display for Expression<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expression::Var(x) => write!(f, "{}", x),
@@ -246,7 +246,7 @@ pub struct Predicate<'a> {
pub body: Expression<'a>,
}
impl<'a> fmt::Display for Predicate<'a> {
impl fmt::Display for Predicate<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(qldoc) = &self.qldoc {
write!(f, "/** {} */", qldoc)?;
@@ -280,7 +280,7 @@ pub struct FormalParameter<'a> {
pub param_type: Type<'a>,
}
impl<'a> fmt::Display for FormalParameter<'a> {
impl fmt::Display for FormalParameter<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.param_type, self.name)
}

View File

@@ -25,6 +25,12 @@ pub struct Writer {
location_labels: std::collections::HashMap<Location, Label>,
}
impl Default for Writer {
fn default() -> Self {
Self::new()
}
}
impl Writer {
pub fn new() -> Writer {
Writer {
@@ -306,9 +312,9 @@ impl Compression {
#[test]
fn limit_string_test() {
assert_eq!("hello", limit_string(&"hello world".to_owned(), 5));
assert_eq!("hi ☹", limit_string(&"hi ☹☹".to_owned(), 6));
assert_eq!("hi ", limit_string(&"hi ☹☹".to_owned(), 5));
assert_eq!("hello", limit_string("hello world", 5));
assert_eq!("hi ☹", limit_string("hi ☹☹", 6));
assert_eq!("hi ", limit_string("hi ☹☹", 5));
}
#[test]

View File

@@ -28,7 +28,7 @@ pub fn create_source_dir(files: Vec<(&'static str, &'static str)>) -> SourceArch
let path = source_archive_dir.join(filename);
let mut file = File::create(&path).unwrap();
file.write_all(contents.as_bytes()).unwrap();
file_paths.push(PathBuf::from(path));
file_paths.push(path);
}
let file_list = {
@@ -69,5 +69,5 @@ pub fn expect_trap_file(root_dir: &Path, filename: &str) {
fn create_dir(root: &Path, path: impl AsRef<Path>) -> PathBuf {
let full_path = root.join(path);
std::fs::create_dir_all(&full_path).expect("Failed to create directory");
full_path.into()
full_path
}

View File

@@ -1,7 +1,6 @@
use codeql_extractor::extractor::simple;
use codeql_extractor::trap;
use tree_sitter_ql;
mod common;
use common::{SourceArchive, create_source_dir, expect_trap_file};

View File

@@ -1,6 +1,5 @@
use codeql_extractor::extractor::simple;
use codeql_extractor::trap;
use tree_sitter_ql;
mod common;
use common::{SourceArchive, create_source_dir, expect_trap_file};