Skip to content

Commit 9ccdca6

Browse files
authored
linter: cleanup auto fix code to be more concise (#622)
1 parent fdb6bc9 commit 9ccdca6

File tree

8 files changed

+59
-96
lines changed

8 files changed

+59
-96
lines changed

crates/squawk_linter/src/rules/prefer_bigint_over_int.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ fn int_to_bigint_replacement(int_type: &str) -> &'static str {
3232
}
3333

3434
fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {
35-
if let Some(type_name) = ty.syntax().first_token() {
36-
let i64 = int_to_bigint_replacement(type_name.text());
37-
let edit = Edit::replace(ty.syntax().text_range(), i64);
38-
Some(Fix::new(
39-
format!("Replace with a 64-bit integer type: `{i64}`"),
40-
vec![edit],
41-
))
42-
} else {
43-
None
44-
}
35+
let type_name = ty.syntax().first_token()?;
36+
let i64 = int_to_bigint_replacement(type_name.text());
37+
let edit = Edit::replace(ty.syntax().text_range(), i64);
38+
Some(Fix::new(
39+
format!("Replace with a 64-bit integer type: `{i64}`"),
40+
vec![edit],
41+
))
4542
}
4643

4744
fn check_ty_for_big_int(ctx: &mut Linter, ty: Option<ast::Type>) {

crates/squawk_linter/src/rules/prefer_bigint_over_smallint.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ fn smallint_to_bigint(smallint_type: &str) -> &'static str {
3131
}
3232

3333
fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {
34-
if let Some(type_name) = ty.syntax().first_token() {
35-
let i64 = smallint_to_bigint(type_name.text());
36-
let edit = Edit::replace(ty.syntax().text_range(), i64);
37-
Some(Fix::new(
38-
format!("Replace with a 64-bit integer type: `{i64}`"),
39-
vec![edit],
40-
))
41-
} else {
42-
None
43-
}
34+
let type_name = ty.syntax().first_token()?;
35+
let i64 = smallint_to_bigint(type_name.text());
36+
let edit = Edit::replace(ty.syntax().text_range(), i64);
37+
Some(Fix::new(
38+
format!("Replace with a 64-bit integer type: `{i64}`"),
39+
vec![edit],
40+
))
4441
}
4542

4643
fn check_ty_for_small_int(ctx: &mut Linter, ty: Option<ast::Type>) {

crates/squawk_linter/src/rules/prefer_identity.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ fn replace_serial(serial_type: &str) -> &'static str {
3232
}
3333

3434
fn create_identity_fix(ty: &ast::Type) -> Option<Fix> {
35-
if let Some(type_name) = ty.syntax().first_token() {
36-
let text = replace_serial(type_name.text());
37-
let edit = Edit::replace(ty.syntax().text_range(), text);
38-
Some(Fix::new("Replace with IDENTITY column", vec![edit]))
39-
} else {
40-
None
41-
}
35+
let type_name = ty.syntax().first_token()?;
36+
let text = replace_serial(type_name.text());
37+
let edit = Edit::replace(ty.syntax().text_range(), text);
38+
Some(Fix::new("Replace with IDENTITY column", vec![edit]))
4239
}
4340

4441
fn check_ty_for_serial(ctx: &mut Linter, ty: Option<ast::Type>) {

crates/squawk_linter/src/rules/prefer_robust_stmts.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
5858
continue;
5959
}
6060

61-
let fix = if let Some(constraint_token) =
62-
drop_constraint.constraint_token()
63-
{
61+
let fix = drop_constraint.constraint_token().map(|constraint_token| {
6462
let at = constraint_token.text_range().end();
6563
let edit = Edit::insert(" if exists", at);
66-
Some(Fix::new("Insert `if exists`", vec![edit]))
67-
} else {
68-
None
69-
};
64+
Fix::new("Insert `if exists`", vec![edit])
65+
});
7066

7167
(ActionErrorMessage::IfExists, fix)
7268
}
@@ -75,13 +71,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
7571
continue;
7672
}
7773

78-
let fix = if let Some(column_token) = add_column.column_token() {
74+
let fix = add_column.column_token().map(|column_token| {
7975
let at = column_token.text_range().end();
8076
let edit = Edit::insert(" if not exists", at);
81-
Some(Fix::new("Insert `if not exists`", vec![edit]))
82-
} else {
83-
None
84-
};
77+
Fix::new("Insert `if not exists`", vec![edit])
78+
});
8579
(ActionErrorMessage::IfNotExists, fix)
8680
}
8781
ast::AlterTableAction::ValidateConstraint(validate_constraint) => {
@@ -113,13 +107,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
113107
continue;
114108
}
115109

