Skip to content

Commit

Permalink
Apply cargo clippy fixes, and enable clippy on CI (#45)
Browse files Browse the repository at this point in the history
This should help us maintain a slightly more idiomatic style. 

Clippy warnings _are_ ignorable via a targeted `#[allow(clippy::...)]`,
but I don't see any place in our codebase where clippy is suggesting a
bad change, so keeping it enabled should lead to maintenance of nicer
code overall.
  • Loading branch information
jaybosamiya authored Mar 25, 2024
2 parents c3009a3 + a3f02f6 commit 6712d10
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 24 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -505,11 +506,7 @@ fn is_prefix_triple(pair: Pair<Rule>) -> 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)),
},
}
}
Expand All @@ -527,10 +524,9 @@ fn is_bare_trigger(pair: Pair<Rule>) -> 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)),
},
}
}
Expand All @@ -557,20 +553,20 @@ fn expr_only_block(r: Rule, pairs: &Pairs<Rule>) -> bool {
_ => 0,
}
});
return count == -1;
count == -1
}

/// Checks if a block terminates in an expression
fn terminal_expr(pairs: &Pairs<Rule>) -> bool {
let e = pairs.clone().find(|p| matches!(p.as_rule(), Rule::expr));
!e.is_none()
e.is_some()
}

fn debug_print(pair: Pair<Rule>, 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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -1328,7 +1317,7 @@ fn find_inline_comment_lines(s: &str) -> HashSet<usize> {
comment_lines.insert(line_num + 1);
}
}
return comment_lines;
comment_lines
}

fn is_inline_comment(s: &str) -> bool {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -1567,15 +1556,15 @@ pub fn parse_and_format(s: &str) -> miette::Result<String> {
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 {
Expand Down Expand Up @@ -1616,7 +1605,7 @@ pub fn run(s: &str, opts: RunOptions) -> miette::Result<String> {

let file_name = opts.file_name.clone().unwrap_or("<input>".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(),
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
Expand Down Expand Up @@ -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"));
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn run_rustfmt(s: &str) -> Option<String> {

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",
Expand Down
2 changes: 1 addition & 1 deletion tests/rustfmt-matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
);
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshot-examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down

0 comments on commit 6712d10

Please sign in to comment.