@@ -165,7 +165,7 @@ pub fn extract(
165
165
schema : & NodeTypeMap ,
166
166
trap_writer : & mut TrapWriter ,
167
167
path : & Path ,
168
- source : & Vec < u8 > ,
168
+ source : & [ u8 ] ,
169
169
ranges : & [ Range ] ,
170
170
) -> std:: io:: Result < ( ) > {
171
171
let span = span ! (
@@ -180,13 +180,13 @@ pub fn extract(
180
180
181
181
let mut parser = Parser :: new ( ) ;
182
182
parser. set_language ( language) . unwrap ( ) ;
183
- parser. set_included_ranges ( & ranges) . unwrap ( ) ;
183
+ parser. set_included_ranges ( ranges) . unwrap ( ) ;
184
184
let tree = parser. parse ( & source, None ) . expect ( "Failed to parse file" ) ;
185
185
trap_writer. comment ( format ! ( "Auto-generated TRAP file for {}" , path. display( ) ) ) ;
186
186
let file_label = & trap_writer. populate_file ( path) ;
187
187
let mut visitor = Visitor {
188
- source : & source ,
189
- trap_writer : trap_writer ,
188
+ source,
189
+ trap_writer,
190
190
// TODO: should we handle path strings that are not valid UTF8 better?
191
191
path : format ! ( "{}" , path. display( ) ) ,
192
192
file_label : * file_label,
@@ -205,15 +205,7 @@ pub fn extract(
205
205
/// HTML entities.
206
206
fn escape_key < ' a , S : Into < Cow < ' a , str > > > ( key : S ) -> Cow < ' a , str > {
207
207
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, '&' | '{' | '}' | '"' | '@' | '#' )
217
209
}
218
210
219
211
let key = key. into ( ) ;
@@ -296,7 +288,7 @@ struct Visitor<'a> {
296
288
/// source file.
297
289
file_label : Label ,
298
290
/// The source code as a UTF-8 byte array
299
- source : & ' a Vec < u8 > ,
291
+ source : & ' a [ u8 ] ,
300
292
/// A TrapWriter to accumulate trap entries
301
293
trap_writer : & ' a mut TrapWriter ,
302
294
/// A counter for top-level child nodes
@@ -342,7 +334,7 @@ impl Visitor<'_> {
342
334
full_error_message : String ,
343
335
node : Node ,
344
336
) {
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) ;
346
338
let loc = self . trap_writer . location (
347
339
self . file_label ,
348
340
start_line,
@@ -373,15 +365,15 @@ impl Visitor<'_> {
373
365
let id = self . trap_writer . fresh_id ( ) ;
374
366
375
367
self . stack . push ( ( id, 0 , Vec :: new ( ) ) ) ;
376
- return true ;
368
+ true
377
369
}
378
370
379
371
fn leave_node ( & mut self , field_name : Option < & ' static str > , node : Node ) {
380
372
if node. is_error ( ) || node. is_missing ( ) {
381
373
return ;
382
374
}
383
375
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) ;
385
377
let loc = self . trap_writer . location (
386
378
self . file_label ,
387
379
start_line,
@@ -440,11 +432,10 @@ impl Visitor<'_> {
440
432
Arg :: Int ( parent_index) ,
441
433
] ,
442
434
) ;
443
- let mut all_args = Vec :: new ( ) ;
444
- all_args. push ( Arg :: Label ( id) ) ;
435
+ let mut all_args = vec ! [ Arg :: Label ( id) ] ;
445
436
all_args. extend ( args) ;
446
437
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) ;
448
439
}
449
440
}
450
441
_ => {
@@ -479,8 +470,8 @@ impl Visitor<'_> {
479
470
fn complex_node (
480
471
& mut self ,
481
472
node : & Node ,
482
- fields : & Vec < Field > ,
483
- child_nodes : & Vec < ChildNode > ,
473
+ fields : & [ Field ] ,
474
+ child_nodes : & [ ChildNode ] ,
484
475
parent_id : Label ,
485
476
) -> Option < Vec < Arg > > {
486
477
let mut map: Map < & Option < String > , ( & Field , Vec < Arg > ) > = Map :: new ( ) ;
@@ -517,22 +508,20 @@ impl Visitor<'_> {
517
508
) ;
518
509
self . record_parse_error_for_node ( error_message, full_error_message, * node) ;
519
510
}
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) ;
536
525
}
537
526
}
538
527
let mut args = Vec :: new ( ) ;
@@ -580,13 +569,12 @@ impl Visitor<'_> {
580
569
) ;
581
570
break ;
582
571
}
583
- let mut args = Vec :: new ( ) ;
584
- args. push ( Arg :: Label ( parent_id) ) ;
572
+ let mut args = vec ! [ Arg :: Label ( parent_id) ] ;
585
573
if * has_index {
586
574
args. push ( Arg :: Int ( index) )
587
575
}
588
576
args. push ( child_value. clone ( ) ) ;
589
- self . trap_writer . add_tuple ( & table_name, args) ;
577
+ self . trap_writer . add_tuple ( table_name, args) ;
590
578
}
591
579
}
592
580
}
@@ -604,13 +592,10 @@ impl Visitor<'_> {
604
592
if tp == single_type {
605
593
return true ;
606
594
}
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 ;
612
598
}
613
- _ => { }
614
599
}
615
600
}
616
601
node_types:: FieldTypeInfo :: Multiple { types, .. } => {
@@ -640,15 +625,15 @@ impl Visitor<'_> {
640
625
}
641
626
642
627
// 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 {
644
629
let range = n. byte_range ( ) ;
645
630
Arg :: String ( String :: from_utf8_lossy ( & source[ range. start ..range. end ] ) . into_owned ( ) )
646
631
}
647
632
648
633
// Emit a pair of `TrapEntry`s for the provided node, appropriately calibrated.
649
634
// The first is the location and label definition, and the second is the
650
635
// '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 ) {
652
637
// Tree-sitter row, column values are 0-based while CodeQL starts
653
638
// counting at 1. In addition Tree-sitter's row and column for the
654
639
// end position are exclusive while CodeQL's end positions are inclusive.
@@ -806,7 +791,7 @@ impl fmt::Display for Arg {
806
791
/// the string is sliced at the provided limit. If there is a multi-byte character
807
792
/// at the limit then the returned slice will be slightly shorter than the limit to
808
793
/// 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 {
810
795
if string. len ( ) <= max_size {
811
796
return string;
812
797
}
@@ -817,7 +802,7 @@ fn limit_string(string: &String, max_size: usize) -> &str {
817
802
// encoded data any byte that matches the bit pattern 10XXXXXX is not a start byte.
818
803
// Therefore we decrement the index as long as there are bytes matching this pattern.
819
804
// 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 } {
821
806
index -= 1 ;
822
807
}
823
808
& string[ 0 ..index]
0 commit comments