Skip to content

Commit

Permalink
Clippy fix (#83)
Browse files Browse the repository at this point in the history
* clippy fixes

* chore: fixing nested match statements

* fixes

* clippy fixes

* fix more lints

* clippy

* chore: fix unncessary_unwrap

* chore: useless asref

* chore: borrow deref and clone on copy

* chore: fix a bunch of clippy warnings

* test thread 1
  • Loading branch information
JasonShin authored Nov 5, 2023
1 parent bca6bd0 commit f8b291f
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 189 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/clippy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: clippy

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
CARGO_TERM_COLOR: always

jobs:
clippy:
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- run: rustup toolchain install stable --profile minimal

- run: rustup component add clippy

- uses: Swatinem/rust-cache@v2
with:
# To only cache runs from `master`:
save-if: ${{ github.ref == 'refs/heads/master' }}
# Specifies what to use as the backend providing cache
# Can be set to either "github" or "buildjet"
# default: "github"
cache-provider: "github"

- name: Run clippy
run: cargo clippy
2 changes: 1 addition & 1 deletion .github/workflows/rust-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
run: docker ps -a

- name: Run tests
run: cargo test
run: cargo test -- --test-threads=1
8 changes: 4 additions & 4 deletions src/common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Config {
}

let file_based_ignore_config = &file_based_ignore_config.unwrap();
let file_based_ignore_config = file_based_ignore_config.split("\n");
let file_based_ignore_config = file_based_ignore_config.split('\n');
let file_based_ignore_config: Vec<&str> = file_based_ignore_config.clone().collect();

let custom_ignore_configs = &file_based_ignore_config
Expand Down Expand Up @@ -128,9 +128,9 @@ impl Config {
});
}
// If the file config is not provided, we will return the CLI arg's default values
return Some(cli_default);
Some(cli_default)
} else {
return Some(cli_default);
Some(cli_default)
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ impl Config {
return detected_conn_name.to_string();
}

return "default".to_string();
"default".to_string()
}

