chore: clippy

This commit is contained in:
dundargoc 2024-02-06 23:18:27 +01:00 committed by Amaan Qureshi
parent a1870b6013
commit c8bd6705cf
No known key found for this signature in database
GPG key ID: E67890ADC4227273
36 changed files with 467 additions and 462 deletions

View file

@ -30,7 +30,7 @@ struct AllocationRecorder {
}
thread_local! {
static RECORDER: AllocationRecorder = Default::default();
static RECORDER: AllocationRecorder = AllocationRecorder::default();
}
extern "C" {
@ -60,12 +60,10 @@ pub fn record<T>(f: impl FnOnce() -> T) -> T {
.map(|e| e.1)
.collect::<Vec<_>>()
});
if !outstanding_allocation_indices.is_empty() {
panic!(
"Leaked allocation indices: {:?}",
outstanding_allocation_indices
);
}
assert!(
outstanding_allocation_indices.is_empty(),
"Leaked allocation indices: {outstanding_allocation_indices:?}"
);
value
}
@ -83,9 +81,7 @@ fn record_alloc(ptr: *mut c_void) {
}
fn record_dealloc(ptr: *mut c_void) {
if ptr.is_null() {
panic!("Zero pointer deallocation!");
}
assert!(!ptr.is_null(), "Zero pointer deallocation!");
RECORDER.with(|recorder| {
if recorder.enabled.load(SeqCst) {
recorder

View file

@ -39,7 +39,7 @@ lazy_static! {
"unknown"
};
let machine = format!("{}-{}-{}-{}-{}", std::env::consts::ARCH, std::env::consts::OS, vendor, env, endian);
let machine = format!("{}-{}-{vendor}-{env}-{endian}", std::env::consts::ARCH, std::env::consts::OS);
let result = SCRATCH_BASE_DIR.join(machine);
fs::create_dir_all(&result).unwrap();
result

View file

@ -10,7 +10,8 @@ pub struct ReadRecorder<'a> {
}
impl<'a> ReadRecorder<'a> {
pub fn new(content: &'a Vec<u8>) -> Self {
#[must_use]
pub const fn new(content: &'a Vec<u8>) -> Self {
Self {
content,
indices_read: Vec::new(),
@ -31,7 +32,7 @@ impl<'a> ReadRecorder<'a> {
pub fn strings_read(&self) -> Vec<&'a str> {
let mut result = Vec::new();
let mut last_range: Option<Range<usize>> = None;
for index in self.indices_read.iter() {
for index in &self.indices_read {
if let Some(ref mut range) = &mut last_range {
if range.end == *index {
range.end += 1;
@ -44,13 +45,13 @@ impl<'a> ReadRecorder<'a> {
}
}
if let Some(range) = last_range {
result.push(str::from_utf8(&self.content[range.clone()]).unwrap());
result.push(str::from_utf8(&self.content[range]).unwrap());
}
result
}
}
pub fn invert_edit(input: &Vec<u8>, edit: &Edit) -> Edit {
pub fn invert_edit(input: &[u8], edit: &Edit) -> Edit {
let position = edit.position;
let removed_content = &input[position..(position + edit.deleted_length)];
Edit {
@ -60,7 +61,7 @@ pub fn invert_edit(input: &Vec<u8>, edit: &Edit) -> Edit {
}
}
pub fn get_random_edit(rand: &mut Rand, input: &Vec<u8>) -> Edit {
pub fn get_random_edit(rand: &mut Rand, input: &[u8]) -> Edit {
let choice = rand.unsigned(10);
if choice < 2 {
// Insert text at end

View file

@ -19,11 +19,11 @@ lazy_static! {
};
}
pub fn test_loader<'a>() -> &'a Loader {
&*TEST_LOADER
pub fn test_loader() -> &'static Loader {
&TEST_LOADER
}
pub fn fixtures_dir<'a>() -> &'static Path {
pub fn fixtures_dir() -> &'static Path {
&FIXTURES_DIR
}
@ -48,12 +48,11 @@ pub fn get_highlight_config(
let language = get_language(language_name);
let queries_path = get_language_queries_path(language_name);
let highlights_query = fs::read_to_string(queries_path.join("highlights.scm")).unwrap();
let injections_query = if let Some(injection_query_filename) = injection_query_filename {
fs::read_to_string(queries_path.join(injection_query_filename)).unwrap()
} else {
String::new()
};
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or(String::new());
let injections_query =
injection_query_filename.map_or_else(String::new, |injection_query_filename| {
fs::read_to_string(queries_path.join(injection_query_filename)).unwrap()
});
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or_default();
let mut result = HighlightConfiguration::new(
language,
language_name,
@ -63,7 +62,7 @@ pub fn get_highlight_config(
false,
)
.unwrap();
result.configure(&highlight_names);
result.configure(highlight_names);
result
}
@ -71,7 +70,7 @@ pub fn get_tags_config(language_name: &str) -> TagsConfiguration {
let language = get_language(language_name);
let queries_path = get_language_queries_path(language_name);
let tags_query = fs::read_to_string(queries_path.join("tags.scm")).unwrap();
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or(String::new());
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or_default();
TagsConfiguration::new(language, &tags_query, &locals_query).unwrap()
}
@ -99,7 +98,7 @@ 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();
fs::write(&header_path.join("parser.h"), tree_sitter::PARSER_HEADER)
fs::write(header_path.join("parser.h"), tree_sitter::PARSER_HEADER)
.with_context(|| {
format!(
"Failed to write {:?}",

View file

@ -26,7 +26,7 @@ fn int_env_var(name: &'static str) -> Option<usize> {
env::var(name).ok().and_then(|e| e.parse().ok())
}
pub(crate) fn new_seed() -> usize {
pub fn new_seed() -> usize {
int_env_var("TREE_SITTER_SEED").unwrap_or_else(|| {
let mut rng = rand::thread_rng();
rng.gen::<usize>()

View file

@ -19,7 +19,7 @@ pub struct Match<'a, 'tree> {
pub last_node: Option<Node<'tree>>,
}
const CAPTURE_NAMES: &'static [&'static str] = &[
const CAPTURE_NAMES: &[&str] = &[
"one", "two", "three", "four", "five", "six", "seven", "eight",
];
@ -57,12 +57,11 @@ impl Pattern {
children: roots,
};
if pattern.children.len() == 1 {
pattern = pattern.children.pop().unwrap();
}
if pattern.children.len() == 1 ||
// In a parenthesized list of sibling patterns, the first
// sibling can't be an anonymous `_` wildcard.
else if pattern.children[0].kind == Some("_") && !pattern.children[0].named {
(pattern.children[0].kind == Some("_") && !pattern.children[0].named)
{
pattern = pattern.children.pop().unwrap();
}
// In a parenthesized list of sibling patterns, the first
@ -123,22 +122,16 @@ impl Pattern {
}
}
pub fn to_string(&self) -> String {
let mut result = String::new();
self.write_to_string(&mut result, 0);
result
}
fn write_to_string(&self, string: &mut String, indent: usize) {
if let Some(field) = self.field {
write!(string, "{}: ", field).unwrap();
write!(string, "{field}: ").unwrap();
}
if self.named {
string.push('(');
let mut has_contents = false;
if let Some(kind) = &self.kind {
write!(string, "{}", kind).unwrap();
write!(string, "{kind}").unwrap();
has_contents = true;
}
for child in &self.children {
@ -154,11 +147,11 @@ impl Pattern {
} else if self.kind == Some("_") {
string.push('_');
} else {
write!(string, "\"{}\"", self.kind.unwrap().replace("\"", "\\\"")).unwrap();
write!(string, "\"{}\"", self.kind.unwrap().replace('\"', "\\\"")).unwrap();
}
if let Some(capture) = &self.capture {
write!(string, " @{}", capture).unwrap();
write!(string, " @{capture}").unwrap();
}
}
@ -214,11 +207,10 @@ impl Pattern {
// Create a match for the current node.
let mat = Match {
captures: if let Some(name) = &self.capture {
vec![(name.as_str(), node)]
} else {
Vec::new()
},
captures: self
.capture
.as_ref()
.map_or_else(Vec::new, |name| vec![(name.as_str(), node)]),
last_node: Some(node),
};
@ -246,7 +238,7 @@ impl Pattern {
new_match_states.push((*pattern_index + 1, combined_match));
} else {
let mut existing = false;
for existing_match in finished_matches.iter_mut() {
for existing_match in &mut finished_matches {
if existing_match.captures == combined_match.captures {
if child_pattern.capture.is_some() {
existing_match.last_node = combined_match.last_node;
@ -271,6 +263,14 @@ impl Pattern {
}
}
impl ToString for Pattern {
fn to_string(&self) -> String {
let mut result = String::new();
self.write_to_string(&mut result, 0);
result
}
}
impl<'a, 'tree> PartialOrd for Match<'a, 'tree> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
@ -314,11 +314,11 @@ pub fn assert_query_matches(
expected: &[(usize, Vec<(&str, &str)>)],
) {
let mut parser = Parser::new();
parser.set_language(&language).unwrap();
parser.set_language(language).unwrap();
let tree = parser.parse(source, None).unwrap();
let mut cursor = QueryCursor::new();
let matches = cursor.matches(&query, tree.root_node(), source.as_bytes());
pretty_assertions::assert_eq!(collect_matches(matches, &query, source), expected);
let matches = cursor.matches(query, tree.root_node(), source.as_bytes());
pretty_assertions::assert_eq!(collect_matches(matches, query, source), expected);
pretty_assertions::assert_eq!(cursor.did_exceed_match_limit(), false);
}
@ -331,7 +331,7 @@ pub fn collect_matches<'a>(
.map(|m| {
(
m.pattern_index,
format_captures(m.captures.iter().cloned(), query, source),
format_captures(m.captures.iter().copied(), query, source),
)
})
.collect()

View file

@ -11,7 +11,7 @@ pub struct Rand(StdRng);
impl Rand {
pub fn new(seed: usize) -> Self {
Rand(StdRng::seed_from_u64(seed as u64))
Self(StdRng::seed_from_u64(seed as u64))
}
pub fn unsigned(&mut self, max: usize) -> usize {
@ -24,9 +24,9 @@ impl Rand {
for i in 0..word_count {
if i > 0 {
if self.unsigned(5) == 0 {
result.push('\n' as u8);
result.push(b'\n');
} else {
result.push(' ' as u8);
result.push(b' ');
}
}
if self.unsigned(3) == 0 {
@ -34,7 +34,7 @@ impl Rand {
result.push(OPERATORS[index] as u8);
} else {
for _ in 0..self.unsigned(8) {
result.push(self.0.sample(Alphanumeric) as u8);
result.push(self.0.sample(Alphanumeric));
}
}
}

View file

@ -7,7 +7,7 @@ type ScopeStack = Vec<&'static str>;
impl ScopeSequence {
pub fn new(tree: &Tree) -> Self {
let mut result = ScopeSequence(Vec::new());
let mut result = Self(Vec::new());
let mut scope_stack = Vec::new();
let mut cursor = tree.walk();
@ -40,9 +40,9 @@ impl ScopeSequence {
pub fn check_changes(
&self,
other: &ScopeSequence,
text: &Vec<u8>,
known_changed_ranges: &Vec<Range>,
other: &Self,
text: &[u8],
known_changed_ranges: &[Range],
) -> Result<(), String> {
let mut position = Point { row: 0, column: 0 };
for i in 0..(self.0.len().max(other.0.len())) {
@ -54,7 +54,7 @@ impl ScopeSequence {
.find(|range| range.start_point <= position && position < range.end_point);
if containing_range.is_none() {
let line = &text[(i - position.column)..]
.split(|c| *c == '\n' as u8)
.split(|c| *c == b'\n')
.next()
.unwrap();
return Err(format!(
@ -78,7 +78,7 @@ impl ScopeSequence {
}
}
if text[i] == '\n' as u8 {
if text[i] == b'\n' {
position.row += 1;
position.column = 0;
} else {