clippy lints 1

This commit is contained in:
collin 2020-11-10 16:23:55 -08:00
parent 2bb17b496b
commit 33fae17b35
18 changed files with 94 additions and 99 deletions

View File

@ -81,9 +81,7 @@ impl ArrayDimensions {
///
pub fn remove_first(&mut self) -> Option<PositiveNumber> {
// If there are no dimensions in the array, then return None.
if self.0.first().is_none() {
return None;
}
self.0.first()?;
// Remove the first dimension.
let removed = self.0.remove(0);

View File

@ -91,7 +91,7 @@ impl Type {
let right_new_type = inner_array_type(*right_type.to_owned(), right_dim_owned);
// Call eq_flat() on the new left and right types.
return left_new_type.eq_flat(&right_new_type);
left_new_type.eq_flat(&right_new_type)
}
(Type::Tuple(left), Type::Tuple(right)) => left
.iter()

View File

@ -70,7 +70,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
output_directory,
program: Program::new(package_name),
program_input: Input::new(),
imported_programs: ImportParser::new(),
imported_programs: ImportParser::default(),
_engine: PhantomData,
_group: PhantomData,
}
@ -214,7 +214,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
})?;
// Run type inference check on program.
TypeInference::new(&self.program, symbol_table).map_err(|mut e| {
TypeInference::run(&self.program, symbol_table).map_err(|mut e| {
e.set_path(&self.main_file_path);
e
@ -256,7 +256,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
let symbol_table = SymbolTable::new(&self.program, &self.imported_programs, &self.program_input)?;
// Run type inference check on program.
TypeInference::new(&self.program, symbol_table)?;
TypeInference::run(&self.program, symbol_table)?;
tracing::debug!("Program parsing complete\n{:#?}", self.program);

View File

@ -48,9 +48,7 @@ pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: Constrai
resolved_program.store_definitions(program, imported_programs)?;
let main = resolved_program
.get(&main_function_name)
.ok_or_else(|| CompilerError::NoMain)?;
let main = resolved_program.get(&main_function_name).ok_or(CompilerError::NoMain)?;
match main.clone() {
ConstrainedValue::Function(_circuit_identifier, function) => {

View File

@ -107,7 +107,10 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
Ok(ConstrainedValue::Array(result))
}
/// Enforce array expressions
///
/// Returns an array value from an array initializer expression.
///
#[allow(clippy::too_many_arguments)]
pub fn enforce_array_initializer<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
@ -142,65 +145,63 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}
Ok(value)
} else if expected_dimensions.first().eq(&actual_dimensions.first()) {
// Case 2 - enforce expression with updated array type.
let dimension = match expected_dimensions.remove_first() {
Some(number) => {
// Parse the array dimension into a `usize`.
parse_index(&number, &span)?
}
None => return Err(ExpressionError::unexpected_array(type_.to_string(), span)),
};
// Update the actual array dimensions.
let _first_dimension = actual_dimensions.remove_first();
// Update the expected type to a new array type with the first dimension removed.
let expected_expression_type = Some(inner_array_type(*type_, expected_dimensions));
// If the expression has more dimensions.
let element_value = match actual_dimensions.first() {
Some(_dimension) => {
// Get the value of the array element as an initializer.
self.enforce_array_initializer(
cs,
file_scope,
function_scope,
expected_expression_type,
element_expression,
actual_dimensions.clone(),
span,
)?
}
None => {
// Get the value of the array element as an expression.
self.enforce_expression(
cs,
file_scope,
function_scope,
expected_expression_type,
element_expression,
)?
}
};
// Allocate the array of values.
let array_values = vec![element_value; dimension];
// Create a new value with the expected dimension.
Ok(ConstrainedValue::Array(array_values))
} else {
if expected_dimensions.first().eq(&actual_dimensions.first()) {
// Case 2 - enforce expression with updated array type.
let dimension = match expected_dimensions.remove_first() {
Some(number) => {
// Parse the array dimension into a `usize`.
parse_index(&number, &span)?
}
None => return Err(ExpressionError::unexpected_array(type_.to_string(), span)),
};
// Update the actual array dimensions.
let _first_dimension = actual_dimensions.remove_first();
// Update the expected type to a new array type with the first dimension removed.
let expected_expression_type = Some(inner_array_type(*type_, expected_dimensions));
// If the expression has more dimensions.
let element_value = match actual_dimensions.first() {
Some(_dimension) => {
// Get the value of the array element as an initializer.
self.enforce_array_initializer(
cs,
file_scope,
function_scope,
expected_expression_type,
element_expression,
actual_dimensions.clone(),
span.clone(),
)?
}
None => {
// Get the value of the array element as an expression.
self.enforce_expression(
cs,
file_scope,
function_scope,
expected_expression_type,
element_expression,
)?
}
};
// Allocate the array of values.
let array_values = vec![element_value; dimension];
// Create a new value with the expected dimension.
Ok(ConstrainedValue::Array(array_values))
} else {
// Case 3 - return mismatched dimensions error.
Err(ExpressionError::invalid_first_dimension(
expected_dimensions
.first()
.ok_or(ExpressionError::undefined_first_dimension(span.clone()))?,
actual_dimensions
.first()
.ok_or(ExpressionError::undefined_first_dimension(span.clone()))?,
))
}
// Case 3 - return mismatched dimensions error.
Err(ExpressionError::invalid_first_dimension(
expected_dimensions
.first()
.ok_or_else(|| ExpressionError::undefined_first_dimension(span.clone()))?,
actual_dimensions
.first()
.ok_or_else(|| ExpressionError::undefined_first_dimension(span))?,
))
}
} else {
// No explicit type given - evaluate array element expression.

View File

@ -70,7 +70,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
*old_value = selected_value;
return Ok(());
Ok(())
} else {
match assignee.accesses[0].clone() {
AssigneeAccess::Array(range_or_expression) => self.assign_array(

View File

@ -28,11 +28,13 @@ pub type StatementResult<T> = Result<T, StatementError>;
pub type IndicatorAndConstrainedValue<T, U> = (Option<Boolean>, ConstrainedValue<T, U>);
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
///
/// Enforce a program statement.
/// Returns a Vector of (indicator, value) tuples.
/// Each evaluated statement may execute of one or more statements that may return early.
/// To indicate which of these return values to take we conditionally select the value according
/// to the `indicator` bit that evaluates to true.
///
#[allow(clippy::too_many_arguments)]
pub fn enforce_statement<CS: ConstraintSystem<F>>(
&mut self,

View File

@ -58,7 +58,7 @@ impl ImportParserError {
/// Failed to convert a file path into an os string.
///
pub fn convert_os_string(span: Span) -> Self {
let message = format!("Failed to convert file string name, maybe an illegal character?");
let message = "Failed to convert file string name, maybe an illegal character?".to_string();
Self::new_from_span(message, span)
}

View File

@ -26,23 +26,13 @@ use std::{
///
/// A program can import one or more packages. A package can be found locally in the source
/// directory, foreign in the imports directory, or part of the core package list.
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct ImportParser {
imports: HashMap<String, Program>,
core_packages: HashSet<Package>,
}
impl ImportParser {
///
/// Creates a new empty `ImportParser`.
///
pub fn new() -> Self {
Self {
imports: HashMap::new(),
core_packages: HashSet::new(),
}
}
///
/// Inserts a (file name -> program) pair into the `ImportParser`.
///
@ -96,10 +86,10 @@ impl ImportParser {
/// 3. Insert the typed syntax tree into the `ImportParser`
///
pub fn parse(program: &Program) -> Result<Self, ImportParserError> {
let mut imports = Self::new();
let mut imports = Self::default();
// Find all imports relative to current directory.
let path = current_dir().map_err(|error| ImportParserError::current_directory_error(error))?;
let path = current_dir().map_err(ImportParserError::current_directory_error)?;
// Parse each import statement.
for import in &program.imports {

View File

@ -46,7 +46,7 @@ impl TypeError {
/// The `Self` keyword was used outside of a circuit.
///
pub fn self_not_available(span: Span) -> Self {
let message = format!("Type `Self` is only available in circuit definitions and circuit functions.");
let message = "Type `Self` is only available in circuit definitions and circuit functions.".to_string();
Self::new_from_span(message, span)
}

View File

@ -367,7 +367,7 @@ impl SymbolTable {
self.check_function_names(&program.functions)
} else {
// Check for a symbol alias.
let identifier = symbol.alias.to_owned().unwrap_or(symbol.symbol.to_owned());
let identifier = symbol.alias.to_owned().unwrap_or_else(|| symbol.symbol.to_owned());
// Check if the imported symbol is a circuit
match program.circuits.get(&symbol.symbol) {

View File

@ -92,7 +92,7 @@ impl FunctionType {
// Type check function output.
let output = FunctionOutputType::new_from_circuit(
table,
circuit_name.clone(),
circuit_name,
unresolved_function.output,
unresolved_function.span,
)?;

View File

@ -76,7 +76,7 @@ impl Type {
// Lookup the circuit type in the symbol table
let circuit_type = table
.get_circuit_type(&identifier.name)
.ok_or(TypeError::undefined_circuit(identifier))?;
.ok_or_else(|| TypeError::undefined_circuit(identifier))?;
Type::Circuit(circuit_type.identifier.clone())
}

View File

@ -60,7 +60,7 @@ impl TestSymbolTable {
let program = self.typed.into_repr();
// Create empty import parser.
let import_parser = ImportParser::new();
let import_parser = ImportParser::default();
// Create empty input.
let input = Input::new();
@ -82,7 +82,7 @@ impl TestSymbolTable {
let static_check = &mut SymbolTable::default();
// Create empty import parser.
let import_parser = ImportParser::new();
let import_parser = ImportParser::default();
// Run pass one and expect an error.
let error = static_check.check_names(&program, &import_parser).unwrap_err();
@ -106,7 +106,7 @@ impl TestSymbolTable {
let static_check = &mut SymbolTable::default();
// Create empty import parser.
let import_parser = ImportParser::new();
let import_parser = ImportParser::default();
// Run the pass one and expect no errors.
static_check.check_names(&program, &import_parser).unwrap();

View File

@ -85,8 +85,14 @@ impl TypeVariablePairs {
///
pub fn push_pairs(&mut self, left: Type, right: Type, span: &Span) -> Result<(), TypeAssertionError> {
match (left, right) {
(Type::TypeVariable(variable), type_) => Ok(self.push(variable, type_)),
(type_, Type::TypeVariable(variable)) => Ok(self.push(variable, type_)),
(Type::TypeVariable(variable), type_) => {
self.push(variable, type_);
Ok(())
}
(type_, Type::TypeVariable(variable)) => {
self.push(variable, type_);
Ok(())
}
(Type::Array(left_type), Type::Array(right_type)) => self.push_pairs_array(*left_type, *right_type, span),
(Type::Tuple(left_types), Type::Tuple(right_types)) => {
self.push_pairs_tuple(left_types.into_iter(), right_types.into_iter(), span)

View File

@ -302,7 +302,7 @@ impl Frame {
};
// Assert that the expected type is equal to the actual type.
self.assert_equal(expected_type.clone(), actual_type.clone(), span)
self.assert_equal(expected_type, actual_type.clone(), span)
}
// Check for multiple defined variables.
@ -1128,7 +1128,7 @@ impl Frame {
}
// Return the function output type.
Ok(function_type.output.type_.clone())
Ok(function_type.output.type_)
}
///

View File

@ -34,7 +34,7 @@ impl TypeInference {
///
/// Evaluates all `TypeAssertion` predicates.
///
pub fn new(program: &Program, symbol_table: SymbolTable) -> Result<(), TypeInferenceError> {
pub fn run(program: &Program, symbol_table: SymbolTable) -> Result<(), TypeInferenceError> {
let mut type_inference = Self {
table: symbol_table,
frames: Vec::new(),

View File

@ -53,7 +53,7 @@ impl TestTypeInference {
let program = typed.into_repr();
// Create empty import parser.
let import_parser = ImportParser::new();
let import_parser = ImportParser::default();
// Create empty input.
let input = Input::new();
@ -66,11 +66,11 @@ impl TestTypeInference {
}
pub fn check(self) {
TypeInference::new(&self.program, self.symbol_table).unwrap();
TypeInference::run(&self.program, self.symbol_table).unwrap();
}
pub fn expect_error(self) {
assert!(TypeInference::new(&self.program, self.symbol_table).is_err());
assert!(TypeInference::run(&self.program, self.symbol_table).is_err());
}
}