Skip to content
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: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ console = "0.11.3"
glob = "0.3.1"
insta = "1.39.0"
jsonwebtoken = { version = "10.3.0", features = ["rust_crypto"] }
lazy_static = "1.5.0"
log = "0.4.25"
reqwest = { version = "0.11.27", features = ["native-tls-vendored", "blocking", "json"] }
la-arena = "0.3.1"
Expand Down
1 change: 0 additions & 1 deletion crates/squawk_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ squawk-syntax.workspace = true

rowan.workspace = true
serde.workspace = true
lazy_static.workspace = true
insta.workspace = true
enum-iterator.workspace = true
line-index.workspace = true
Expand Down
12 changes: 7 additions & 5 deletions crates/squawk_linter/src/rules/adding_field_with_default.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use std::sync::OnceLock;

use rustc_hash::FxHashSet;

use squawk_syntax::ast::AstNode;
Expand All @@ -7,15 +8,16 @@ use squawk_syntax::{ast, identifier::Identifier};

use crate::{Linter, Rule, Version, Violation};

lazy_static! {
static ref NON_VOLATILE_FUNCS: FxHashSet<Identifier> = {
fn non_volatile_funcs() -> &'static FxHashSet<Identifier> {
static NON_VOLATILE_FUNCS: OnceLock<FxHashSet<Identifier>> = OnceLock::new();
NON_VOLATILE_FUNCS.get_or_init(|| {
NON_VOLATILE_BUILT_IN_FUNCTIONS
.split('\n')
.map(|x| x.trim())
.filter(|x| !x.is_empty())
.map(Identifier::new)
.collect()
};
})
}

