diff --git a/buildkite.yaml b/buildkite.yaml index 137e5bb..bac873a 100644 --- a/buildkite.yaml +++ b/buildkite.yaml @@ -78,3 +78,12 @@ steps: command: - echo +++ - nix flake check + + - label: lint + if: build.branch != "main" + command: + - echo --- Load environment + - direnv allow + - eval "$(direnv export bash)" + - echo +++ Run Linter + - cargo clippy diff --git a/src/builder.rs b/src/builder.rs index 6e1001f..65eb68d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -44,8 +44,8 @@ pub fn build( config.clone(), force_wide, path, - crate::position::Position::new(), - crate::position::Position::new(), + crate::position::Position::default(), + crate::position::Position::default(), ); build_step( diff --git a/src/children.rs b/src/children.rs index b371458..928515f 100644 --- a/src/children.rs +++ b/src/children.rs @@ -126,15 +126,12 @@ impl Children { pub fn drain_comment(&mut self, mut callback: F) { if let Some(child) = self.peek_next() { - match child.element.kind() { - rnix::SyntaxKind::TOKEN_COMMENT => { - callback(dedent_comment( - &child.pos, - child.element.into_token().unwrap().text(), - )); - self.move_next(); - } - _ => {} + if let rnix::SyntaxKind::TOKEN_COMMENT = child.element.kind() { + callback(dedent_comment( + &child.pos, + child.element.into_token().unwrap().text(), + )); + self.move_next(); } } } diff --git a/src/cli.rs b/src/cli.rs index c87ef89..67013d2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -150,8 +150,8 @@ pub fn tui( while !finished { loop { - match receiver.try_recv() { - Ok(event) => match event { + if let Ok(event) = receiver.try_recv() { + match event { Event::FormattedPath(formatted_path) => { match formatted_path.result { Ok(changed) => { @@ -175,20 +175,14 @@ pub fn tui( finished = true; } Event::Input(key) => { - match key { - termion::event::Key::Ctrl('c') => { - return Err( - std::io::ErrorKind::Interrupted.into() - ); - } - _ => {} - }; + if let termion::event::Key::Ctrl('c') = key { + return Err(std::io::ErrorKind::Interrupted.into()); + } } Event::Tick => { break; } - }, - Err(_) => {} + } } } diff --git a/src/config.rs b/src/config.rs index bbc823c..317c1ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,11 +9,13 @@ pub struct Config { layout: Layout, } -impl Config { - pub fn new() -> Config { +impl Default for Config { + fn default() -> Config { Config { layout: Layout::Tall } } +} +impl Config { pub fn layout(&self) -> &Layout { &self.layout } diff --git a/src/main.rs b/src/main.rs index 7fb15fe..e58f25d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ fn main() -> std::io::Result<()> { let matches = alejandra::cli::parse(std::env::args().collect()); - let config = alejandra::config::Config::new(); + let config = alejandra::config::Config::default(); match matches.values_of("paths") { Some(paths) => { diff --git a/src/position.rs b/src/position.rs index 6dfb247..6012d64 100644 --- a/src/position.rs +++ b/src/position.rs @@ -4,11 +4,13 @@ pub struct Position { pub line: usize, } -impl Position { - pub fn new() -> Position { +impl Default for Position { + fn default() -> Position { Position { column: 0, line: 1 } } +} +impl Position { pub fn update(&mut self, text: &str) { let chars: Vec = text.chars().collect(); let newlines = chars.iter().filter(|&c| *c == '\n').count(); diff --git a/src/rules/bin_op_and_or_default.rs b/src/rules/bin_op_and_or_default.rs index efa2aaa..15c6107 100644 --- a/src/rules/bin_op_and_or_default.rs +++ b/src/rules/bin_op_and_or_default.rs @@ -28,16 +28,14 @@ pub fn rule_with_configuration( crate::config::Layout::Tall => { let kind = child.element.kind(); - if parent_kind == "bin_op_and_or_default" + if (parent_kind == "bin_op_and_or_default" && matches!( kind, rnix::SyntaxKind::NODE_BIN_OP | rnix::SyntaxKind::NODE_OR_DEFAULT - ) - { - steps.push_back(crate::builder::Step::Format(child.element)); - } else if parent_kind == "select" - && matches!(kind, rnix::SyntaxKind::NODE_SELECT) + )) + || (parent_kind == "select" + && matches!(kind, rnix::SyntaxKind::NODE_SELECT)) { steps.push_back(crate::builder::Step::Format(child.element)); } else { diff --git a/src/rules/if_else.rs b/src/rules/if_else.rs index 37f0c75..9a10f1e 100644 --- a/src/rules/if_else.rs +++ b/src/rules/if_else.rs @@ -36,13 +36,8 @@ pub fn rule( if crate::builder::fits_in_single_line( build_ctx, child.element.clone(), - ) { - steps.push_back(crate::builder::Step::Whitespace); - steps.push_back(crate::builder::Step::FormatWider( - child.element, - )); - } else if branch == "else" - && child.element.kind() == rnix::SyntaxKind::NODE_IF_ELSE + ) || (branch == "else" + && child.element.kind() == rnix::SyntaxKind::NODE_IF_ELSE) { steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::FormatWider( diff --git a/src/rules/key_value.rs b/src/rules/key_value.rs index e8b06fb..a89bbb8 100644 --- a/src/rules/key_value.rs +++ b/src/rules/key_value.rs @@ -83,22 +83,20 @@ pub fn rule( steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::Pad); - } else if false - || matches!( - child_expr.element.kind(), - rnix::SyntaxKind::NODE_ASSERT - | rnix::SyntaxKind::NODE_ATTR_SET - | rnix::SyntaxKind::NODE_PAREN - | rnix::SyntaxKind::NODE_LAMBDA - | rnix::SyntaxKind::NODE_LET_IN - | rnix::SyntaxKind::NODE_LIST - | rnix::SyntaxKind::NODE_STRING - | rnix::SyntaxKind::NODE_WITH - ) - || (matches!( - child_expr.element.kind(), - rnix::SyntaxKind::NODE_APPLY - ) && !newlines) + } else if matches!( + child_expr.element.kind(), + rnix::SyntaxKind::NODE_ASSERT + | rnix::SyntaxKind::NODE_ATTR_SET + | rnix::SyntaxKind::NODE_PAREN + | rnix::SyntaxKind::NODE_LAMBDA + | rnix::SyntaxKind::NODE_LET_IN + | rnix::SyntaxKind::NODE_LIST + | rnix::SyntaxKind::NODE_STRING + | rnix::SyntaxKind::NODE_WITH + ) || (matches!( + child_expr.element.kind(), + rnix::SyntaxKind::NODE_APPLY + ) && !newlines) { steps.push_back(crate::builder::Step::Whitespace); } else { diff --git a/src/rules/let_in.rs b/src/rules/let_in.rs index 10e1fc1..bcc1a48 100644 --- a/src/rules/let_in.rs +++ b/src/rules/let_in.rs @@ -10,11 +10,13 @@ pub fn rule( let items_count = node .children() - .filter(|element| match element.kind() { - rnix::SyntaxKind::NODE_KEY_VALUE - | rnix::SyntaxKind::NODE_INHERIT - | rnix::SyntaxKind::NODE_INHERIT_FROM => true, - _ => false, + .filter(|element| { + matches!( + element.kind(), + rnix::SyntaxKind::NODE_KEY_VALUE + | rnix::SyntaxKind::NODE_INHERIT + | rnix::SyntaxKind::NODE_INHERIT_FROM + ) }) .count(); diff --git a/src/rules/pattern.rs b/src/rules/pattern.rs index 290a98d..2aee8fb 100644 --- a/src/rules/pattern.rs +++ b/src/rules/pattern.rs @@ -21,10 +21,12 @@ pub fn rule( let items_count = node .children_with_tokens() - .filter(|element| match element.kind() { - rnix::SyntaxKind::TOKEN_ELLIPSIS - | rnix::SyntaxKind::NODE_PAT_ENTRY => true, - _ => false, + .filter(|element| { + matches!( + element.kind(), + rnix::SyntaxKind::TOKEN_ELLIPSIS + | rnix::SyntaxKind::NODE_PAT_ENTRY + ) }) .count();