Skip to content

Commit

Permalink
Lint with Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
gskorokhod committed Jan 26, 2024
1 parent 962b00e commit 4c5d575
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
43 changes: 21 additions & 22 deletions conjure_oxide/src/solvers/kissat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl CNF {
expr_clauses.push(self.clause_to_expression(clause)?);
}

return Ok(ConjureExpression::And(expr_clauses));
Ok(ConjureExpression::And(expr_clauses))
}

fn clause_to_expression(&self, clause: &Vec<i32>) -> Result<ConjureExpression, CNFError> {
Expand All @@ -120,29 +120,29 @@ impl CNF {
}
}

return Ok(ConjureExpression::Or(ans));
Ok(ConjureExpression::Or(ans))
}

fn get_reference_index(&self, name: &ConjureName) -> Result<i32, CNFError> {
return match self.get_index(name) {
match self.get_index(name) {
None => {
Err(CNFError::VariableNameNotFound(name.clone()))
}
Some(ind) => Ok(ind),
};
}
}

fn handle_reference(&self, name: &ConjureName) -> Result<Vec<i32>, CNFError> {
return Ok(vec![self.get_reference_index(name)?]);
Ok(vec![self.get_reference_index(name)?])
}

fn handle_not(&self, expr_box: &Box<ConjureExpression>) -> Result<Vec<i32>, CNFError> {
let expr = expr_box.as_ref();
return match expr {
match expr {
// Expression inside the Not()
ConjureExpression::Reference(name) => Ok(vec![-self.get_reference_index(name)?]),
_ => Err(CNFError::UnexpectedExpressionInsideNot(expr.clone())),
};
}
}

fn handle_or(&self, expressions: &Vec<ConjureExpression>) -> Result<Vec<i32>, CNFError> {
Expand All @@ -155,25 +155,25 @@ impl CNF {
}
}

return Ok(ans);
Ok(ans)
}

/// Convert a single Reference, Not or Or into a row of the CNF format
fn handle_flat_expression(&self, expression: &ConjureExpression) -> Result<Vec<i32>, CNFError> {
return match expression {
match expression {
ConjureExpression::Reference(name) => self.handle_reference(name),
ConjureExpression::Not(var_box) => self.handle_not(var_box),
ConjureExpression::Or(expressions) => self.handle_or(expressions),
_ => Err(CNFError::UnexpectedExpression(expression.clone())),
};
}
}

fn handle_and(&self, expressions: &Vec<ConjureExpression>) -> Result<Vec<Vec<i32>>, CNFError> {
let mut ans: Vec<Vec<i32>> = Vec::new();

for expression in expressions {
match expression {
ConjureExpression::And(expressions) => {
ConjureExpression::And(_expressions) => {
return Err(CNFError::NestedAnd(expression.clone()));
}
_ => {
Expand All @@ -182,14 +182,14 @@ impl CNF {
}
}

return Ok(ans);
Ok(ans)
}

fn handle_expression(&self, expression: &ConjureExpression) -> Result<Vec<Vec<i32>>, CNFError> {
return match expression {
match expression {
ConjureExpression::And(expressions) => self.handle_and(expressions),
_ => Ok(vec![self.handle_flat_expression(expression)?]),
};
}
}
}

Expand All @@ -205,7 +205,7 @@ impl HasVariable for i32 {

impl HasVariable for &ConjureName {
fn has_variable(self, cnf: &CNF) -> bool {
return cnf.get_index(self).is_some();
cnf.get_index(self).is_some()
}
}

Expand All @@ -227,7 +227,7 @@ impl FromConjureModel for CNF {
}

for expr in conjure_model.constraints {
match (ans.add_expression(&expr)) {
match ans.add_expression(&expr) {
Ok(_) => {}
Err(error) => {
let message = format!("Error converting to CNF: {:?}", error);
Expand All @@ -238,18 +238,17 @@ impl FromConjureModel for CNF {
}
}

return Ok(ans);
Ok(ans)
}
}

#[cfg(test)]
mod tests {
use std::any::Any;
use crate::ast::Domain::{BoolDomain, IntDomain};
use crate::ast::Expression::{And, Not, Or, Reference};
use crate::ast::{DecisionVariable, Model};
use crate::ast::{Expression, Name};
use crate::solvers::kissat::{CNF, CNFError};
use crate::solvers::kissat::{CNF};
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
Expand All @@ -260,7 +259,7 @@ mod tests {
for el in a {
a_set.insert(el.clone());
}
return a_set;
a_set
}

fn assert_eq_any_order<T: Eq + Hash + Debug + Clone>(a: &Vec<Vec<T>>, b: &Vec<Vec<T>>) {
Expand All @@ -286,7 +285,7 @@ mod tests {

fn if_ok<T, E: Debug>(result: Result<T, E>) -> T {
assert!(result.is_ok());
return result.unwrap();
result.unwrap()
}

#[test]
Expand All @@ -297,7 +296,7 @@ mod tests {

let x: Name = Name::UserName(String::from('x'));
model.add_variable(x.clone(), DecisionVariable { domain: BoolDomain });
model.add_constraint(Expression::Reference(x.clone()));
model.add_constraint(Reference(x.clone()));

let res: Result<CNF, SolverError> = CNF::from_conjure(model);
assert!(res.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion conjure_oxide/tests/rewrite_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use conjure_oxide::ast::*;
#[test]
fn rules_present() {
let rules = conjure_rules::get_rules();
assert!(rules.len() > 0);
assert!(!rules.is_empty());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/conjure_core/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter, write};
use std::fmt::{Debug, Display, Formatter};

#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -126,7 +126,7 @@ fn display_expressions(expressions: &Vec<Expression>) -> String {
if expressions.len() <= 3 {
format!("Sum({})", expressions.iter().map(|e| e.to_string()).collect::<Vec<String>>().join(", "))
} else {
format!("Sum({}..{})", expressions[0].to_string(), expressions[expressions.len() - 1].to_string())
format!("Sum({}..{})", expressions[0], expressions[expressions.len() - 1])
}
}

Expand Down

0 comments on commit 4c5d575

Please sign in to comment.