Skip to content

Commit

Permalink
Take care of most of clippy's warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sonmarcho committed Nov 29, 2023
1 parent a7e2f31 commit fb8fbd1
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 170 deletions.
13 changes: 3 additions & 10 deletions charon/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub fn get_args_crate_index<T: Deref<Target = str>>(args: &[T]) -> Option<usize>
/// Translate a crate to LLBC (Low-Level Borrow Calculus).
///
/// This function is a callback function for the Rust compiler.
#[allow(clippy::result_unit_err)]
pub fn translate(sess: &Session, tcx: TyCtxt, internal: &mut CharonCallbacks) -> Result<(), ()> {
trace!();
let options = &internal.options;
Expand Down Expand Up @@ -189,15 +190,11 @@ pub fn translate(sess: &Session, tcx: TyCtxt, internal: &mut CharonCallbacks) ->
if options.ullbc {
// # Extract the files
export::export_ullbc(
ctx.error_count > 0,
&ctx,
crate_name,
&ctx.id_to_file,
&ordered_decls,
&ctx.type_defs,
&ctx.fun_defs,
&ctx.global_defs,
&ctx.trait_decls,
&ctx.trait_impls,
&options.dest_dir,
)?;
} else {
Expand Down Expand Up @@ -288,15 +285,11 @@ pub fn translate(sess: &Session, tcx: TyCtxt, internal: &mut CharonCallbacks) ->

// # Final step: generate the files.
export::export_llbc(
ctx.error_count > 0,
&ctx,
crate_name,
&ctx.id_to_file,
&ordered_decls,
&ctx.type_defs,
&llbc_funs,
&llbc_globals,
&ctx.trait_decls,
&ctx.trait_impls,
&options.dest_dir,
)?;
}
Expand Down
46 changes: 15 additions & 31 deletions charon/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::llbc_ast;
use crate::meta::{FileId, FileName};
use crate::reorder_decls::{DeclarationGroup, DeclarationsGroups};
use crate::translate_ctx::*;
use crate::types::*;
use crate::ullbc_ast;
use crate::ullbc_ast::{FunDeclId, GlobalDeclId, TraitDecl, TraitDecls, TraitImpl, TraitImpls};
use crate::ullbc_ast::{FunDeclId, GlobalDeclId, TraitDecl, TraitImpl};
use serde::Serialize;
use std::collections::HashMap;
use std::fs::File;
use std::path::PathBuf;

Expand All @@ -29,16 +29,13 @@ struct GCrateSerializer<'a, FD, GD> {
/// Export the translated definitions to a JSON file.
///
/// This is a generic function, used both for LLBC and ULLBC.
#[allow(clippy::result_unit_err)]
pub fn gexport<FD: Serialize + Clone, GD: Serialize + Clone>(
errors: bool,
ctx: &TransCtx,
crate_name: String,
id_to_file: &HashMap<FileId::Id, FileName>,
ordered_decls: &DeclarationsGroups,
type_defs: &TypeDecls,
fun_defs: &FunDeclId::Map<FD>,
global_defs: &GlobalDeclId::Map<GD>,
trait_decls: &TraitDeclId::Map<TraitDecl>,
trait_impls: &TraitImplId::Map<TraitImpl>,
dest_dir: &Option<PathBuf>,
extension: &str,
) -> Result<(), ()> {
Expand All @@ -52,6 +49,7 @@ pub fn gexport<FD: Serialize + Clone, GD: Serialize + Clone>(

// Transform the map file id -> file into a vector.
// Sort the vector to make the serialized file as stable as possible.
let id_to_file = &ctx.id_to_file;
let mut file_ids: Vec<FileId::Id> = id_to_file.keys().copied().collect();
file_ids.sort();
let id_to_file: Vec<(FileId::Id, FileName)> = file_ids
Expand All @@ -63,11 +61,11 @@ pub fn gexport<FD: Serialize + Clone, GD: Serialize + Clone>(
// Serialize
// Note that we replace the maps with vectors (the declarations contain
// their ids, so it is easy to reconstruct the maps from there).
let types = type_defs.iter().cloned().collect();
let types = ctx.type_defs.iter().cloned().collect();
let functions = fun_defs.iter().cloned().collect();
let globals = global_defs.iter().cloned().collect();
let trait_decls = trait_decls.iter().cloned().collect();
let trait_impls = trait_impls.iter().cloned().collect();
let trait_decls = ctx.trait_decls.iter().cloned().collect();
let trait_impls = ctx.trait_impls.iter().cloned().collect();
let crate_serializer = GCrateSerializer {
name: crate_name,
id_to_file,
Expand Down Expand Up @@ -100,7 +98,7 @@ pub fn gexport<FD: Serialize + Clone, GD: Serialize + Clone>(
// We canonicalize (i.e., make absolute) the path before printing it:
// this makes it clearer to the user where to find the file.
let path = std::fs::canonicalize(target_filename).unwrap();
if errors {
if ctx.error_count > 0 {
info!(
"Generated the partial (because we encountered errors) file: {}",
path.to_str().unwrap()
Expand All @@ -123,56 +121,42 @@ pub fn gexport<FD: Serialize + Clone, GD: Serialize + Clone>(
}

/// Export the translated ULLBC definitions to a JSON file.
#[allow(clippy::result_unit_err)]
pub fn export_ullbc(
errors: bool,
ctx: &TransCtx,
crate_name: String,
id_to_file: &HashMap<FileId::Id, FileName>,
ordered_decls: &DeclarationsGroups,
type_defs: &TypeDecls,
fun_defs: &ullbc_ast::FunDecls,
global_defs: &ullbc_ast::GlobalDecls,
trait_decls: &TraitDecls,
trait_impls: &TraitImpls,
dest_dir: &Option<PathBuf>,
) -> Result<(), ()> {
gexport(
errors,
ctx,
crate_name,
id_to_file,
ordered_decls,
type_defs,
fun_defs,
global_defs,
trait_decls,
trait_impls,
dest_dir,
"ullbc",
)
}

/// Export the translated LLBC definitions to a JSON file.
#[allow(clippy::result_unit_err)]
pub fn export_llbc(
errors: bool,
ctx: &TransCtx,
crate_name: String,
id_to_file: &HashMap<FileId::Id, FileName>,
ordered_decls: &DeclarationsGroups,
type_defs: &TypeDecls,
fun_defs: &llbc_ast::FunDecls,
global_defs: &llbc_ast::GlobalDecls,
trait_decls: &TraitDecls,
trait_impls: &TraitImpls,
dest_dir: &Option<PathBuf>,
) -> Result<(), ()> {
gexport(
errors,
ctx,
crate_name,
id_to_file,
ordered_decls,
type_defs,
fun_defs,
global_defs,
trait_decls,
trait_impls,
dest_dir,
"llbc",
)
Expand Down
1 change: 1 addition & 0 deletions charon/src/gast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub struct TraitItemName(pub String);
/// helpers like `all`, `map`, etc. that shouldn't be re-implemented.
/// Of course, this forbids other useful use cases such as visitors implemented
/// by means of traits.
#[allow(clippy::type_complexity)]
#[derive(Debug, Clone, Serialize)]
pub struct TraitDecl {
pub def_id: TraitDeclId::Id,
Expand Down
13 changes: 6 additions & 7 deletions charon/src/gast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn make_locals_generator(locals: &mut VarId::Vector<Var>) -> impl FnMut(Ty)
}

impl FunDeclId::Id {
pub fn to_pretty_string(&self) -> String {
pub fn to_pretty_string(self) -> String {
format!("@Fun{self}")
}
}
Expand Down Expand Up @@ -226,12 +226,11 @@ pub fn fmt_call<T>(ctx: &T, call: &Call) -> (String, Option<String>)
where
T: ExprFormatter,
{
let trait_and_method_generic_args =
if let Some(generics) = &call.func.trait_and_method_generic_args {
Option::Some(generics.fmt_with_ctx_split_trait_refs(ctx))
} else {
None
};
let trait_and_method_generic_args = call
.func
.trait_and_method_generic_args
.as_ref()
.map(|generics| generics.fmt_with_ctx_split_trait_refs(ctx));

let f = call.func.fmt_with_ctx(ctx);

Expand Down
3 changes: 2 additions & 1 deletion charon/src/id_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Map<Id, T> {
}

impl<Id: std::cmp::Ord, T> Map<Id, T> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Map {
map: std::collections::BTreeMap::new(),
Expand All @@ -35,7 +36,7 @@ impl<Id: std::cmp::Ord, T> Map<Id, T> {
}

pub fn iter(&self) -> impl Iterator<Item = &T> {
self.map.iter().map(|(_, x)| x)
self.map.values()
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
Expand Down
3 changes: 2 additions & 1 deletion charon/src/index_to_function_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a> Transform<'a> {
let (index_var_id, buf_ty) = pe.to_index();

let (id, generics) = buf_ty.as_adt();
let cgs: Vec<ConstGeneric> = generics.const_generics.iter().cloned().collect();
let cgs: Vec<ConstGeneric> = generics.const_generics.to_vec();
let index_id = match id.as_assumed() {
AssumedTy::Array => {
if mut_access {
Expand Down Expand Up @@ -173,6 +173,7 @@ impl<'a> MutExprVisitor for Transform<'a> {

impl<'a> MutAstVisitor for Transform<'a> {
fn spawn(&mut self, visitor: &mut dyn FnMut(&mut Self)) {
#[allow(clippy::mem_replace_with_default)]
let statements = replace(&mut self.statements, Vec::new());
visitor(self);
// Make sure we didn't update the vector of statements
Expand Down
61 changes: 28 additions & 33 deletions charon/src/remove_dynamic_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,40 +164,35 @@ impl MutAstVisitor for RemoveDynChecks {
&& move_p.projection.len() == 1
&& move_p1.projection.len() == 1
{
match (&move_p.projection[0], &move_p1.projection[0]) {
(
ProjectionElem::Field(FieldProjKind::Tuple(..), fid0),
ProjectionElem::Field(FieldProjKind::Tuple(..), fid1),
) => {
use crate::id_vector::ToUsize;
if fid0.to_usize() == 1 && fid1.to_usize() == 0 {
// Collapse into one assignment
take(s, |s| {
let (s0, s1) = s.content.to_sequence();
let (_, s2) = s1.content.to_sequence();
let (s2, s3) = s2.content.to_sequence();
let (_, op) = s0.content.to_assign();
let (dest, _) = s2.content.to_assign();
let meta0 = s0.meta;
let s0 = RawStatement::Assign(dest, op);
let s0 = Statement {
meta: meta0,
content: s0,
};
Statement {
meta: s2.meta,
content: RawStatement::Sequence(
Box::new(s0),
s3,
),
}
});
self.visit_statement(s);
// Return so as not to take the default branch
return;
}
if let (
ProjectionElem::Field(FieldProjKind::Tuple(..), fid0),
ProjectionElem::Field(FieldProjKind::Tuple(..), fid1),
) = (&move_p.projection[0], &move_p1.projection[0])
{
use crate::id_vector::ToUsize;
if fid0.to_usize() == 1 && fid1.to_usize() == 0 {
// Collapse into one assignment
take(s, |s| {
let (s0, s1) = s.content.to_sequence();
let (_, s2) = s1.content.to_sequence();
let (s2, s3) = s2.content.to_sequence();
let (_, op) = s0.content.to_assign();
let (dest, _) = s2.content.to_assign();
let meta0 = s0.meta;
let s0 = RawStatement::Assign(dest, op);
let s0 = Statement {
meta: meta0,
content: s0,
};
Statement {
meta: s2.meta,
content: RawStatement::Sequence(Box::new(s0), s3),
}
});
self.visit_statement(s);
// Return so as not to take the default branch
return;
}
_ => (),
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions charon/src/reorder_decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,15 @@ impl Deps {
//
// The declaration may not be present if we encountered errors.
if let Some(decl) = ctx.fun_defs.get(id) {
match &decl.kind {
FunKind::TraitMethodImpl {
impl_id,
trait_id: _,
method_name: _,
provided: _,
} => {
// Register the trait decl id
self.impl_trait_id = Some(*impl_id)
}
_ => (),
if let FunKind::TraitMethodImpl {
impl_id,
trait_id: _,
method_name: _,
provided: _,
} = &decl.kind
{
// Register the trait decl id
self.impl_trait_id = Some(*impl_id)
}
} else {
// Sanity check
Expand Down
16 changes: 6 additions & 10 deletions charon/src/translate_functions_to_ullbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ impl<'tcx, 'ctx, 'ctx1> BodyTransCtx<'tcx, 'ctx, 'ctx1> {
/// arguments.
///
/// TODO: should we always erase the regions?
#[allow(clippy::too_many_arguments)]
pub(crate) fn translate_fun_decl_id_with_args(
&mut self,
span: rustc_span::Span,
Expand Down Expand Up @@ -1306,6 +1307,7 @@ impl<'tcx, 'ctx, 'ctx1> BodyTransCtx<'tcx, 'ctx, 'ctx1> {
/// Note that `body` is the body of the function being translated, not of the
/// function referenced in the function call: we need it in order to translate
/// the blocks we go to after the function call returns.
#[allow(clippy::too_many_arguments)]
fn translate_function_call(
&mut self,
span: rustc_span::Span,
Expand Down Expand Up @@ -1636,11 +1638,8 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
// TODO: for now, if there is an error while translating the signature
// of the function, we ignore the function altogether, while we should
// save somewhere that we failed to extract it.
match self.translate_function_aux(rust_id) {
Ok(()) => (),
Err(_) => {
// TODO
}
if self.translate_function_aux(rust_id).is_err() {
// TODO
}
}

Expand Down Expand Up @@ -1712,11 +1711,8 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
// TODO: for now, if there is an error while translating the parameters/
// predicates of the global, we ignore the declaration altogether, while
// we should save somewhere that we failed to extract it.
match self.translate_global_aux(rust_id) {
Ok(()) => (),
Err(_) => {
// TODO
}
if self.translate_global_aux(rust_id).is_err() {
// TODO
}
}

Expand Down
Loading

0 comments on commit fb8fbd1

Please sign in to comment.