1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-08-01 04:57:44 +00:00

refactor: solve clippy warnings

This commit is contained in:
Kevin Amado 2022-02-21 14:12:46 -05:00
parent f9ba9b9192
commit 4cdee48ddf
12 changed files with 65 additions and 66 deletions

View file

@ -78,3 +78,12 @@ steps:
command: command:
- echo +++ - echo +++
- nix flake check - nix flake check
- label: lint
if: build.branch != "main"
command:
- echo --- Load environment
- direnv allow
- eval "$(direnv export bash)"
- echo +++ Run Linter
- cargo clippy

View file

@ -44,8 +44,8 @@ pub fn build(
config.clone(), config.clone(),
force_wide, force_wide,
path, path,
crate::position::Position::new(), crate::position::Position::default(),
crate::position::Position::new(), crate::position::Position::default(),
); );
build_step( build_step(

View file

@ -126,16 +126,13 @@ impl Children {
pub fn drain_comment<F: FnMut(String)>(&mut self, mut callback: F) { pub fn drain_comment<F: FnMut(String)>(&mut self, mut callback: F) {
if let Some(child) = self.peek_next() { if let Some(child) = self.peek_next() {
match child.element.kind() { if let rnix::SyntaxKind::TOKEN_COMMENT = child.element.kind() {
rnix::SyntaxKind::TOKEN_COMMENT => {
callback(dedent_comment( callback(dedent_comment(
&child.pos, &child.pos,
child.element.into_token().unwrap().text(), child.element.into_token().unwrap().text(),
)); ));
self.move_next(); self.move_next();
} }
_ => {}
}
} }
} }

View file

@ -150,8 +150,8 @@ pub fn tui(
while !finished { while !finished {
loop { loop {
match receiver.try_recv() { if let Ok(event) = receiver.try_recv() {
Ok(event) => match event { match event {
Event::FormattedPath(formatted_path) => { Event::FormattedPath(formatted_path) => {
match formatted_path.result { match formatted_path.result {
Ok(changed) => { Ok(changed) => {
@ -175,20 +175,14 @@ pub fn tui(
finished = true; finished = true;
} }
Event::Input(key) => { Event::Input(key) => {
match key { if let termion::event::Key::Ctrl('c') = key {
termion::event::Key::Ctrl('c') => { return Err(std::io::ErrorKind::Interrupted.into());
return Err(
std::io::ErrorKind::Interrupted.into()
);
} }
_ => {}
};
} }
Event::Tick => { Event::Tick => {
break; break;
} }
}, }
Err(_) => {}
} }
} }

View file

@ -9,11 +9,13 @@ pub struct Config {
layout: Layout, layout: Layout,
} }
impl Config { impl Default for Config {
pub fn new() -> Config { fn default() -> Config {
Config { layout: Layout::Tall } Config { layout: Layout::Tall }
} }
}
impl Config {
pub fn layout(&self) -> &Layout { pub fn layout(&self) -> &Layout {
&self.layout &self.layout
} }

View file

@ -1,7 +1,7 @@
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let matches = alejandra::cli::parse(std::env::args().collect()); 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") { match matches.values_of("paths") {
Some(paths) => { Some(paths) => {

View file

@ -4,11 +4,13 @@ pub struct Position {
pub line: usize, pub line: usize,
} }
impl Position { impl Default for Position {
pub fn new() -> Position { fn default() -> Position {
Position { column: 0, line: 1 } Position { column: 0, line: 1 }
} }
}
impl Position {
pub fn update(&mut self, text: &str) { pub fn update(&mut self, text: &str) {
let chars: Vec<char> = text.chars().collect(); let chars: Vec<char> = text.chars().collect();
let newlines = chars.iter().filter(|&c| *c == '\n').count(); let newlines = chars.iter().filter(|&c| *c == '\n').count();

View file

@ -28,16 +28,14 @@ pub fn rule_with_configuration(
crate::config::Layout::Tall => { crate::config::Layout::Tall => {
let kind = child.element.kind(); let kind = child.element.kind();
if parent_kind == "bin_op_and_or_default" if (parent_kind == "bin_op_and_or_default"
&& matches!( && matches!(
kind, kind,
rnix::SyntaxKind::NODE_BIN_OP rnix::SyntaxKind::NODE_BIN_OP
| rnix::SyntaxKind::NODE_OR_DEFAULT | rnix::SyntaxKind::NODE_OR_DEFAULT
) ))
{ || (parent_kind == "select"
steps.push_back(crate::builder::Step::Format(child.element)); && matches!(kind, rnix::SyntaxKind::NODE_SELECT))
} else if parent_kind == "select"
&& matches!(kind, rnix::SyntaxKind::NODE_SELECT)
{ {
steps.push_back(crate::builder::Step::Format(child.element)); steps.push_back(crate::builder::Step::Format(child.element));
} else { } else {

View file

@ -36,13 +36,8 @@ pub fn rule(
if crate::builder::fits_in_single_line( if crate::builder::fits_in_single_line(
build_ctx, build_ctx,
child.element.clone(), child.element.clone(),
) { ) || (branch == "else"
steps.push_back(crate::builder::Step::Whitespace); && child.element.kind() == rnix::SyntaxKind::NODE_IF_ELSE)
steps.push_back(crate::builder::Step::FormatWider(
child.element,
));
} else if branch == "else"
&& child.element.kind() == rnix::SyntaxKind::NODE_IF_ELSE
{ {
steps.push_back(crate::builder::Step::Whitespace); steps.push_back(crate::builder::Step::Whitespace);
steps.push_back(crate::builder::Step::FormatWider( steps.push_back(crate::builder::Step::FormatWider(

View file

@ -83,8 +83,7 @@ pub fn rule(
steps.push_back(crate::builder::Step::Indent); steps.push_back(crate::builder::Step::Indent);
steps.push_back(crate::builder::Step::NewLine); steps.push_back(crate::builder::Step::NewLine);
steps.push_back(crate::builder::Step::Pad); steps.push_back(crate::builder::Step::Pad);
} else if false } else if matches!(
|| matches!(
child_expr.element.kind(), child_expr.element.kind(),
rnix::SyntaxKind::NODE_ASSERT rnix::SyntaxKind::NODE_ASSERT
| rnix::SyntaxKind::NODE_ATTR_SET | rnix::SyntaxKind::NODE_ATTR_SET
@ -94,8 +93,7 @@ pub fn rule(
| rnix::SyntaxKind::NODE_LIST | rnix::SyntaxKind::NODE_LIST
| rnix::SyntaxKind::NODE_STRING | rnix::SyntaxKind::NODE_STRING
| rnix::SyntaxKind::NODE_WITH | rnix::SyntaxKind::NODE_WITH
) ) || (matches!(
|| (matches!(
child_expr.element.kind(), child_expr.element.kind(),
rnix::SyntaxKind::NODE_APPLY rnix::SyntaxKind::NODE_APPLY
) && !newlines) ) && !newlines)

View file

@ -10,11 +10,13 @@ pub fn rule(
let items_count = node let items_count = node
.children() .children()
.filter(|element| match element.kind() { .filter(|element| {
matches!(
element.kind(),
rnix::SyntaxKind::NODE_KEY_VALUE rnix::SyntaxKind::NODE_KEY_VALUE
| rnix::SyntaxKind::NODE_INHERIT | rnix::SyntaxKind::NODE_INHERIT
| rnix::SyntaxKind::NODE_INHERIT_FROM => true, | rnix::SyntaxKind::NODE_INHERIT_FROM
_ => false, )
}) })
.count(); .count();

View file

@ -21,10 +21,12 @@ pub fn rule(
let items_count = node let items_count = node
.children_with_tokens() .children_with_tokens()
.filter(|element| match element.kind() { .filter(|element| {
matches!(
element.kind(),
rnix::SyntaxKind::TOKEN_ELLIPSIS rnix::SyntaxKind::TOKEN_ELLIPSIS
| rnix::SyntaxKind::NODE_PAT_ENTRY => true, | rnix::SyntaxKind::NODE_PAT_ENTRY
_ => false, )
}) })
.count(); .count();