Skip to content

Commit 956204e

Browse files
authored
fix panic when formatting violations (#426)
fixes #425
1 parent b5a625b commit 956204e

File tree

8 files changed

+74
-9
lines changed

8 files changed

+74
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## v1.6.1 - 2025-05-02
11+
12+
### Fixed
13+
14+
- Fixed panic when formatting violations (#426).
15+
1016
## v1.6.0 - 2025-04-02
1117

18+
### Added
19+
1220
- Added `ban-alter-domain-with-add-constraint` and `ban-create-domain-with-constraint` (#418). Thanks @johnmastro!
1321

1422
## v1.5.5 - 2025-03-20

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "squawk"
3-
version = "1.6.0"
3+
version = "1.6.1"
44
authors = ["Steve Dignam <[email protected]>"]
55
edition = "2018"
66
license = "GPL-3.0"

cli/src/reporter.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: cli/src/reporter.rs
3+
expression: columns
4+
---
5+
col1 varchar(255),
6+
col2 varchar(255),
7+
col3 varchar(255)
8+

cli/src/snapshots/squawk__reporter__test_reporter__span_offsets.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ViolationContent {
88
violations: [
99
ReportViolation {
1010
file: "main.sql",
11-
line: 1,
11+
line: 2,
1212
column: 2,
1313
level: Warning,
1414
messages: [

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
squawk = final.rustPlatform.buildRustPackage {
2020
pname = "squawk";
21-
version = "1.6.0";
21+
version = "1.6.1";
2222

2323
cargoLock = {
2424
lockFile = ./Cargo.lock;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "squawk-cli",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "linter for PostgreSQL, focused on migrations",
55
"repository": "[email protected]:sbdchd/squawk.git",
66
"author": "Steve Dignam <[email protected]>",

0 commit comments

Comments
 (0)