@@ -386,17 +386,28 @@ pub fn pretty_violations(
386386 #[ allow( clippy:: cast_sign_loss) ]
387387 let start = start as usize ;
388388
389- // 1-indexed
390- // remove the leading whitespace on last line
391- let lineno = sql[ ..start] . trim_end ( ) . lines ( ) . count ( ) + 1 ;
389+ let mut lineno = 0 ;
390+
391+ for ( idx, char) in sql. chars ( ) . enumerate ( ) {
392+ if char == '\n' {
393+ lineno += 1 ;
394+ }
395+
396+ if idx == start {
397+ break ;
398+ }
399+ }
400+
401+ lineno += 1 ;
392402
393403 let content = if let Some ( len) = len {
394404 #[ allow( clippy:: cast_sign_loss) ]
395405 & sql[ start..=start + len as usize ]
396406 } else {
397407 // Use current line
398408 let tail = sql[ start..] . find ( '\n' ) . unwrap_or ( sql. len ( ) - start) ;
399- & sql[ start..=start + tail]
409+
410+ & sql. chars ( ) . skip ( start) . take ( tail + 1 ) . collect :: < String > ( )
400411 } ;
401412
402413 // TODO(sbdchd): could remove the leading whitespace and comments to
@@ -666,6 +677,7 @@ mod test_reporter {
666677 check_sql_with_rule,
667678 violations:: { RuleViolation , RuleViolationKind } ,
668679 } ;
680+ use squawk_parser:: ast:: Span ;
669681
670682 fn lint_sql ( sql : & str ) -> Vec < RuleViolation > {
671683 check_sql_with_rule ( sql, & RuleViolationKind :: AddingRequiredField , None , false ) . unwrap ( )
@@ -825,4 +837,41 @@ SELECT 1;
825837 }
826838 "# ) ;
827839 }
840+
841+ #[ test]
842+ fn regression_slicing_issue_425 ( ) {
843+ // Squawk was crashing with an slicing issue.
844+ let sql = "ALTER TABLE test ADD COLUMN IF NOT EXISTS test INTEGER;" ;
845+ let violation = RuleViolation :: new (
846+ RuleViolationKind :: PreferBigInt ,
847+ Span {
848+ start : 42 ,
849+ len : None ,
850+ } ,
851+ None ,
852+ ) ;
853+ pretty_violations ( vec ! [ violation] , sql, "main.sql" ) ;
854+ }
855+ #[ test]
856+ fn highlight_column_for_issues ( ) {
857+ // Display only the columns with issues for large DDLs.
858+ fn lint_sql ( sql : & str ) -> Vec < RuleViolation > {
859+ check_sql_with_rule ( sql, & RuleViolationKind :: PreferTextField , None , false ) . unwrap ( )
860+ }
861+ // Squawk was crashing with an slicing issue.
862+ let sql = "create table test_table (
863+ col1 varchar(255),
864+ col2 varchar(255),
865+ col3 varchar(255)
866+ --- other columns
867+ );" ;
868+ let violations = lint_sql ( sql) ;
869+ let res = pretty_violations ( violations, sql, "main.sql" ) ;
870+ let columns = res
871+ . violations
872+ . iter ( )
873+ . map ( |v| v. sql . clone ( ) )
874+ . collect :: < String > ( ) ;
875+ assert_display_snapshot ! ( columns) ;
876+ }
828877}
0 commit comments