@@ -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.
206206fn 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]
0 commit comments