fn is_non_volatile_or_const(expr: &ast::Expr) -> bool {
Expand All @@ -40,7 +42,7 @@ fn is_non_volatile_or_const(expr: &ast::Expr) -> bool {
};

let non_volatile_name =
NON_VOLATILE_FUNCS.contains(&Identifier::new(name_ref.text().as_str()));
non_volatile_funcs().contains(&Identifier::new(name_ref.text().as_str()));

no_args && non_volatile_name
} else {
Expand Down
23 changes: 12 additions & 11 deletions crates/squawk_linter/src/rules/ban_char_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ use squawk_syntax::{
use crate::visitors::check_not_allowed_types;
use crate::{Edit, Fix, Linter, Rule, Violation};

use lazy_static::lazy_static;

lazy_static! {
static ref CHAR_TYPES: FxHashSet<Identifier> = [
Identifier::new("char"),
Identifier::new("character"),
Identifier::new("bpchar"),
]
.into_iter()
.collect();
use std::sync::OnceLock;

fn char_types() -> &'static FxHashSet<Identifier> {
static CHAR_TYPES: OnceLock<FxHashSet<Identifier>> = OnceLock::new();
CHAR_TYPES.get_or_init(|| {
FxHashSet::from_iter([
Identifier::new("char"),
Identifier::new("character"),
Identifier::new("bpchar"),
])
})
}

fn is_char_type(x: TokenText<'_>) -> bool {
CHAR_TYPES.contains(&Identifier::new(x.as_ref()))
char_types().contains(&Identifier::new(x.as_ref()))
}

fn create_fix(range: TextRange, args: Option<ast::ArgList>) -> Fix {
Expand Down
27 changes: 14 additions & 13 deletions crates/squawk_linter/src/rules/prefer_bigint_over_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ use crate::{Edit, Fix, Linter, Rule, Violation};
use crate::visitors::check_not_allowed_types;
use crate::visitors::is_not_valid_int_type;

use lazy_static::lazy_static;

lazy_static! {
static ref INT_TYPES: FxHashSet<Identifier> = [
Identifier::new("int"),
Identifier::new("integer"),
Identifier::new("int4"),
Identifier::new("serial"),
Identifier::new("serial4"),
]
.into_iter()
.collect();
use std::sync::OnceLock;

fn int_types() -> &'static FxHashSet<Identifier> {
static INT_TYPES: OnceLock<FxHashSet<Identifier>> = OnceLock::new();
INT_TYPES.get_or_init(|| {
FxHashSet::from_iter([
Identifier::new("int"),
Identifier::new("integer"),
Identifier::new("int4"),
Identifier::new("serial"),
Identifier::new("serial4"),
])
})
}

fn int_to_bigint_replacement(int_type: &str) -> &'static str {
Expand All @@ -44,7 +45,7 @@ fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {

fn check_ty_for_big_int(ctx: &mut Linter, ty: Option<ast::Type>) {
if let Some(ty) = ty {
if is_not_valid_int_type(&ty, &INT_TYPES) {
if is_not_valid_int_type(&ty, int_types()) {
let fix = create_bigint_fix(&ty);

ctx.report(
Expand Down
25 changes: 13 additions & 12 deletions crates/squawk_linter/src/rules/prefer_bigint_over_smallint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ use crate::{Edit, Fix, Linter, Rule, Violation};
use crate::visitors::check_not_allowed_types;
use crate::visitors::is_not_valid_int_type;

use lazy_static::lazy_static;

lazy_static! {
static ref SMALL_INT_TYPES: FxHashSet<Identifier> = [
Identifier::new("smallint"),
Identifier::new("int2"),
Identifier::new("smallserial"),
Identifier::new("serial2"),
]
.into_iter()
.collect();
use std::sync::OnceLock;

fn small_int_types() -> &'static FxHashSet<Identifier> {
static SMALL_INT_TYPES: OnceLock<FxHashSet<Identifier>> = OnceLock::new();
SMALL_INT_TYPES.get_or_init(|| {
FxHashSet::from_iter([
Identifier::new("smallint"),
Identifier::new("int2"),
Identifier::new("smallserial"),
Identifier::new("serial2"),
])
})
}

fn smallint_to_bigint(smallint_type: &str) -> &'static str {
Expand All @@ -43,7 +44,7 @@ fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {

fn check_ty_for_small_int(ctx: &mut Linter, ty: Option<ast::Type>) {
if let Some(ty) = ty {
if is_not_valid_int_type(&ty, &SMALL_INT_TYPES) {
if is_not_valid_int_type(&ty, small_int_types()) {
let fix = create_bigint_fix(&ty);

ctx.report(
Expand Down
27 changes: 14 additions & 13 deletions crates/squawk_linter/src/rules/prefer_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ use squawk_syntax::{

use crate::{Edit, Fix, Linter, Rule, Violation};

use lazy_static::lazy_static;
use std::sync::OnceLock;

use crate::visitors::{check_not_allowed_types, is_not_valid_int_type};

lazy_static! {
static ref SERIAL_TYPES: FxHashSet<Identifier> = [
Identifier::new("serial"),
Identifier::new("serial2"),
Identifier::new("serial4"),
Identifier::new("serial8"),
Identifier::new("smallserial"),
Identifier::new("bigserial"),
]
.into_iter()
.collect();
fn serial_types() -> &'static FxHashSet<Identifier> {
static SERIAL_TYPES: OnceLock<FxHashSet<Identifier>> = OnceLock::new();
SERIAL_TYPES.get_or_init(|| {
FxHashSet::from_iter([
Identifier::new("serial"),
Identifier::new("serial2"),
Identifier::new("serial4"),
Identifier::new("serial8"),
Identifier::new("smallserial"),
Identifier::new("bigserial"),
])
})
}

fn replace_serial(serial_type: &str) -> &'static str {
Expand All @@ -43,7 +44,7 @@ fn create_identity_fix(ty: &ast::Type) -> Option<Fix> {

fn check_ty_for_serial(ctx: &mut Linter, ty: Option<ast::Type>) {
if let Some(ty) = ty {
if is_not_valid_int_type(&ty, &SERIAL_TYPES) {
if is_not_valid_int_type(&ty, serial_types()) {
let fix = create_identity_fix(&ty);

ctx.report(
Expand Down
8 changes: 0 additions & 8 deletions crates/squawk_linter/src/rules/prefer_text_field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use rustc_hash::FxHashSet;

use squawk_syntax::{
Parse, SourceFile,
ast::{self, AstNode},
Expand All @@ -10,12 +8,6 @@ use crate::{Edit, Fix, Linter, Rule, Violation};

use crate::visitors::check_not_allowed_types;

use lazy_static::lazy_static;

lazy_static! {
static ref VARCHAR_TYPE_NAMES: FxHashSet<&'static str> = ["varchar"].into_iter().collect();
}

fn is_not_allowed_varchar(ty: &ast::Type) -> bool {
match ty {
ast::Type::ArrayType(array_type) => {
Expand Down
Loading