From a6792610e5527679f7942cb92bb14ea73de66f0d Mon Sep 17 00:00:00 2001 From: ermakov-oleg Date: Mon, 21 Oct 2024 21:05:47 +0200 Subject: [PATCH 1/2] Use ColumnDef position in lint report --- cli/src/reporter.rs | 17 ++++--- linter/src/rules/ban_char_field.rs | 2 +- linter/src/rules/prefer_big_int.rs | 17 +++++-- linter/src/rules/prefer_bigint_over_int.rs | 6 +-- .../src/rules/prefer_bigint_over_smallint.rs | 6 +-- linter/src/rules/prefer_identity.rs | 6 +-- linter/src/rules/prefer_text_field.rs | 18 +++---- linter/src/rules/prefer_timestamptz.rs | 6 +-- ...ules__creating_table_with_char_errors.snap | 24 ++++------ ...big_int__test_rules__create_table_bad.snap | 48 +++++++------------ ..._test_rules__create_table_many_errors.snap | 36 ++++++++++++++ ...ver_int__test_rules__create_table_bad.snap | 24 ++++------ ...mallint__test_rules__create_table_bad.snap | 24 ++++------ ...tity__test_rules__prefer_identity_bad.snap | 36 +++++--------- ...d__test_rules__adding_column_non_text.snap | 6 +-- ..._timestamptz__test_rules__alter_table.snap | 12 ++--- parser/src/ast.rs | 10 ++++ 17 files changed, 149 insertions(+), 149 deletions(-) create mode 100644 linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_many_errors.snap diff --git a/cli/src/reporter.rs b/cli/src/reporter.rs index 4094244f..27244848 100644 --- a/cli/src/reporter.rs +++ b/cli/src/reporter.rs @@ -386,13 +386,18 @@ pub fn pretty_violations( #[allow(clippy::cast_sign_loss)] let start = start as usize; - #[allow(clippy::cast_sign_loss)] - let len = len.unwrap_or(0) as usize; - // 1-indexed - let lineno = sql[..start].lines().count() + 1; - - let content = &sql[start..=start + len]; + // remove the leading whitespace on last line + let lineno = sql[..start].trim_end().lines().count() + 1; + + let content = if let Some(len) = len { + #[allow(clippy::cast_sign_loss)] + &sql[start..=start + len as usize] + } else { + // Use current line + let tail = sql[start..].find('\n').unwrap_or(sql.len() - start); + &sql[start..=start + tail] + }; // TODO(sbdchd): could remove the leading whitespace and comments to // get cleaner reports diff --git a/linter/src/rules/ban_char_field.rs b/linter/src/rules/ban_char_field.rs index a544de06..6f7e9457 100644 --- a/linter/src/rules/ban_char_field.rs +++ b/linter/src/rules/ban_char_field.rs @@ -22,7 +22,7 @@ pub fn ban_char_type( if field_type_name.string.sval == "bpchar" { errs.push(RuleViolation::new( RuleViolationKind::BanCharField, - raw_stmt.into(), + column_def.into(), None, )); } diff --git a/linter/src/rules/prefer_big_int.rs b/linter/src/rules/prefer_big_int.rs index 5338666f..752f2c03 100644 --- a/linter/src/rules/prefer_big_int.rs +++ b/linter/src/rules/prefer_big_int.rs @@ -18,7 +18,7 @@ pub fn prefer_big_int( let mut errs = vec![]; for raw_stmt in tree { for column in columns_create_or_modified(&raw_stmt.stmt) { - check_column_def(&mut errs, raw_stmt, column); + check_column_def(&mut errs, column); } } errs @@ -37,12 +37,12 @@ lazy_static! { ]); } -fn check_column_def(errs: &mut Vec, raw_stmt: &RawStmt, column_def: &ColumnDef) { +fn check_column_def(errs: &mut Vec, column_def: &ColumnDef) { if let Some(column_name) = column_def.type_name.names.last() { if SMALL_INT_TYPES.contains(column_name.string.sval.as_str()) { errs.push(RuleViolation::new( RuleViolationKind::PreferBigInt, - raw_stmt.into(), + column_def.into(), None, )); } @@ -126,4 +126,15 @@ create table users ( ); assert_debug_snapshot!(res); } + #[test] + fn test_create_table_many_errors() { + let bad_sql = r" +create table users ( + foo integer, + bar serial +); + "; + let res = lint_sql(bad_sql); + assert_debug_snapshot!(res); + } } diff --git a/linter/src/rules/prefer_bigint_over_int.rs b/linter/src/rules/prefer_bigint_over_int.rs index a42188c5..8482e7d8 100644 --- a/linter/src/rules/prefer_bigint_over_int.rs +++ b/linter/src/rules/prefer_bigint_over_int.rs @@ -18,7 +18,7 @@ pub fn prefer_bigint_over_int( let mut errs = vec![]; for raw_stmt in tree { for column in columns_create_or_modified(&raw_stmt.stmt) { - check_column_def(&mut errs, raw_stmt, column); + check_column_def(&mut errs, column); } } errs @@ -29,12 +29,12 @@ lazy_static! { HashSet::from(["integer", "int4", "serial", "serial4",]); } -fn check_column_def(errs: &mut Vec, raw_stmt: &RawStmt, column_def: &ColumnDef) { +fn check_column_def(errs: &mut Vec, column_def: &ColumnDef) { if let Some(column_name) = column_def.type_name.names.last() { if INT_TYPES.contains(column_name.string.sval.as_str()) { errs.push(RuleViolation::new( RuleViolationKind::PreferBigintOverInt, - raw_stmt.into(), + column_def.into(), None, )); } diff --git a/linter/src/rules/prefer_bigint_over_smallint.rs b/linter/src/rules/prefer_bigint_over_smallint.rs index fee262f0..99ffb2dc 100644 --- a/linter/src/rules/prefer_bigint_over_smallint.rs +++ b/linter/src/rules/prefer_bigint_over_smallint.rs @@ -18,7 +18,7 @@ pub fn prefer_bigint_over_smallint( let mut errs = vec![]; for raw_stmt in tree { for column in columns_create_or_modified(&raw_stmt.stmt) { - check_column_def(&mut errs, raw_stmt, column); + check_column_def(&mut errs, column); } } errs @@ -29,12 +29,12 @@ lazy_static! { HashSet::from(["smallint", "int2", "smallserial", "serial2",]); } -fn check_column_def(errs: &mut Vec, raw_stmt: &RawStmt, column_def: &ColumnDef) { +fn check_column_def(errs: &mut Vec, column_def: &ColumnDef) { if let Some(column_name) = column_def.type_name.names.last() { if SMALL_INT_TYPES.contains(column_name.string.sval.as_str()) { errs.push(RuleViolation::new( RuleViolationKind::PreferBigintOverSmallint, - raw_stmt.into(), + column_def.into(), None, )); } diff --git a/linter/src/rules/prefer_identity.rs b/linter/src/rules/prefer_identity.rs index 5e050f07..3d971569 100644 --- a/linter/src/rules/prefer_identity.rs +++ b/linter/src/rules/prefer_identity.rs @@ -18,7 +18,7 @@ pub fn prefer_identity( let mut errs = vec![]; for raw_stmt in tree { for column in columns_create_or_modified(&raw_stmt.stmt) { - check_column_def(&mut errs, raw_stmt, column); + check_column_def(&mut errs, column); } } errs @@ -35,12 +35,12 @@ lazy_static! { ]); } -fn check_column_def(errs: &mut Vec, raw_stmt: &RawStmt, column_def: &ColumnDef) { +fn check_column_def(errs: &mut Vec, column_def: &ColumnDef) { if let Some(column_name) = column_def.type_name.names.last() { if SERIAL_TYPES.contains(column_name.string.sval.as_str()) { errs.push(RuleViolation::new( RuleViolationKind::PreferIdentity, - raw_stmt.into(), + column_def.into(), None, )); } diff --git a/linter/src/rules/prefer_text_field.rs b/linter/src/rules/prefer_text_field.rs index cf12dab3..1bae329c 100644 --- a/linter/src/rules/prefer_text_field.rs +++ b/linter/src/rules/prefer_text_field.rs @@ -19,19 +19,19 @@ pub fn prefer_text_field( let mut errs = vec![]; for raw_stmt in tree { for column in columns_create_or_modified(&raw_stmt.stmt) { - check_column_def(&mut errs, raw_stmt, column); + check_column_def(&mut errs, column); } } errs } -fn check_column_def(errs: &mut Vec, raw_stmt: &RawStmt, column_def: &ColumnDef) { +fn check_column_def(errs: &mut Vec, column_def: &ColumnDef) { let type_name = &column_def.type_name; for field_type_name in &type_name.names { if field_type_name.string.sval == "varchar" && !type_name.typmods.is_empty() { errs.push(RuleViolation::new( RuleViolationKind::PreferTextField, - raw_stmt.into(), + column_def.into(), None, )); } @@ -68,10 +68,8 @@ COMMIT; RuleViolation { kind: PreferTextField, span: Span { - start: 7, - len: Some( - 123, - ), + start: 77, + len: None, }, messages: [ Note( @@ -104,10 +102,8 @@ COMMIT; RuleViolation { kind: PreferTextField, span: Span { - start: 7, - len: Some( - 127, - ), + start: 103, + len: None, }, messages: [ Note( diff --git a/linter/src/rules/prefer_timestamptz.rs b/linter/src/rules/prefer_timestamptz.rs index 8b35d50a..f1309339 100644 --- a/linter/src/rules/prefer_timestamptz.rs +++ b/linter/src/rules/prefer_timestamptz.rs @@ -16,18 +16,18 @@ pub fn prefer_timestamptz( let mut errs = vec![]; for raw_stmt in tree { for column in columns_create_or_modified(&raw_stmt.stmt) { - check_column_def(&mut errs, raw_stmt, column); + check_column_def(&mut errs, column); } } errs } -fn check_column_def(errs: &mut Vec, raw_stmt: &RawStmt, column_def: &ColumnDef) { +fn check_column_def(errs: &mut Vec, column_def: &ColumnDef) { if let Some(type_name) = column_def.type_name.names.last() { if type_name.string.sval == "timestamp" { errs.push(RuleViolation::new( RuleViolationKind::PreferTimestampTz, - raw_stmt.into(), + column_def.into(), None, )); } diff --git a/linter/src/rules/snapshots/squawk_linter__rules__ban_char_field__test_rules__creating_table_with_char_errors.snap b/linter/src/rules/snapshots/squawk_linter__rules__ban_char_field__test_rules__creating_table_with_char_errors.snap index 00bcf2cf..58189170 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__ban_char_field__test_rules__creating_table_with_char_errors.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__ban_char_field__test_rules__creating_table_with_char_errors.snap @@ -6,10 +6,8 @@ expression: lint_sql(sql) RuleViolation { kind: BanCharField, span: Span { - start: 7, - len: Some( - 194, - ), + start: 76, + len: None, }, messages: [ Help( @@ -20,10 +18,8 @@ expression: lint_sql(sql) RuleViolation { kind: BanCharField, span: Span { - start: 7, - len: Some( - 194, - ), + start: 108, + len: None, }, messages: [ Help( @@ -34,10 +30,8 @@ expression: lint_sql(sql) RuleViolation { kind: BanCharField, span: Span { - start: 7, - len: Some( - 194, - ), + start: 144, + len: None, }, messages: [ Help( @@ -48,10 +42,8 @@ expression: lint_sql(sql) RuleViolation { kind: BanCharField, span: Span { - start: 7, - len: Some( - 194, - ), + start: 173, + len: None, }, messages: [ Help( diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_bad.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_bad.snap index 9b82c1a8..ef5c09b7 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_bad.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_bad.snap @@ -6,10 +6,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 0, - len: Some( - 39, - ), + start: 26, + len: None, }, messages: [ Note( @@ -23,10 +21,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 40, - len: Some( - 35, - ), + start: 66, + len: None, }, messages: [ Note( @@ -40,10 +36,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 76, - len: Some( - 38, - ), + start: 102, + len: None, }, messages: [ Note( @@ -57,10 +51,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 115, - len: Some( - 35, - ), + start: 141, + len: None, }, messages: [ Note( @@ -74,10 +66,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 151, - len: Some( - 37, - ), + start: 177, + len: None, }, messages: [ Note( @@ -91,10 +81,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 189, - len: Some( - 38, - ), + start: 215, + len: None, }, messages: [ Note( @@ -108,10 +96,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 228, - len: Some( - 38, - ), + start: 254, + len: None, }, messages: [ Note( @@ -125,10 +111,8 @@ expression: res RuleViolation { kind: PreferBigInt, span: Span { - start: 267, - len: Some( - 42, - ), + start: 293, + len: None, }, messages: [ Note( diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_many_errors.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_many_errors.snap new file mode 100644 index 00000000..fdb4d463 --- /dev/null +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_big_int__test_rules__create_table_many_errors.snap @@ -0,0 +1,36 @@ +--- +source: linter/src/rules/prefer_big_int.rs +expression: res +--- +[ + RuleViolation { + kind: PreferBigInt, + span: Span { + start: 26, + len: None, + }, + messages: [ + Note( + "Hitting the max 32 bit integer is possible and may break your application.", + ), + Help( + "Use 64bit integer values instead to prevent hitting this limit.", + ), + ], + }, + RuleViolation { + kind: PreferBigInt, + span: Span { + start: 43, + len: None, + }, + messages: [ + Note( + "Hitting the max 32 bit integer is possible and may break your application.", + ), + Help( + "Use 64bit integer values instead to prevent hitting this limit.", + ), + ], + }, +] diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_int__test_rules__create_table_bad.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_int__test_rules__create_table_bad.snap index 5fd81f81..ff2691f9 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_int__test_rules__create_table_bad.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_int__test_rules__create_table_bad.snap @@ -6,10 +6,8 @@ expression: res RuleViolation { kind: PreferBigintOverInt, span: Span { - start: 0, - len: Some( - 38, - ), + start: 26, + len: None, }, messages: [ Note( @@ -23,10 +21,8 @@ expression: res RuleViolation { kind: PreferBigintOverInt, span: Span { - start: 39, - len: Some( - 35, - ), + start: 65, + len: None, }, messages: [ Note( @@ -40,10 +36,8 @@ expression: res RuleViolation { kind: PreferBigintOverInt, span: Span { - start: 75, - len: Some( - 37, - ), + start: 101, + len: None, }, messages: [ Note( @@ -57,10 +51,8 @@ expression: res RuleViolation { kind: PreferBigintOverInt, span: Span { - start: 113, - len: Some( - 38, - ), + start: 139, + len: None, }, messages: [ Note( diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_smallint__test_rules__create_table_bad.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_smallint__test_rules__create_table_bad.snap index c8a17517..9559895d 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_smallint__test_rules__create_table_bad.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_bigint_over_smallint__test_rules__create_table_bad.snap @@ -6,10 +6,8 @@ expression: res RuleViolation { kind: PreferBigintOverSmallint, span: Span { - start: 0, - len: Some( - 39, - ), + start: 26, + len: None, }, messages: [ Note( @@ -23,10 +21,8 @@ expression: res RuleViolation { kind: PreferBigintOverSmallint, span: Span { - start: 40, - len: Some( - 35, - ), + start: 66, + len: None, }, messages: [ Note( @@ -40,10 +36,8 @@ expression: res RuleViolation { kind: PreferBigintOverSmallint, span: Span { - start: 76, - len: Some( - 42, - ), + start: 102, + len: None, }, messages: [ Note( @@ -57,10 +51,8 @@ expression: res RuleViolation { kind: PreferBigintOverSmallint, span: Span { - start: 119, - len: Some( - 38, - ), + start: 145, + len: None, }, messages: [ Note( diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_identity__test_rules__prefer_identity_bad.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_identity__test_rules__prefer_identity_bad.snap index 13161222..5f1a8399 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__prefer_identity__test_rules__prefer_identity_bad.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_identity__test_rules__prefer_identity_bad.snap @@ -6,10 +6,8 @@ expression: res RuleViolation { kind: PreferIdentity, span: Span { - start: 0, - len: Some( - 37, - ), + start: 26, + len: None, }, messages: [ Note( @@ -23,10 +21,8 @@ expression: res RuleViolation { kind: PreferIdentity, span: Span { - start: 38, - len: Some( - 38, - ), + start: 64, + len: None, }, messages: [ Note( @@ -40,10 +36,8 @@ expression: res RuleViolation { kind: PreferIdentity, span: Span { - start: 77, - len: Some( - 38, - ), + start: 103, + len: None, }, messages: [ Note( @@ -57,10 +51,8 @@ expression: res RuleViolation { kind: PreferIdentity, span: Span { - start: 116, - len: Some( - 38, - ), + start: 142, + len: None, }, messages: [ Note( @@ -74,10 +66,8 @@ expression: res RuleViolation { kind: PreferIdentity, span: Span { - start: 155, - len: Some( - 42, - ), + start: 181, + len: None, }, messages: [ Note( @@ -91,10 +81,8 @@ expression: res RuleViolation { kind: PreferIdentity, span: Span { - start: 198, - len: Some( - 40, - ), + start: 224, + len: None, }, messages: [ Note( diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_text_field__test_rules__adding_column_non_text.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_text_field__test_rules__adding_column_non_text.snap index 88d55ba2..fa57f4b5 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__prefer_text_field__test_rules__adding_column_non_text.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_text_field__test_rules__adding_column_non_text.snap @@ -6,10 +6,8 @@ expression: res RuleViolation { kind: PreferTextField, span: Span { - start: 7, - len: Some( - 66, - ), + start: 43, + len: None, }, messages: [ Note( diff --git a/linter/src/rules/snapshots/squawk_linter__rules__prefer_timestamptz__test_rules__alter_table.snap b/linter/src/rules/snapshots/squawk_linter__rules__prefer_timestamptz__test_rules__alter_table.snap index 93de8c6b..ff7c07a7 100644 --- a/linter/src/rules/snapshots/squawk_linter__rules__prefer_timestamptz__test_rules__alter_table.snap +++ b/linter/src/rules/snapshots/squawk_linter__rules__prefer_timestamptz__test_rules__alter_table.snap @@ -6,10 +6,8 @@ expression: res RuleViolation { kind: PreferTimestampTz, span: Span { - start: 0, - len: Some( - 73, - ), + start: 48, + len: None, }, messages: [ Note( @@ -23,10 +21,8 @@ expression: res RuleViolation { kind: PreferTimestampTz, span: Span { - start: 74, - len: Some( - 94, - ), + start: 125, + len: None, }, messages: [ Note( diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 0431e1c3..66805572 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -54,6 +54,16 @@ impl std::convert::From<&RawStmt> for Span { } } +impl std::convert::From<&ColumnDef> for Span { + fn from(stmt: &ColumnDef) -> Self { + Self { + start: stmt.location, + // Use current line + len: None, + } + } +} + impl RawStmt { #[must_use] pub fn span(&self) -> Span { From 993a450bae9c95bba77be9baad2e2fc3938813b7 Mon Sep 17 00:00:00 2001 From: ermakov-oleg Date: Mon, 11 Nov 2024 12:29:15 +0100 Subject: [PATCH 2/2] Fix build python wheels --- .github/workflows/python.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index ac10da80..b6bf678d 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -49,6 +49,7 @@ jobs: uses: PyO3/maturin-action@v1 with: target: ${{ matrix.platform.target }} + maturin-version: v1.7.1 working-directory: cli args: --release --out dist ${{ matrix.platform.maturin-options }} manylinux: ${{ matrix.platform.manylinux }}