From a3b2bd98332475657d2d986cb26e9f5540531857 Mon Sep 17 00:00:00 2001 From: Jay Bosamiya Date: Thu, 21 Mar 2024 14:47:47 -0400 Subject: [PATCH 1/2] chore: cargo-clippy fixes --- src/lib.rs | 59 ++++++++++++++++---------------------- src/main.rs | 8 +++--- src/rustfmt.rs | 2 +- tests/rustfmt-matching.rs | 2 +- tests/snapshot-examples.rs | 2 +- 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae625ac..58576ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -466,9 +466,10 @@ fn loop_to_doc<'a>( // We need to add a newline after the very last clause, // so that the opening brace of the loop body is on a fresh line let mut last_clause_span = None; - pair.clone().into_inner().for_each(|p| match p.as_rule() { - Rule::loop_clause => last_clause_span = Some(p.as_span()), - _ => (), + pair.clone().into_inner().for_each(|p| { + if p.as_rule() == Rule::loop_clause { + last_clause_span = Some(p.as_span()) + } }); arena.concat(pair.into_inner().map(|p| { if p.as_rule() == Rule::condition { @@ -505,11 +506,7 @@ fn is_prefix_triple(pair: Pair) -> bool { None => false, Some(pair) => pair .into_inner() - .find(|p| { - matches!(p.as_rule(), Rule::triple_and) - || matches!(p.as_rule(), Rule::triple_or) - }) - .is_some(), + .any(|p| matches!(p.as_rule(), Rule::triple_and | Rule::triple_or)), }, } } @@ -527,10 +524,9 @@ fn is_bare_trigger(pair: Pair) -> bool { .find(|p| matches!(p.as_rule(), Rule::trigger_attribute)) { None => false, - Some(trigger) => trigger + Some(trigger) => !trigger .into_inner() - .find(|p| matches!(p.as_rule(), Rule::comma_delimited_exprs)) - .is_none(), + .any(|p| matches!(p.as_rule(), Rule::comma_delimited_exprs)), }, } } @@ -557,20 +553,20 @@ fn expr_only_block(r: Rule, pairs: &Pairs) -> bool { _ => 0, } }); - return count == -1; + count == -1 } /// Checks if a block terminates in an expression fn terminal_expr(pairs: &Pairs) -> bool { let e = pairs.clone().find(|p| matches!(p.as_rule(), Rule::expr)); - !e.is_none() + e.is_some() } fn debug_print(pair: Pair, indent: usize) { print!("{:indent$}{:?} {{", "", pair.as_rule(), indent = indent); let pairs = pair.into_inner(); if pairs.peek().is_some() { - print!("\n"); + println!(); pairs.for_each(|p| debug_print(p, indent + 2)); println!("{:indent$}}}", "", indent = indent); } else { @@ -829,18 +825,12 @@ fn to_doc<'a>( Rule::fn_terminator => map_to_doc(ctx, arena, pair), Rule::r#fn => { let pairs = pair.into_inner(); - let has_qualifier = pairs - .clone() - .find(|p| { - matches!(p.as_rule(), Rule::fn_qualifier) && p.clone().into_inner().count() > 0 - }) - .is_some(); - let has_ret_type = pairs - .clone() - .find(|p| { - matches!(p.as_rule(), Rule::ret_type) && p.clone().into_inner().count() > 0 - }) - .is_some(); + let has_qualifier = pairs.clone().any(|p| { + matches!(p.as_rule(), Rule::fn_qualifier) && p.clone().into_inner().count() > 0 + }); + let has_ret_type = pairs.clone().any(|p| { + matches!(p.as_rule(), Rule::ret_type) && p.clone().into_inner().count() > 0 + }); let mut saw_param_list = false; let mut saw_comment_after_param_list = false; let mut pre_ret_type = true; @@ -1104,8 +1094,7 @@ fn to_doc<'a>( let has_ret = pair .clone() .into_inner() - .find(|p| matches!(p.as_rule(), Rule::ret_type)) - .is_some(); + .any(|p| matches!(p.as_rule(), Rule::ret_type)); arena .concat(pair.into_inner().map(|p| { match p.as_rule() { @@ -1328,7 +1317,7 @@ fn find_inline_comment_lines(s: &str) -> HashSet { comment_lines.insert(line_num + 1); } } - return comment_lines; + comment_lines } fn is_inline_comment(s: &str) -> bool { @@ -1412,7 +1401,7 @@ fn fix_inline_comments(s: String) -> String { Some(ref c) => { // Replace our marker with the comment fixed_str += "\n"; - prev_str = line.replace(NONINLINE_COMMENT_DST, &c); + prev_str = line.replace(NONINLINE_COMMENT_DST, c); } } } else { @@ -1425,7 +1414,7 @@ fn fix_inline_comments(s: String) -> String { } fixed_str += &prev_str; fixed_str += "\n"; - return fixed_str; + fixed_str } fn strip_whitespace(s: String) -> String { @@ -1567,15 +1556,15 @@ pub fn parse_and_format(s: &str) -> miette::Result { let mut final_prefix_comment_is_multiline = false; for comment in &prefix_comments { formatted_output += comment.as_str(); - final_prefix_comment_is_multiline = is_multiline_comment(&comment); + final_prefix_comment_is_multiline = is_multiline_comment(comment); } - if prefix_comments.len() > 0 && final_prefix_comment_is_multiline { + if !prefix_comments.is_empty() && final_prefix_comment_is_multiline { formatted_output += "\n"; } formatted_output += &format_item(&ctx, body); - if suffix_comments.len() > 0 { + if !suffix_comments.is_empty() { formatted_output += "\n"; } for comment in suffix_comments { @@ -1616,7 +1605,7 @@ pub fn run(s: &str, opts: RunOptions) -> miette::Result { let file_name = opts.file_name.clone().unwrap_or("".into()); - let verus_fmted = parse_and_format(&unparsed_file).map_err(|e| { + let verus_fmted = parse_and_format(unparsed_file).map_err(|e| { e.with_source_code(miette::NamedSource::new( file_name, unparsed_file.to_owned(), diff --git a/src/main.rs b/src/main.rs index 8cbd62d..330555b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ fn format_file(file: &PathBuf, args: &Args) -> miette::Result<()> { if args.check { if unparsed_file == formatted_output { info!("✨Perfectly formatted✨"); - return Ok(()); + Ok(()) } else { info!("Found some differences in {}", file.display()); error!("Input found not to be well formatted"); @@ -65,7 +65,7 @@ fn format_file(file: &PathBuf, args: &Args) -> miette::Result<()> { )), ); println!("{diff}"); - return Err(miette!("invalid formatting")); + Err(miette!("invalid formatting")) } } else if matches!( args.unstable_command, @@ -104,7 +104,7 @@ fn format_file(file: &PathBuf, args: &Args) -> miette::Result<()> { fn main() -> miette::Result<()> { let args = Args::parse(); - if args.files.len() == 0 { + if args.files.is_empty() { return Err(miette!("No files specified")); } @@ -122,7 +122,7 @@ fn main() -> miette::Result<()> { let mut errors = vec![]; for file in &args.files { - match format_file(&file, &args) { + match format_file(file, &args) { Ok(()) => {} Err(e) => { errors.push(e); diff --git a/src/rustfmt.rs b/src/rustfmt.rs index 7923f4b..f4bc118 100644 --- a/src/rustfmt.rs +++ b/src/rustfmt.rs @@ -134,7 +134,7 @@ fn run_rustfmt(s: &str) -> Option { let output = proc.wait_with_output().ok()?; if output.status.success() { - Some(String::from_utf8(output.stdout).unwrap().into()) + Some(String::from_utf8(output.stdout).unwrap()) } else { eprintln!( "\nrustfmt failed! {}\n\tConsider running with --verus-only\n", diff --git a/tests/rustfmt-matching.rs b/tests/rustfmt-matching.rs index 63e59fd..f9a9d0b 100644 --- a/tests/rustfmt-matching.rs +++ b/tests/rustfmt-matching.rs @@ -15,7 +15,7 @@ fn compare(file: &str) { let diff = similar::udiff::unified_diff( similar::Algorithm::Patience, &rust_fmt, - &verus_inner, + verus_inner, 3, Some(("rust", "verus")), ); diff --git a/tests/snapshot-examples.rs b/tests/snapshot-examples.rs index 636c6bb..c988421 100644 --- a/tests/snapshot-examples.rs +++ b/tests/snapshot-examples.rs @@ -9,7 +9,7 @@ fn check_snapshot(original: &str) { if original != formatted { let diff = similar::udiff::unified_diff( similar::Algorithm::Patience, - &original, + original, &formatted, 3, Some(("original", "formatted")), From a3f02f6a06209d4c81cc8a6b11d9b48c1d8ada31 Mon Sep 17 00:00:00 2001 From: Jay Bosamiya Date: Thu, 21 Mar 2024 14:50:13 -0400 Subject: [PATCH 2/2] Add clippy check to CI --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 62969c1..9cd5226 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,5 +15,6 @@ jobs: run: rustup toolchain install stable --profile minimal --no-self-update - uses: Swatinem/rust-cache@v2 - run: cargo fmt --check --verbose + - run: cargo clippy --verbose - run: cargo build --verbose - run: cargo test --verbose