Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ColumnDef position in lint report #391

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
17 changes: 11 additions & 6 deletions cli/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion linter/src/rules/ban_char_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}
Expand Down
17 changes: 14 additions & 3 deletions linter/src/rules/prefer_big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,12 +37,12 @@ lazy_static! {
]);
}

fn check_column_def(errs: &mut Vec<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
fn check_column_def(errs: &mut Vec<RuleViolation>, 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,
));
}
Expand Down Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions linter/src/rules/prefer_bigint_over_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,12 +29,12 @@ lazy_static! {
HashSet::from(["integer", "int4", "serial", "serial4",]);
}

fn check_column_def(errs: &mut Vec<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
fn check_column_def(errs: &mut Vec<RuleViolation>, 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,
));
}
Expand Down
6 changes: 3 additions & 3 deletions linter/src/rules/prefer_bigint_over_smallint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,12 +29,12 @@ lazy_static! {
HashSet::from(["smallint", "int2", "smallserial", "serial2",]);
}

fn check_column_def(errs: &mut Vec<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
fn check_column_def(errs: &mut Vec<RuleViolation>, 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,
));
}
Expand Down
6 changes: 3 additions & 3 deletions linter/src/rules/prefer_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,12 +35,12 @@ lazy_static! {
]);
}

fn check_column_def(errs: &mut Vec<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
fn check_column_def(errs: &mut Vec<RuleViolation>, 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,
));
}
Expand Down
18 changes: 7 additions & 11 deletions linter/src/rules/prefer_text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
fn check_column_def(errs: &mut Vec<RuleViolation>, 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,
));
}
Expand Down Expand Up @@ -68,10 +68,8 @@ COMMIT;
RuleViolation {
kind: PreferTextField,
span: Span {
start: 7,
len: Some(
123,
),
start: 77,
len: None,
},
messages: [
Note(
Expand Down Expand Up @@ -104,10 +102,8 @@ COMMIT;
RuleViolation {
kind: PreferTextField,
span: Span {
start: 7,
len: Some(
127,
),
start: 103,
len: None,
},
messages: [
Note(
Expand Down
6 changes: 3 additions & 3 deletions linter/src/rules/prefer_timestamptz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuleViolation>, raw_stmt: &RawStmt, column_def: &ColumnDef) {
fn check_column_def(errs: &mut Vec<RuleViolation>, 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,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ expression: lint_sql(sql)
RuleViolation {
kind: BanCharField,
span: Span {
start: 7,
len: Some(
194,
),
start: 76,
len: None,
},
messages: [
Help(
Expand All @@ -20,10 +18,8 @@ expression: lint_sql(sql)
RuleViolation {
kind: BanCharField,
span: Span {
start: 7,
len: Some(
194,
),
start: 108,
len: None,
},
messages: [
Help(
Expand All @@ -34,10 +30,8 @@ expression: lint_sql(sql)
RuleViolation {
kind: BanCharField,
span: Span {
start: 7,
len: Some(
194,
),
start: 144,
len: None,
},
messages: [
Help(
Expand All @@ -48,10 +42,8 @@ expression: lint_sql(sql)
RuleViolation {
kind: BanCharField,
span: Span {
start: 7,
len: Some(
194,
),
start: 173,
len: None,
},
messages: [
Help(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 0,
len: Some(
39,
),
start: 26,
len: None,
},
messages: [
Note(
Expand All @@ -23,10 +21,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 40,
len: Some(
35,
),
start: 66,
len: None,
},
messages: [
Note(
Expand All @@ -40,10 +36,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 76,
len: Some(
38,
),
start: 102,
len: None,
},
messages: [
Note(
Expand All @@ -57,10 +51,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 115,
len: Some(
35,
),
start: 141,
len: None,
},
messages: [
Note(
Expand All @@ -74,10 +66,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 151,
len: Some(
37,
),
start: 177,
len: None,
},
messages: [
Note(
Expand All @@ -91,10 +81,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 189,
len: Some(
38,
),
start: 215,
len: None,
},
messages: [
Note(
Expand All @@ -108,10 +96,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 228,
len: Some(
38,
),
start: 254,
len: None,
},
messages: [
Note(
Expand All @@ -125,10 +111,8 @@ expression: res
RuleViolation {
kind: PreferBigInt,
span: Span {
start: 267,
len: Some(
42,
),
start: 293,
len: None,
},
messages: [
Note(
Expand Down
Loading
Loading