pub fn get_postgres_cred(&self, conn: &DbConnectionConfig) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/common/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ lazy_static! {
let db_type = connection_config.db_type.to_owned();
let conn = match db_type {
DatabaseType::Mysql => {
let opts = CONFIG.get_mysql_cred(&connection_config);
let opts = CONFIG.get_mysql_cred(connection_config);
let mut conn = MySQLConn::new(opts).unwrap();
DBConn::MySQLPooledConn(Mutex::new(conn))
}
DatabaseType::Postgres => {
let postgres_cred = &CONFIG.get_postgres_cred(&connection_config);
let postgres_cred = &CONFIG.get_postgres_cred(connection_config);
DBConn::PostgresConn(Mutex::new(PGClient::connect(postgres_cred, PGNoTls).unwrap()))
}
};
Expand Down
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod lazy;
pub mod types;

// Source Parser
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone)]
pub struct SQL {
/// Note that not all sql`` statements belong to a variable expression, therefore we must store it as an option
Expand Down
4 changes: 2 additions & 2 deletions src/core/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl DBConn {
handler: &Handler,
) -> Result<(bool, Option<TsQuery>)> {
let (explain_failed, ts_query) = match &self {
DBConn::MySQLPooledConn(_conn) => mysql_explain::prepare(&self, sql, should_generate_types, handler)?,
DBConn::PostgresConn(_conn) => postgres_explain::prepare(&self, sql, should_generate_types, handler)?,
DBConn::MySQLPooledConn(_conn) => mysql_explain::prepare(self, sql, should_generate_types, handler)?,
DBConn::PostgresConn(_conn) => postgres_explain::prepare(self, sql, should_generate_types, handler)?,
};

Ok((explain_failed, ts_query))
Expand Down
6 changes: 3 additions & 3 deletions src/core/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub fn execute(queries: &HashMap<PathBuf, Vec<SQL>>, handler: &Handler) -> Resul
let connection = &connection.get_connection(&sql.query).clone();
let connection = &connection.lock().unwrap();

let (explain_failed, ts_query) = &connection.prepare(&sql, &should_generate_types, &handler)?;
let (explain_failed, ts_query) = &connection.prepare(sql, should_generate_types, handler)?;

// If any prepare statement fails, we should set the failed flag as true
failed = explain_failed.clone();
failed = *explain_failed;

if *should_generate_types {
let ts_query = &ts_query.clone().expect("Failed to generate types from query");
Expand All @@ -42,7 +42,7 @@ pub fn execute(queries: &HashMap<PathBuf, Vec<SQL>>, handler: &Handler) -> Resul
}

if *should_generate_types {
let is_sqls_empty = sqls_to_write.len() == 0;
let is_sqls_empty = sqls_to_write.is_empty();
let sqls_to_write = sqls_to_write.join("\n");

if is_sqls_empty {
Expand Down
4 changes: 2 additions & 2 deletions src/core/mysql/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn prepare(
let span = sql.span.to_owned();
let explain_query = format!("PREPARE stmt FROM \"{}\"", sql.query);

let mut conn = match &db_conn {
let conn = match &db_conn {
DBConn::MySQLPooledConn(conn) => conn,
_ => panic!("Invalid connection type"),
};
Expand All @@ -38,7 +38,7 @@ pub fn prepare(
let mut ts_query = None;

if should_generate_types == &true {
ts_query = Some(generate_ts_interface(sql, &db_conn)?);
ts_query = Some(generate_ts_interface(sql, db_conn)?);
}

Ok((failed, ts_query))
Expand Down
2 changes: 1 addition & 1 deletion src/core/postgres/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn prepare(
let mut ts_query = None;

if should_generate_types == &true {
ts_query = Some(generate_ts_interface(sql, &db_conn)?);
ts_query = Some(generate_ts_interface(sql, db_conn)?);
}

Ok((failed, ts_query))
Expand Down
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#![deny(
clippy::correctness,
clippy::style,
clippy::perf,
clippy::unnecessary_unwrap,
clippy::bool_comparison,
clippy::useless_asref,
clippy::borrow_deref_ref,
clippy::clone_on_copy,
clippy::extra_unused_lifetimes,
clippy::explicit_auto_deref
)]
#![allow(clippy::ptr_arg)]
mod common;
mod core;
mod parser;
Expand Down Expand Up @@ -44,7 +57,7 @@ fn main() -> Result<()> {
clear_single_ts_file_if_exists()?;

for file_path in files.iter() {
let (sqls, handler) = parse_source(&file_path)?;
let (sqls, handler) = parse_source(file_path)?;
let failed = execute(&sqls, &handler)?;
if failed {
eprint!("SQLs failed to compile!");
Expand Down
38 changes: 19 additions & 19 deletions src/parser/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@ use super::{
tag::{get_sql_from_expr, get_sql_from_var_decl},
};

fn process_class_member(mut sqls: &mut Vec<SQL>, body_stmt: &ClassMember, import_alias: &String) -> Result<()> {
fn process_class_member(sqls: &mut Vec<SQL>, body_stmt: &ClassMember, import_alias: &String) -> Result<()> {
match body_stmt {
ClassMember::Constructor(constructor) => {
if let Some(body) = &constructor.body {
for stmt in &body.stmts {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
}
ClassMember::Method(class_method) => {
if let Some(body) = &class_method.function.body {
for stmt in &body.stmts {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
}
ClassMember::PrivateMethod(private_method) => {
if let Some(body) = &private_method.function.body {
for stmt in &body.stmts {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
}
ClassMember::StaticBlock(static_block) => {
for stmt in &static_block.body.stmts {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
ClassMember::PrivateProp(private_prop) => {
if let Some(expr) = &private_prop.value {
let span: MultiSpan = private_prop.span.into();
get_sql_from_expr(&mut sqls, &None, &expr.clone(), &span, import_alias);
get_sql_from_expr(sqls, &None, &expr.clone(), &span, import_alias);
}
}
ClassMember::ClassProp(class_prop) => {
if let Some(expr) = &class_prop.value {
let span: MultiSpan = class_prop.span.into();
get_sql_from_expr(&mut sqls, &None, &expr.clone(), &span, import_alias);
get_sql_from_expr(sqls, &None, &expr.clone(), &span, import_alias);
}
}
ClassMember::AutoAccessor(auto_accessor) => {
Expand All @@ -55,8 +55,8 @@ fn process_class_member(mut sqls: &mut Vec<SQL>, body_stmt: &ClassMember, import

if let Some(expr) = &value {
let span: MultiSpan = auto_accessor.span.into();
let var_decl_name = get_var_decl_name_from_key(&key);
get_sql_from_expr(&mut sqls, &var_decl_name, expr, &span, import_alias);
let var_decl_name = get_var_decl_name_from_key(key);
get_sql_from_expr(sqls, &var_decl_name, expr, &span, import_alias);
}
}
ClassMember::TsIndexSignature(_) => {}
Expand All @@ -65,20 +65,20 @@ fn process_class_member(mut sqls: &mut Vec<SQL>, body_stmt: &ClassMember, import
Ok(())
}

pub fn process_default_decl(mut sqls: &mut Vec<SQL>, default_decl: &DefaultDecl, import_alias: &String) -> Result<()> {
pub fn process_default_decl(sqls: &mut Vec<SQL>, default_decl: &DefaultDecl, import_alias: &String) -> Result<()> {
match default_decl {
DefaultDecl::Class(class) => {
let class_body = &class.class.body;
for body_stmt in class_body {
process_class_member(sqls, &body_stmt, import_alias)?;
process_class_member(sqls, body_stmt, import_alias)?;
}
}
DefaultDecl::Fn(func) => {
let body = &func.function.body;

if let Some(body) = body {
for stmt in &body.stmts {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
}
Expand All @@ -87,31 +87,31 @@ pub fn process_default_decl(mut sqls: &mut Vec<SQL>, default_decl: &DefaultDecl,
Ok(())
}

pub fn process_class_decl(mut sqls: &mut Vec<SQL>, class: &ClassDecl, import_alias: &String) -> Result<()> {
pub fn process_class_decl(sqls: &mut Vec<SQL>, class: &ClassDecl, import_alias: &String) -> Result<()> {
let class_body = &class.class.body;
let class_decorators = &class.class.decorators;

for decorator in class_decorators {
let expr = &decorator.expr;
let span: MultiSpan = decorator.span.into();
get_sql_from_expr(&mut sqls, &None, expr, &span, import_alias);
get_sql_from_expr(sqls, &None, expr, &span, import_alias);
}

for body_stmt in class_body {
process_class_member(sqls, &body_stmt, import_alias)?;
process_class_member(sqls, body_stmt, import_alias)?;
}
Ok(())
}

pub fn process_decl(mut sqls: &mut Vec<SQL>, decl: &Decl, import_alias: &String) -> Result<()> {
match decl {
Decl::Class(class) => {
process_class_decl(&mut sqls, class, import_alias)?;
process_class_decl(sqls, class, import_alias)?;
}
Decl::Fn(fun) => {
if let Some(body) = &fun.function.body {
for stmt in &body.stmts {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
}
Expand All @@ -133,7 +133,7 @@ pub fn process_decl(mut sqls: &mut Vec<SQL>, decl: &Decl, import_alias: &String)
// const [rows, i] = await connection.execute....
if let Some(init) = &var_decl.init {
let expr = *init.clone();
get_sql_from_expr(&mut sqls, &name, &expr, &span, import_alias);
get_sql_from_expr(sqls, &name, &expr, &span, import_alias);
}
}
}
Expand All @@ -146,7 +146,7 @@ pub fn process_decl(mut sqls: &mut Vec<SQL>, decl: &Decl, import_alias: &String)
for body in &block.body {
let stmt = &body.clone().stmt();
if let Some(stmt) = stmt {
recurse_and_find_sql(&mut sqls, stmt, import_alias)?;
recurse_and_find_sql(sqls, stmt, import_alias)?;
}
}
}
Expand Down
29 changes: 13 additions & 16 deletions src/parser/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@ pub fn find_sqlx_import_alias(import_decl: &ImportDecl) -> Option<String> {

if src == "sqlx-ts" {
for specifier in &import_decl.specifiers {
match specifier {
ImportSpecifier::Named(import_named_specifier) => {
// for exampleL
// import { sql: aliased } from 'sqlx-ts' <<< should satisfy following
if let Some(imported) = &import_named_specifier.imported {
match imported {
ModuleExportName::Ident(ident) => {
if ident.sym.to_string() == "sql" {
name = Some(import_named_specifier.local.sym.to_string())
}
if let ImportSpecifier::Named(import_named_specifier) = specifier {
// for exampleL
// import { sql: aliased } from 'sqlx-ts' <<< should satisfy following
if let Some(imported) = &import_named_specifier.imported {
match imported {
ModuleExportName::Ident(ident) => {
if ident.sym.to_string() == "sql" {
name = Some(import_named_specifier.local.sym.to_string())
}
_ => continue,
}
// for example:
// import { sql } from 'sqlx-ts' <<< should satisfy following
} else if &import_named_specifier.local.sym.to_string() == "sql" {
name = Some("sql".to_string())
_ => continue,
}
// for example:
// import { sql } from 'sqlx-ts' <<< should satisfy following
} else if &import_named_specifier.local.sym.to_string() == "sql" {
name = Some("sql".to_string())
}
_ => {}
}
}
}
Expand Down
Loading

0 comments on commit f8b291f

Please sign in to comment.