Skip to content

Commit b955fdb

Browse files
authored
Merge pull request #324 from github/clippy_fixes
clippy fixes
2 parents 2f46277 + 46d1280 commit b955fdb

File tree

8 files changed

+139
-158
lines changed

8 files changed

+139
-158
lines changed

autobuilder/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ fn main() -> std::io::Result<()> {
2626

2727
for line in env::var("LGTM_INDEX_FILTERS")
2828
.unwrap_or_default()
29-
.split("\n")
29+
.split('\n')
3030
{
31-
if line.starts_with("include:") {
32-
cmd.arg("--include").arg(&line[8..]);
33-
} else if line.starts_with("exclude:") {
34-
cmd.arg("--exclude").arg(&line[8..]);
31+
if let Some(stripped) = line.strip_prefix("include:") {
32+
cmd.arg("--include").arg(stripped);
33+
} else if let Some(stripped) = line.strip_prefix("exclude:") {
34+
cmd.arg("--exclude").arg(stripped);
3535
}
3636
}
3737
let exit = &cmd.spawn()?.wait()?;

extractor/src/extractor.rs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub fn extract(
165165
schema: &NodeTypeMap,
166166
trap_writer: &mut TrapWriter,
167167
path: &Path,
168-
source: &Vec<u8>,
168+
source: &[u8],
169169
ranges: &[Range],
170170
) -> std::io::Result<()> {
171171
let span = span!(
@@ -180,13 +180,13 @@ pub fn extract(
180180

181181
let mut parser = Parser::new();
182182
parser.set_language(language).unwrap();
183-
parser.set_included_ranges(&ranges).unwrap();
183+
parser.set_included_ranges(ranges).unwrap();
184184
let tree = parser.parse(&source, None).expect("Failed to parse file");
185185
trap_writer.comment(format!("Auto-generated TRAP file for {}", path.display()));
186186
let file_label = &trap_writer.populate_file(path);
187187
let mut visitor = Visitor {
188-
source: &source,
189-
trap_writer: trap_writer,
188+
source,
189+
trap_writer,
190190
// TODO: should we handle path strings that are not valid UTF8 better?
191191
path: format!("{}", path.display()),
192192
file_label: *file_label,
@@ -205,15 +205,7 @@ pub fn extract(
205205
/// HTML entities.
206206
fn escape_key<'a, S: Into<Cow<'a, str>>>(key: S) -> Cow<'a, str> {
207207
fn needs_escaping(c: char) -> bool {
208-
match c {
209-
'&' => true,
210-
'{' => true,
211-
'}' => true,
212-
'"' => true,
213-
'@' => true,
214-
'#' => true,
215-
_ => false,
216-
}
208+
matches!(c, '&' | '{' | '}' | '"' | '@' | '#')
217209
}
218210

219211
let key = key.into();
@@ -296,7 +288,7 @@ struct Visitor<'a> {
296288
/// source file.
297289
file_label: Label,
298290
/// The source code as a UTF-8 byte array
299-
source: &'a Vec<u8>,
291+
source: &'a [u8],
300292
/// A TrapWriter to accumulate trap entries
301293
trap_writer: &'a mut TrapWriter,
302294
/// A counter for top-level child nodes
@@ -342,7 +334,7 @@ impl Visitor<'_> {
342334
full_error_message: String,
343335
node: Node,
344336
) {
345-
let (start_line, start_column, end_line, end_column) = location_for(&self.source, node);
337+
let (start_line, start_column, end_line, end_column) = location_for(self.source, node);
346338
let loc = self.trap_writer.location(
347339
self.file_label,
348340
start_line,
@@ -373,15 +365,15 @@ impl Visitor<'_> {
373365
let id = self.trap_writer.fresh_id();
374366

375367
self.stack.push((id, 0, Vec::new()));
376-
return true;
368+
true
377369
}
378370

379371
fn leave_node(&mut self, field_name: Option<&'static str>, node: Node) {
380372
if node.is_error() || node.is_missing() {
381373
return;
382374
}
383375
let (id, _, child_nodes) = self.stack.pop().expect("Vistor: empty stack");
384-
let (start_line, start_column, end_line, end_column) = location_for(&self.source, node);
376+
let (start_line, start_column, end_line, end_column) = location_for(self.source, node);
385377
let loc = self.trap_writer.location(
386378
self.file_label,
387379
start_line,
@@ -440,11 +432,10 @@ impl Visitor<'_> {
440432
Arg::Int(parent_index),
441433
],
442434
);
443-
let mut all_args = Vec::new();
444-
all_args.push(Arg::Label(id));
435+
let mut all_args = vec![Arg::Label(id)];
445436
all_args.extend(args);
446437
all_args.push(Arg::Label(loc));
447-
self.trap_writer.add_tuple(&table_name, all_args);
438+
self.trap_writer.add_tuple(table_name, all_args);
448439
}
449440
}
450441
_ => {
@@ -479,8 +470,8 @@ impl Visitor<'_> {
479470
fn complex_node(
480471
&mut self,
481472
node: &Node,
482-
fields: &Vec<Field>,
483-
child_nodes: &Vec<ChildNode>,
473+
fields: &[Field],
474+
child_nodes: &[ChildNode],
484475
parent_id: Label,
485476
) -> Option<Vec<Arg>> {
486477
let mut map: Map<&Option<String>, (&Field, Vec<Arg>)> = Map::new();
@@ -517,22 +508,20 @@ impl Visitor<'_> {
517508
);
518509
self.record_parse_error_for_node(error_message, full_error_message, *node);
519510
}
520-
} else {
521-
if child_node.field_name.is_some() || child_node.type_name.named {
522-
let error_message = format!(
523-
"value for unknown field: {}::{} and type {:?}",
524-
node.kind(),
525-
&child_node.field_name.unwrap_or("child"),
526-
&child_node.type_name
527-
);
528-
let full_error_message = format!(
529-
"{}:{}: {}",
530-
&self.path,
531-
node.start_position().row + 1,
532-
error_message
533-
);
534-
self.record_parse_error_for_node(error_message, full_error_message, *node);
535-
}
511+
} else if child_node.field_name.is_some() || child_node.type_name.named {
512+
let error_message = format!(
513+
"value for unknown field: {}::{} and type {:?}",
514+
node.kind(),
515+
&child_node.field_name.unwrap_or("child"),
516+
&child_node.type_name
517+
);
518+
let full_error_message = format!(
519+
"{}:{}: {}",
520+
&self.path,
521+
node.start_position().row + 1,
522+
error_message
523+
);
524+
self.record_parse_error_for_node(error_message, full_error_message, *node);
536525
}
537526
}
538527
let mut args = Vec::new();
@@ -580,13 +569,12 @@ impl Visitor<'_> {
580569
);
581570
break;
582571
}
583-
let mut args = Vec::new();
584-
args.push(Arg::Label(parent_id));
572+
let mut args = vec![Arg::Label(parent_id)];
585573
if *has_index {
586574
args.push(Arg::Int(index))
587575
}
588576
args.push(child_value.clone());
589-
self.trap_writer.add_tuple(&table_name, args);
577+
self.trap_writer.add_tuple(table_name, args);
590578
}
591579
}
592580
}
@@ -604,13 +592,10 @@ impl Visitor<'_> {
604592
if tp == single_type {
605593
return true;
606594
}
607-
match &self.schema.get(single_type).unwrap().kind {
608-
EntryKind::Union { members } => {
609-
if self.type_matches_set(tp, members) {
610-
return true;
611-
}
595+
if let EntryKind::Union { members } = &self.schema.get(single_type).unwrap().kind {
596+
if self.type_matches_set(tp, members) {
597+
return true;
612598
}
613-
_ => {}
614599
}
615600
}
616601
node_types::FieldTypeInfo::Multiple { types, .. } => {
@@ -640,15 +625,15 @@ impl Visitor<'_> {
640625
}
641626

642627
// Emit a slice of a source file as an Arg.
643-
fn sliced_source_arg(source: &Vec<u8>, n: Node) -> Arg {
628+
fn sliced_source_arg(source: &[u8], n: Node) -> Arg {
644629
let range = n.byte_range();
645630
Arg::String(String::from_utf8_lossy(&source[range.start..range.end]).into_owned())
646631
}
647632

648633
// Emit a pair of `TrapEntry`s for the provided node, appropriately calibrated.
649634
// The first is the location and label definition, and the second is the
650635
// 'Located' entry.
651-
fn location_for<'a>(source: &Vec<u8>, n: Node) -> (usize, usize, usize, usize) {
636+
fn location_for(source: &[u8], n: Node) -> (usize, usize, usize, usize) {
652637
// Tree-sitter row, column values are 0-based while CodeQL starts
653638
// counting at 1. In addition Tree-sitter's row and column for the
654639
// end position are exclusive while CodeQL's end positions are inclusive.
@@ -806,7 +791,7 @@ impl fmt::Display for Arg {
806791
/// the string is sliced at the provided limit. If there is a multi-byte character
807792
/// at the limit then the returned slice will be slightly shorter than the limit to
808793
/// avoid splitting that multi-byte character.
809-
fn limit_string(string: &String, max_size: usize) -> &str {
794+
fn limit_string(string: &str, max_size: usize) -> &str {
810795
if string.len() <= max_size {
811796
return string;
812797
}
@@ -817,7 +802,7 @@ fn limit_string(string: &String, max_size: usize) -> &str {
817802
// encoded data any byte that matches the bit pattern 10XXXXXX is not a start byte.
818803
// Therefore we decrement the index as long as there are bytes matching this pattern.
819804
// This ensures we cut the string at the border between one character and another.
820-
while index > 0 && unsafe { (*p.offset(index as isize) & 0b11000000) == 0b10000000 } {
805+
while index > 0 && unsafe { (*p.add(index) & 0b11000000) == 0b10000000 } {
821806
index -= 1;
822807
}
823808
&string[0..index]

extractor/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod extractor;
22

33
extern crate num_cpus;
44

5-
use clap;
65
use flate2::write::GzEncoder;
76
use rayon::prelude::*;
87
use std::fs;
@@ -57,7 +56,7 @@ impl TrapCompression {
5756
* (minimum of 1). If unspecified, should be considered as set to -1."
5857
*/
5958
fn num_codeql_threads() -> usize {
60-
let threads_str = std::env::var("CODEQL_THREADS").unwrap_or("-1".to_owned());
59+
let threads_str = std::env::var("CODEQL_THREADS").unwrap_or_else(|_| "-1".to_owned());
6160
match threads_str.parse::<i32>() {
6261
Ok(num) if num <= 0 => {
6362
let reduction = -num as usize;
@@ -191,12 +190,12 @@ fn main() -> std::io::Result<()> {
191190
}
192191

193192
fn write_trap(
194-
trap_dir: &PathBuf,
193+
trap_dir: &Path,
195194
path: PathBuf,
196195
trap_writer: extractor::TrapWriter,
197196
trap_compression: &TrapCompression,
198197
) -> std::io::Result<()> {
199-
let trap_file = path_for(&trap_dir, &path, trap_compression.extension());
198+
let trap_file = path_for(trap_dir, &path, trap_compression.extension());
200199
std::fs::create_dir_all(&trap_file.parent().unwrap())?;
201200
let trap_file = std::fs::File::create(&trap_file)?;
202201
let mut trap_file = BufWriter::new(trap_file);
@@ -211,7 +210,7 @@ fn write_trap(
211210

212211
fn scan_erb(
213212
erb: Language,
214-
source: &Vec<u8>,
213+
source: &[u8],
215214
directive_id: u16,
216215
output_directive_id: u16,
217216
code_id: u16,
@@ -238,7 +237,7 @@ fn scan_erb(
238237
}
239238
}
240239
}
241-
if result.len() == 0 {
240+
if result.is_empty() {
242241
let root = tree.root_node();
243242
// Add an empty range at the end of the file
244243
result.push(Range {

generator/src/dbscheme.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ impl<'a> fmt::Display for Table<'a> {
6868
}
6969
write!(f, "{}", key)?;
7070
}
71-
write!(f, "]\n")?;
71+
writeln!(f, "]")?;
7272
}
7373

74-
write!(f, "{}(\n", self.name)?;
74+
writeln!(f, "{}(", self.name)?;
7575
for (column_index, column) in self.columns.iter().enumerate() {
7676
write!(f, " ")?;
7777
if column.unique {
@@ -92,7 +92,7 @@ impl<'a> fmt::Display for Table<'a> {
9292
if column_index + 1 != self.columns.len() {
9393
write!(f, ",")?;
9494
}
95-
write!(f, "\n")?;
95+
writeln!(f)?;
9696
}
9797
write!(f, ");")?;
9898

0 commit comments

Comments
 (0)