Skip to content

Commit 7ead01f

Browse files
authored
linter: fixes for prefer-robust-stmts (#602)
Currently only available via the LSP server, not sure it makes sense to include these as auto-fixable in the CLI. Might need a config or setting to determine what should and shouldn't be fixed. Looked at rust-analyzer's fix API along with ruff's for inspo.
1 parent 82d6b79 commit 7ead01f

File tree

78 files changed

+551
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+551
-96
lines changed

crates/squawk/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Please open an issue at https://github.com/sbdchd/squawk/issues/new with the log
217217
let mut clap_app = Opt::clap();
218218
let is_stdin = !atty::is(Stream::Stdin);
219219
let github_annotations = std::env::var("GITHUB_ACTIONS").is_ok()
220-
&& !std::env::var("SQUAWK_DISABLE_GITHUB_ANNOTATIONS").is_ok();
220+
&& std::env::var("SQUAWK_DISABLE_GITHUB_ANNOTATIONS").is_err();
221221
match opts.cmd {
222222
Some(Command::Server) => {
223223
squawk_server::run().context("language server failed")?;

crates/squawk_linter/src/ignore.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ alter table t drop column c cascade;
284284
let errors: Vec<_> = linter
285285
.lint(parse, sql)
286286
.into_iter()
287-
.map(|x| x.code.clone())
287+
.map(|x| x.code)
288288
.collect();
289289
assert!(errors.is_empty());
290290
}
@@ -355,7 +355,7 @@ alter table t2 drop column c2 cascade;
355355
let errors: Vec<_> = linter
356356
.lint(parse, sql)
357357
.into_iter()
358-
.map(|x| x.code.clone())
358+
.map(|x| x.code)
359359
.collect();
360360

361361
assert_debug_snapshot!(errors, @r"
@@ -379,7 +379,7 @@ alter table t2 drop column c2 cascade;
379379
let errors: Vec<_> = linter
380380
.lint(parse, sql)
381381
.into_iter()
382-
.map(|x| x.code.clone())
382+
.map(|x| x.code)
383383
.collect();
384384

385385
assert_debug_snapshot!(errors, @r"

crates/squawk_linter/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ignore::find_ignores;
88
use ignore_index::IgnoreIndex;
99
use lazy_static::lazy_static;
1010
use rowan::TextRange;
11+
use rowan::TextSize;
1112
use serde::{Deserialize, Serialize};
1213

1314
use squawk_syntax::{Parse, SourceFile};
@@ -222,6 +223,36 @@ pub struct Violation {
222223
pub message: String,
223224
pub text_range: TextRange,
224225
pub help: Option<String>,
226+
pub fix: Option<Fix>,
227+
}
228+
229+
#[derive(Debug, Clone, PartialEq, Eq)]
230+
pub struct Fix {
231+
pub title: String,
232+
pub edits: Vec<Edit>,
233+
}
234+
235+
impl Fix {
236+
fn new<T: Into<String>>(title: T, edits: Vec<Edit>) -> Fix {
237+
Fix {
238+
title: title.into(),
239+
edits,
240+
}
241+
}
242+
}
243+
244+
#[derive(Debug, Clone, PartialEq, Eq)]
245+
pub struct Edit {
246+
pub text_range: TextRange,
247+
pub text: Option<String>,
248+
}
249+
impl Edit {
250+
fn insert<T: Into<String>>(text: T, at: TextSize) -> Self {
251+
Self {
252+
text_range: TextRange::new(at, at),
253+
text: Some(text.into()),
254+
}
255+
}
225256
}
226257

227258
impl Violation {
@@ -237,8 +268,14 @@ impl Violation {
237268
text_range,
238269
message,
239270
help: help.into(),
271+
fix: None,
240272
}
241273
}
274+
275+
fn with_fix(mut self, fix: Option<Fix>) -> Violation {
276+
self.fix = fix;
277+
self
278+
}
242279
}
243280

244281
pub struct LinterSettings {

crates/squawk_linter/src/rules/adding_field_with_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lazy_static! {
2222
.split('\n')
2323
.map(|x| x.trim())
2424
.filter(|x| !x.is_empty())
25-
.map(|x| Identifier::new(x))
25+
.map(Identifier::new)
2626
.collect()
2727
};
2828
}

crates/squawk_linter/src/rules/constraint_missing_not_valid.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use squawk_syntax::{
55
ast::{self, AstNode},
66
};
77

8-
use crate::{
9-
Linter, Rule, Violation,
10-
identifier::Identifier,
11-
};
8+
use crate::{Linter, Rule, Violation, identifier::Identifier};
129

1310
pub fn tables_created_in_transaction(
1411
assume_in_transaction: bool,
@@ -73,7 +70,8 @@ fn not_valid_validate_in_transaction(
7370
if add_constraint.not_valid().is_some() {
7471
if let Some(constraint) = add_constraint.constraint() {
7572
if let Some(constraint_name) = constraint.name() {
76-
not_valid_names.insert(Identifier::new(&constraint_name.text()));
73+
not_valid_names
74+
.insert(Identifier::new(&constraint_name.text()));
7775
}
7876
}
7977
}

0 commit comments

Comments
 (0)