Skip to content

Commit

Permalink
Use ColumnDef position in lint report
Browse files Browse the repository at this point in the history
  • Loading branch information
ermakov-oleg committed Nov 11, 2024
1 parent 3a31b17 commit a679261
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 149 deletions.
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

0 comments on commit a679261

Please sign in to comment.