116-
let fix = if let Some(column_token) = drop_column.column_token() {
110+
let fix = drop_column.column_token().map(|column_token| {
117111
let at = column_token.text_range().end();
118112
let edit = Edit::insert(" if exists", at);
119-
Some(Fix::new("Insert `if exists`", vec![edit]))
120-
} else {
121-
None
122-
};
113+
Fix::new("Insert `if exists`", vec![edit])
114+
});
123115
(ActionErrorMessage::IfExists, fix)
124116
}
125117
_ => (ActionErrorMessage::None, None),
@@ -152,13 +144,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
152144
&& create_index.name().is_some()
153145
&& (create_index.concurrently_token().is_some() || !inside_transaction) =>
154146
{
155-
let fix = if let Some(name) = create_index.name() {
147+
let fix = create_index.name().map(|name| {
156148
let at = name.syntax().text_range().start();
157149
let edit = Edit::insert("if not exists ", at);
158-
Some(Fix::new("Insert `if not exists`", vec![edit]))
159-
} else {
160-
None
161-
};
150+
Fix::new("Insert `if not exists`", vec![edit])
151+
});
162152
ctx.report(Violation::for_node(
163153
Rule::PreferRobustStmts,
164154
"Missing `IF NOT EXISTS`, the migration can't be rerun if it fails part way through.".into(),
@@ -168,13 +158,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
168158
ast::Stmt::CreateTable(create_table)
169159
if create_table.if_not_exists().is_none() && !inside_transaction =>
170160
{
171-
let fix = if let Some(table_token) = create_table.table_token() {
161+
let fix = create_table.table_token().map(|table_token| {
172162
let at = table_token.text_range().end();
173163
let edit = Edit::insert(" if not exists", at);
174-
Some(Fix::new("Insert `if not exists`", vec![edit]))
175-
} else {
176-
None
177-
};
164+
Fix::new("Insert `if not exists`", vec![edit])
165+
});
178166

179167
ctx.report(Violation::for_node(
180168
Rule::PreferRobustStmts,
@@ -185,13 +173,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
185173
ast::Stmt::DropIndex(drop_index)
186174
if drop_index.if_exists().is_none() && !inside_transaction =>
187175
{
188-
let fix = if let Some(first_index) = drop_index.paths().next() {
176+
let fix = drop_index.paths().next().map(|first_index| {
189177
let at = first_index.syntax().text_range().start();
190178
let edit = Edit::insert("if exists ", at);
191-
Some(Fix::new("Insert `if exists`", vec![edit]))
192-
} else {
193-
None
194-
};
179+
Fix::new("Insert `if exists`", vec![edit])
180+
});
195181

196182
ctx.report(Violation::for_node(
197183
Rule::PreferRobustStmts,
@@ -202,13 +188,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
202188
ast::Stmt::DropTable(drop_table)
203189
if drop_table.if_exists().is_none() && !inside_transaction =>
204190
{
205-
let fix = if let Some(table_token) = drop_table.table_token() {
191+
let fix = drop_table.table_token().map(|table_token| {
206192
let at = table_token.text_range().end();
207193
let edit = Edit::insert(" if exists", at);
208-
Some(Fix::new("Insert `if exists`", vec![edit]))
209-
} else {
210-
None
211-
};
194+
Fix::new("Insert `if exists`", vec![edit])
195+
});
212196
ctx.report(Violation::for_node(
213197
Rule::PreferRobustStmts,
214198
"Missing `IF EXISTS`, the migration can't be rerun if it fails part way through.".into(),
@@ -218,13 +202,11 @@ pub(crate) fn prefer_robust_stmts(ctx: &mut Linter, parse: &Parse<SourceFile>) {
218202
ast::Stmt::DropType(drop_type)
219203
if drop_type.if_exists().is_none() && !inside_transaction =>
220204
{
221-
let fix = if let Some(type_token) = drop_type.type_token() {
205+
let fix = drop_type.type_token().map(|type_token| {
222206
let at = type_token.text_range().end();
223207
let edit = Edit::insert(" if exists", at);
224-
Some(Fix::new("Insert `if exists`", vec![edit]))
225-
} else {
226-
None
227-
};
208+
Fix::new("Insert `if exists`", vec![edit])
209+
});
228210

229211
ctx.report(Violation::for_node(
230212
Rule::PreferRobustStmts,

crates/squawk_linter/src/rules/prefer_text_field.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,25 @@ fn is_not_allowed_varchar(ty: &ast::Type) -> bool {
5050
}
5151

5252
fn create_varchar_to_text_fix(ty: &ast::Type) -> Option<Fix> {
53-
match ty {
53+
let range = match ty {
5454
ast::Type::PathType(path_type) => {
5555
// we'll replace the entire path type, including args
5656
// so: `"varchar"(100)` becomes `text`
57-
let edit = Edit::replace(path_type.syntax().text_range(), "text");
58-
Some(Fix::new("Replace with `text`", vec![edit]))
57+
path_type.syntax().text_range()
5958
}
6059
ast::Type::CharType(char_type) => {
6160
// we'll replace the entire char type, including args
6261
// so: `varchar(100)` becomes `text`
63-
let edit = Edit::replace(char_type.syntax().text_range(), "text");
64-
Some(Fix::new("Replace with `text`", vec![edit]))
62+
char_type.syntax().text_range()
6563
}
6664
ast::Type::ArrayType(array_type) => {
6765
let ty = array_type.ty()?;
68-
let edit = Edit::replace(ty.syntax().text_range(), "text");
69-
Some(Fix::new("Replace with `text`", vec![edit]))
66+
ty.syntax().text_range()
7067
}
71-
_ => None,
72-
}
68+
_ => return None,
69+
};
70+
let edit = Edit::replace(range, "text");
71+
Some(Fix::new("Replace with `text`", vec![edit]))
7372
}
7473

7574
fn check_ty_for_varchar(ctx: &mut Linter, ty: Option<ast::Type>) {

crates/squawk_linter/src/rules/require_concurrent_index_creation.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ use crate::{Edit, Fix, Linter, Rule, Violation, identifier::Identifier};
88
use super::constraint_missing_not_valid::tables_created_in_transaction;
99

1010
fn concurrently_fix(create_index: &ast::CreateIndex) -> Option<Fix> {
11-
if let Some(index_token) = create_index.index_token() {
12-
let at = index_token.text_range().end();
13-
let edit = Edit::insert(" concurrently", at);
14-
Some(Fix::new("Add `concurrently`", vec![edit]))
15-
} else {
16-
None
17-
}
11+
let index_token = create_index.index_token()?;
12+
let at = index_token.text_range().end();
13+
let edit = Edit::insert(" concurrently", at);
14+
Some(Fix::new("Add `concurrently`", vec![edit]))
1815
}
1916

2017
pub(crate) fn require_concurrent_index_creation(ctx: &mut Linter, parse: &Parse<SourceFile>) {

crates/squawk_linter/src/rules/require_concurrent_index_deletion.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ use squawk_syntax::{
66
use crate::{Edit, Fix, Linter, Rule, Violation};
77

88
fn concurrently_fix(drop_index: &ast::DropIndex) -> Option<Fix> {
9-
if let Some(index_token) = drop_index.index_token() {
10-
let at = index_token.text_range().end();
11-
let edit = Edit::insert(" concurrently", at);
12-
Some(Fix::new("Add `concurrently`", vec![edit]))
13-
} else {
14-
None
15-
}
9+
let index_token = drop_index.index_token()?;
10+
let at = index_token.text_range().end();
11+
let edit = Edit::insert(" concurrently", at);
12+
Some(Fix::new("Add `concurrently`", vec![edit]))
1613
}
1714

1815
pub(crate) fn require_concurrent_index_deletion(ctx: &mut Linter, parse: &Parse<SourceFile>) {

crates/squawk_wasm/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ pub fn lint(text: String) -> Result<JsValue, Error> {
128128
.to_wide(line_index::WideEncoding::Utf16, end)
129129
.unwrap();
130130

131-
let messages = match x.help {
132-
Some(help) => vec![help],
133-
None => vec![],
134-
};
131+
let messages = x.help.into_iter().collect();
135132

136133
let fix = x.fix.map(|fix| {
137134
let edits = fix

0 commit comments

Comments
 (0)