Skip to content

Commit

Permalink
Setup cargo clippy (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer authored Dec 21, 2024
1 parent ed92efc commit b3ad7c2
Show file tree
Hide file tree
Showing 43 changed files with 228 additions and 212 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: '${{ env.RUST_TOOLCHAIN }}'
components: 'clippy'

# Compile, but don't run.
- run: cargo test --no-run
Expand All @@ -45,6 +46,8 @@ jobs:
# Deny warnings.
RUSTDOCFLAGS="-D warnings" cargo doc
- run: cargo clippy -- -Dwarnings

typos:
runs-on: ubuntu-22.04
timeout-minutes: 10
Expand Down
8 changes: 4 additions & 4 deletions arnoldc/src/arnold.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use anyhow::Result;
use std::fmt::Formatter;
use std::sync::Arc;
use xrcf::frontend::Parse;
use xrcf::frontend::Parser;
use xrcf::frontend::ParserDispatch;
use xrcf::frontend::TokenKind;
use xrcf::ir::APInt;
use xrcf::ir::Attribute;
use xrcf::ir::Block;
Expand All @@ -11,10 +15,6 @@ use xrcf::ir::OpOperand;
use xrcf::ir::Operation;
use xrcf::ir::OperationName;
use xrcf::ir::Region;
use xrcf::parser::Parse;
use xrcf::parser::Parser;
use xrcf::parser::ParserDispatch;
use xrcf::parser::TokenKind;
use xrcf::shared::Shared;
use xrcf::shared::SharedExt;

Expand Down
3 changes: 1 addition & 2 deletions arnoldc/src/arnold_to_mlir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ impl ModuleLowering {
let operand = Shared::new(operand.into());
ret.set_operand(0, operand);
let ret = func::ReturnOp::from_operation(ret);
let ret = Shared::new(ret.into());
ret
Shared::new(ret.into())
}
fn return_zero(func: Shared<dyn Op>) {
let typ = IntegerType::new(32);
Expand Down
7 changes: 4 additions & 3 deletions arnoldc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::arc_with_non_send_sync)]
mod arnold;
mod arnold_to_mlir;
mod transform;
Expand Down Expand Up @@ -36,8 +37,8 @@ struct ArnoldcArgs {

fn cli() -> Command {
let cli = Command::new("arnoldc").args(xrcf::default_arguments());
let cli = ArnoldcArgs::augment_args(cli);
cli

ArnoldcArgs::augment_args(cli)
}

fn compile_passes() -> Vec<&'static str> {
Expand Down Expand Up @@ -166,7 +167,7 @@ mod tests {
];
tracing::info!("\nBefore {args:?}:\n{src}");
let out: Shared<Vec<u8>> = Shared::new(Vec::new().into());
let result = run_app(Some(out.clone()), args.clone(), &src);
let result = run_app(Some(out.clone()), args.clone(), src);
assert!(result.is_ok());
let actual = match result.unwrap() {
RewriteResult::Changed(op) => {
Expand Down
26 changes: 10 additions & 16 deletions arnoldc/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::arnold_to_mlir::ConvertArnoldToMLIR;
use anyhow::Result;
use xrcf::convert::Pass;
use xrcf::convert::RewriteResult;
use xrcf::frontend::default_dispatch;
use xrcf::frontend::default_parse_type;
use xrcf::frontend::Parse;
use xrcf::frontend::Parser;
use xrcf::frontend::ParserDispatch;
use xrcf::frontend::TokenKind;
use xrcf::ir::Block;
use xrcf::ir::Op;
use xrcf::ir::Type;
use xrcf::parser::default_dispatch;
use xrcf::parser::default_parse_type;
use xrcf::parser::Parse;
use xrcf::parser::Parser;
use xrcf::parser::ParserDispatch;
use xrcf::parser::TokenKind;
use xrcf::shared::Shared;
use xrcf::transform;
use xrcf::DefaultTransformDispatch;
Expand All @@ -25,11 +25,7 @@ fn is_function_call<T: ParserDispatch>(parser: &Parser<T>) -> bool {
&& parser.peek_n(1).unwrap().kind == TokenKind::LParen;
let known_keyword = {
if let TokenKind::BareIdentifier = parser.peek().kind {
match parser.peek().lexeme.as_str() {
"def" => true,
"print" => true,
_ => false,
}
matches!(parser.peek().lexeme.as_str(), "def" | "print")
} else {
false
}
Expand Down Expand Up @@ -75,9 +71,7 @@ impl ParserDispatch for ArnoldParserDispatch {
// Ignore nothing (e.g., `<op name> x, y`).
parser.peek().clone()
};
match name.lexeme.clone().as_str() {
_ => default_dispatch(name, parser, parent),
}
default_dispatch(name, parser, parent)
}
fn parse_type(parser: &mut Parser<Self>) -> Result<Shared<dyn Type>> {
default_parse_type(parser)
Expand All @@ -103,6 +97,7 @@ impl TransformDispatch for ArnoldTransformDispatch {
fn preprocess(src: &str) -> String {
let mut result = String::new();
for line in src.lines() {
#[allow(clippy::if_same_then_else)]
if line.contains("IT'S SHOWTIME") {
result.push_str(&format!("{} {{", line));
} else if line.contains("BECAUSE I'M GOING TO SAY PLEASE") {
Expand All @@ -114,7 +109,7 @@ fn preprocess(src: &str) -> String {
} else if line.contains("BULLSHIT") {
result.push_str(&line.replace("BULLSHIT", "} BULLSHIT {"));
} else {
result.push_str(&line);
result.push_str(line);
}
result.push('\n');
}
Expand All @@ -134,7 +129,6 @@ mod tests {
use crate::compile_passes;
use indoc::indoc;
use std::panic::Location;
use tracing;
use xrcf::shared::SharedExt;
use xrcf::tester::Tester;
use xrcf::Passes;
Expand Down
7 changes: 3 additions & 4 deletions xrcf-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use clap::Args;
use clap::Command;
use std::io::Read;
use xrcf::convert::RewriteResult;
use xrcf::frontend::DefaultParserDispatch;
use xrcf::frontend::Parser;
use xrcf::init_subscriber;
use xrcf::parser::DefaultParserDispatch;
use xrcf::parser::Parser;
use xrcf::shared::SharedExt;
use xrcf::transform;
use xrcf::DefaultTransformDispatch;
Expand All @@ -26,8 +26,7 @@ struct XRCFArgs {

fn cli() -> Command {
let cli = Command::new("xrcf").args(xrcf::default_arguments());
let cli = XRCFArgs::augment_args(cli);
cli
XRCFArgs::augment_args(cli)
}

fn remove_comments(src: &str) -> String {
Expand Down
9 changes: 5 additions & 4 deletions xrcf/src/convert/experimental_to_mlir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
use dialect::experimental::PrintfOp;
use std::str::FromStr;
use std::sync::Arc;

struct PrintLowering;
Expand Down Expand Up @@ -50,7 +51,7 @@ impl PrintLowering {
fn len_specifier(parent: &Shared<Block>, len: usize) -> Shared<dyn Op> {
let mut operation = Operation::default();
operation.set_parent(Some(parent.clone()));
let typ = IntegerType::from_str("i16");
let typ = IntegerType::from_str("i16").unwrap();
let name = parent.rd().unique_value_name("%");
let result_type = Shared::new(typ.into());
let result = operation.add_new_op_result(&name, result_type);
Expand Down Expand Up @@ -117,7 +118,7 @@ impl PrintLowering {
let var = var.expect("expected vararg");
operation.set_operand(1, var);
}
let typ = IntegerType::from_str("i32");
let typ = IntegerType::from_str("i32").unwrap();
let name = parent.rd().unique_value_name("%");
let result_type = Shared::new(typ.into());
let result = operation.add_new_op_result(&name, result_type);
Expand All @@ -128,7 +129,7 @@ impl PrintLowering {
op.set_identifier("@printf".to_string());
if set_varargs {
let varargs = "!llvm.func<i32 (!llvm.ptr, ...)>";
let varargs = llvm::FunctionType::from_str(varargs);
let varargs = llvm::FunctionType::from_str(varargs).unwrap();
let varargs = Shared::new(varargs.into());
op.set_varargs(Some(varargs));
}
Expand Down Expand Up @@ -171,7 +172,7 @@ impl PrintLowering {
fn printf_func_def(parent: Shared<Block>, set_varargs: bool) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(Some(parent.clone()));
let result_type = crate::ir::IntegerType::from_str("i32");
let result_type = IntegerType::from_str("i32").unwrap();
let result_type = Shared::new(result_type.into());
operation.set_anonymous_result(result_type)?;

Expand Down
15 changes: 8 additions & 7 deletions xrcf/src/convert/mlir_to_llvmir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::shared::Shared;
use crate::shared::SharedExt;
use crate::targ3t;
use anyhow::Result;
use std::str::FromStr;

struct AddLowering;

Expand Down Expand Up @@ -265,7 +266,7 @@ fn lower_block_argument_types(operation: &mut Operation) {
let typ = argument.rd().typ().unwrap();
let typ = typ.rd();
if typ.as_any().is::<dialect::llvm::PointerType>() {
let typ = targ3t::llvmir::PointerType::from_str("ptr");
let typ = targ3t::llvmir::PointerType::from_str("ptr").unwrap();
let typ = Shared::new(typ.into());
let name = BlockArgumentName::Unset;
let name = Shared::new(name.into());
Expand Down Expand Up @@ -366,16 +367,16 @@ fn determine_argument_pairs(block: &Shared<Block>) -> Vec<(Shared<OpOperand>, Sh

/// Replace the operands of the argument pairs by constants if possible.
fn replace_constant_argument_pairs(pairs: &mut Vec<(Shared<OpOperand>, Shared<Block>)>) {
for i in 0..pairs.len() {
let (op_operand, block) = pairs[i].clone();
for pair in pairs {
let (op_operand, block) = pair.clone();
let new = constant_op_operand(op_operand);
if let Some(new) = new {
pairs[i] = (new, block);
*pair = (new, block);
}
}
}

fn verify_argument_pairs(pairs: &Vec<(Shared<OpOperand>, Shared<Block>)>) {
fn verify_argument_pairs(pairs: &[(Shared<OpOperand>, Shared<Block>)]) {
if pairs.len() != 2 {
panic!("Expected two callers");
}
Expand Down Expand Up @@ -460,7 +461,7 @@ fn insert_phi(block: Shared<Block>) {
replace_constant_argument_pairs(&mut argument_pairs);
verify_argument_pairs(&argument_pairs);
phi.set_argument_pairs(Some(argument_pairs));
let argument = arguments.get(0).unwrap();
let argument = arguments.first().unwrap();
let phi = Shared::new(phi.into());
set_phi_result(phi.clone(), argument);
arguments.clear();
Expand Down Expand Up @@ -634,7 +635,7 @@ impl TypeConvert for ConvertMLIRToLLVMIR {
let converted = arguments
.vec()
.iter()
.map(|argument| Self::convert_type(argument))
.map(Self::convert_type)
.collect::<Result<Vec<_>>>()?;
let arguments = Types::from_vec(converted);
let typ = targ3t::llvmir::FunctionType::new(typ.return_types().clone(), arguments);
Expand Down
6 changes: 3 additions & 3 deletions xrcf/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub enum RewriteResult {
impl RewriteResult {
pub fn is_changed(&self) -> Option<&ChangedOp> {
match self {
RewriteResult::Changed(op) => Some(&op),
RewriteResult::Changed(op) => Some(op),
RewriteResult::Unchanged => None,
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ fn apply_rewrites_helper(
for nested_op in ops.iter() {
let indent = indent + 1;
let result = apply_rewrites_helper(nested_op.clone(), rewrites, indent)?;
if let Some(_) = result.is_changed() {
if result.is_changed().is_some() {
let root_passthrough = ChangedOp::new(root.clone());
let root_passthrough = RewriteResult::Changed(root_passthrough);
return Ok(root_passthrough);
Expand All @@ -110,7 +110,7 @@ fn apply_rewrites_helper(
if rewrite.is_match(&*root_read)? {
debug!("{}--> Success", spaces(indent));
let root_rewrite = rewrite.rewrite(root.clone())?;
if let Some(_) = root_rewrite.is_changed() {
if root_rewrite.is_changed().is_some() {
debug!("{}----> Changed", spaces(indent));
return Ok(root_rewrite);
}
Expand Down
7 changes: 3 additions & 4 deletions xrcf/src/convert/scf_to_cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ fn branch_op(after: Shared<Block>) -> Shared<dyn Op> {
let mut new_op = dialect::cf::BranchOp::from_operation(operation);
let operand = Shared::new(OpOperand::from_block(after).into());
new_op.set_dest(operand);
let new_op = Shared::new(new_op.into());
new_op
Shared::new(new_op.into())
}

/// Add a `cf.br` to the end of `block` with destination `after`.
Expand All @@ -81,7 +80,7 @@ fn add_branch_to_after(block: Shared<Block>, after: Shared<Block>) {
let last_op = last_op.rd();
let yield_op = last_op.as_any().downcast_ref::<dialect::scf::YieldOp>();
if let Some(yield_op) = yield_op {
let new_op = lower_yield_op(&yield_op, after.clone()).unwrap();
let new_op = lower_yield_op(yield_op, after.clone()).unwrap();
ops.pop();
ops.push(new_op.clone());
} else {
Expand Down Expand Up @@ -282,7 +281,7 @@ impl Rewrite for IfLowering {
let parent_region = parent.rd().parent().expect("Expected parent region");
let op = op.as_any().downcast_ref::<dialect::scf::IfOp>().unwrap();

let (then, els) = add_blocks(&op, parent_region.clone())?;
let (then, els) = add_blocks(op, parent_region.clone())?;

let mut operation = Operation::default();
operation.set_parent(Some(parent.clone()));
Expand Down
8 changes: 4 additions & 4 deletions xrcf/src/dialect/arith/op.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::convert::ChangedOp;
use crate::convert::RewriteResult;
use crate::frontend::Parse;
use crate::frontend::Parser;
use crate::frontend::ParserDispatch;
use crate::frontend::TokenKind;
use crate::ir::AnyType;
use crate::ir::Attribute;
use crate::ir::Block;
Expand All @@ -11,10 +15,6 @@ use crate::ir::Operation;
use crate::ir::OperationName;
use crate::ir::Value;
use crate::ir::Values;
use crate::parser::Parse;
use crate::parser::Parser;
use crate::parser::ParserDispatch;
use crate::parser::TokenKind;
use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
Expand Down
8 changes: 4 additions & 4 deletions xrcf/src/dialect/cf/op.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::frontend::Parse;
use crate::frontend::Parser;
use crate::frontend::ParserDispatch;
use crate::frontend::TokenKind;
use crate::ir::Block;
use crate::ir::Op;
use crate::ir::OpOperand;
use crate::ir::Operation;
use crate::ir::OperationName;
use crate::parser::Parse;
use crate::parser::Parser;
use crate::parser::ParserDispatch;
use crate::parser::TokenKind;
use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
Expand Down
8 changes: 4 additions & 4 deletions xrcf/src/dialect/experimental/op.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::frontend::Parse;
use crate::frontend::Parser;
use crate::frontend::ParserDispatch;
use crate::frontend::TokenKind;
use crate::ir::Block;
use crate::ir::Constant;
use crate::ir::Op;
Expand All @@ -6,10 +10,6 @@ use crate::ir::Operation;
use crate::ir::OperationName;
use crate::ir::StringAttr;
use crate::ir::Value;
use crate::parser::Parse;
use crate::parser::Parser;
use crate::parser::ParserDispatch;
use crate::parser::TokenKind;
use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
Expand Down
Loading

0 comments on commit b3ad7c2

Please sign in to comment.