Skip to content

Commit

Permalink
Merge pull request #956 from hacspec/fix-clippy
Browse files Browse the repository at this point in the history
Run `cargo clippy --fix`
  • Loading branch information
maximebuyse authored Oct 3, 2024
2 parents 8ab6225 + f4d400d commit 52d7d71
Show file tree
Hide file tree
Showing 25 changed files with 139 additions and 160 deletions.
23 changes: 13 additions & 10 deletions cli/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,20 @@ const HAX_VANILLA_RUSTC: &str = "HAX_VANILLA_RUSTC";
fn main() {
setup_logging();

let options: hax_types::cli_options::Options =
serde_json::from_str(&std::env::var(ENV_VAR_OPTIONS_FRONTEND).expect(&format!(
"Cannot find environnement variable {}",
ENV_VAR_OPTIONS_FRONTEND
)))
.expect(&format!(
let options: hax_types::cli_options::Options = serde_json::from_str(
&std::env::var(ENV_VAR_OPTIONS_FRONTEND).unwrap_or_else(|_| {
panic!(
"Cannot find environnement variable {}",
ENV_VAR_OPTIONS_FRONTEND
)
}),
)
.unwrap_or_else(|_| {
panic!(
"Invalid value for the environnement variable {}",
ENV_VAR_OPTIONS_FRONTEND
));
)
});

let mut rustc_args: Vec<String> = std::env::args().skip(1).collect();
// add [--sysroot] if not present
Expand Down Expand Up @@ -140,9 +145,7 @@ fn main() {
adt_const_params: false, // not useful for now
generic_const_exprs: false, // not useful for now
register_tool: true,
registered_tools: HashSet::from_iter(
vec![hax_lib_macros_types::HAX_TOOL.into()].into_iter(),
),
registered_tools: HashSet::from_iter(vec![hax_lib_macros_types::HAX_TOOL.into()]),
auto_traits: true,
negative_impls: true,
} - Features::detect_forking();
Expand Down
15 changes: 7 additions & 8 deletions cli/driver/src/exporter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use hax_frontend_exporter;
use hax_frontend_exporter::state::{ExportedSpans, LocalContextS};
use hax_frontend_exporter::SInto;
use hax_types::cli_options::{Backend, PathOrDash, ENV_VAR_OPTIONS_FRONTEND};
Expand All @@ -19,11 +18,11 @@ use std::rc::Rc;

type ThirBundle<'tcx> = (Rc<rustc_middle::thir::Thir<'tcx>>, ExprId);
/// Generates a dummy THIR body with an error literal as first expression
fn dummy_thir_body<'tcx>(
tcx: TyCtxt<'tcx>,
fn dummy_thir_body(
tcx: TyCtxt<'_>,
span: rustc_span::Span,
guar: rustc_errors::ErrorGuaranteed,
) -> ThirBundle<'tcx> {
) -> ThirBundle<'_> {
use rustc_middle::thir::*;
let ty = tcx.mk_ty_from_kind(rustc_type_ir::TyKind::Never);
let mut thir = Thir::new(BodyTy::Const(ty));
Expand All @@ -45,9 +44,9 @@ fn dummy_thir_body<'tcx>(

/// Precompute all THIR bodies in a certain order so that we avoid
/// stealing issues (theoretically...)
fn precompute_local_thir_bodies<'tcx>(
tcx: TyCtxt<'tcx>,
) -> std::collections::HashMap<rustc_span::def_id::LocalDefId, ThirBundle<'tcx>> {
fn precompute_local_thir_bodies(
tcx: TyCtxt<'_>,
) -> std::collections::HashMap<rustc_span::def_id::LocalDefId, ThirBundle<'_>> {
let hir = tcx.hir();
use rustc_hir::def::DefKind::*;
use rustc_hir::*;
Expand Down Expand Up @@ -79,7 +78,7 @@ fn precompute_local_thir_bodies<'tcx>(
.filter(|ldid| {
match tcx.def_kind(ldid.to_def_id()) {
InlineConst | AnonConst => {
fn is_fn<'tcx>(tcx: TyCtxt<'tcx>, did: rustc_span::def_id::DefId) -> bool {
fn is_fn(tcx: TyCtxt<'_>, did: rustc_span::def_id::DefId) -> bool {
use rustc_hir::def::DefKind;
matches!(
tcx.def_kind(did),
Expand Down
2 changes: 1 addition & 1 deletion cli/driver/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Features {
};
let exit_code = rustc_driver::catch_with_exit_code(|| {
rustc_driver::RunCompiler::new(
&rustc_args,
rustc_args,
&mut CallbacksWrapper {
sub: &mut callbacks,
options: options.clone(),
Expand Down
18 changes: 7 additions & 11 deletions cli/subcommands/src/cargo_hax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ fn find_hax_engine(message_format: MessageFormat) -> process::Command {

std::env::var("HAX_ENGINE_BINARY")
.ok()
.map(|name| process::Command::new(name))
.or_else(|| {
which(ENGINE_BINARY_NAME)
.ok()
.map(|name| process::Command::new(name))
})
.map(process::Command::new)
.or_else(|| which(ENGINE_BINARY_NAME).ok().map(process::Command::new))
.or_else(|| {
which("node").ok().and_then(|_| {
if let Ok(true) = inquire::Confirm::new(&format!(
Expand Down Expand Up @@ -184,7 +180,7 @@ impl HaxMessage {
}
Self::CargoBuildFailure => {
let title =
format!("hax: running `cargo build` was not successful, continuing anyway.");
"hax: running `cargo build` was not successful, continuing anyway.".to_string();
eprintln!("{}", renderer.render(Level::Warning.title(&title)));
}
Self::WarnExperimentalBackend { backend } => {
Expand Down Expand Up @@ -215,14 +211,13 @@ fn run_engine(
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.map_err(|e| {
.inspect_err(|e| {
if let std::io::ErrorKind::NotFound = e.kind() {
panic!(
"The binary [{}] was not found in your [PATH].",
ENGINE_BINARY_NAME
)
}
e
})
.unwrap();

Expand Down Expand Up @@ -288,7 +283,7 @@ fn run_engine(
output.files.push(file)
} else {
let path = out_dir.join(&file.path);
std::fs::create_dir_all(&path.parent().unwrap()).unwrap();
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let mut wrote = false;
if fs::read_to_string(&path).as_ref().ok() != Some(&file.contents) {
std::fs::write(&path, file.contents).unwrap();
Expand Down Expand Up @@ -326,7 +321,8 @@ fn run_engine(
if !exit_status.success() {
HaxMessage::HaxEngineFailure {
exit_code: exit_status.code().unwrap_or(-1),
};
}
.report(message_format, None);
std::process::exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion engine/utils/phase-debug-webapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn run(get_json: impl Fn() -> String) {
path if path.starts_with("/debug-hax-engine.json") => {
Response::from_string(get_json()).with_header(ct_utf8.clone())
}
_ => Response::from_string(format!("Unknown route")).with_status_code(404),
_ => Response::from_string("Unknown route".to_string()).with_status_code(404),
};
let _ = request.respond(response);
}
Expand Down
5 changes: 2 additions & 3 deletions frontend/exporter/options/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ impl std::convert::From<String> for Namespace {
Namespace {
chunks: s
.split("::")
.into_iter()
.filter(|s| s.len() > 0)
.map(|s| NamespaceChunk::from(s))
.filter(|s| !s.is_empty())
.map(NamespaceChunk::from)
.collect(),
}
}
Expand Down
10 changes: 4 additions & 6 deletions frontend/exporter/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod module {
s: &S,
) -> FnDef<Body> {
let hir_id = body_id.hir_id;
let ldid = hir_id.clone().owner.def_id;
let ldid = hir_id.owner.def_id;

let (thir, expr_entrypoint) = get_thir(ldid, s);
let s = &with_owner_id(s.base(), thir.clone(), (), ldid.to_def_id());
Expand Down Expand Up @@ -64,15 +64,13 @@ mod module {
mod implementations {
use super::*;
impl IsBody for () {
fn body<'tcx, S: UnderOwnerState<'tcx>>(_did: RLocalDefId, _s: &S) -> Self {
()
}
fn body<'tcx, S: UnderOwnerState<'tcx>>(_did: RLocalDefId, _s: &S) -> Self {}
}
impl IsBody for ThirBody {
fn body<'tcx, S: UnderOwnerState<'tcx>>(did: RLocalDefId, s: &S) -> Self {
let (thir, expr) = get_thir(did, s);
if *CORE_EXTRACTION_MODE {
let expr = &thir.exprs[expr.clone()];
let expr = &thir.exprs[expr];
Decorated {
contents: Box::new(ExprKind::Tuple { fields: vec![] }),
hir_id: None,
Expand Down Expand Up @@ -111,7 +109,7 @@ mod module {

impl<'tcx, S: UnderOwnerState<'tcx>, Body: IsBody> SInto<S, Body> for rustc_hir::BodyId {
fn sinto(&self, s: &S) -> Body {
body_from_id::<Body, _>(self.clone(), s)
body_from_id::<Body, _>(*self, s)
}
}
}
12 changes: 5 additions & 7 deletions frontend/exporter/src/constant_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ mod rustc {
) -> ConstantLiteral {
match ty.kind() {
ty::Char => ConstantLiteral::Char(
char::try_from(x)
.s_expect(s, "scalar_int_to_constant_literal: expected a char")
.into(),
char::try_from(x).s_expect(s, "scalar_int_to_constant_literal: expected a char"),
),
ty::Bool => ConstantLiteral::Bool(
x.try_to_bool()
Expand Down Expand Up @@ -296,7 +294,7 @@ mod rustc {
},
);
ConstantExprKind::Literal(ConstantLiteral::ByteStr(
values.iter().copied().collect(),
values.to_vec(),
StrStyle::Cooked,
))
}
Expand Down Expand Up @@ -356,9 +354,9 @@ mod rustc {
/// Rustc; we don't want to reflect that, instead we prefer inlining
/// those. `is_anon_const` is used to detect such AnonConst so that we
/// can evaluate and inline them.
pub(crate) fn is_anon_const<'tcx>(
pub(crate) fn is_anon_const(
did: rustc_span::def_id::DefId,
tcx: rustc_middle::ty::TyCtxt<'tcx>,
tcx: rustc_middle::ty::TyCtxt<'_>,
) -> bool {
matches!(
tcx.def_path(did).data.last().map(|x| x.data),
Expand Down Expand Up @@ -489,7 +487,7 @@ mod rustc {
}

ty::ConstKind::Unevaluated(ucv) => match self.translate_uneval(s, ucv, span) {
TranslateUnevalRes::EvaluatedConstant(c) => return c.sinto(s),
TranslateUnevalRes::EvaluatedConstant(c) => c.sinto(s),
TranslateUnevalRes::GlobalName(c) => c,
},
ty::ConstKind::Value(ty, valtree) => valtree_to_constant_expr(s, valtree, ty, span),
Expand Down
6 changes: 3 additions & 3 deletions frontend/exporter/src/index_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl<I: rustc_index::Idx, T: Sized> std::ops::DerefMut for IndexVec<I, T> {
}

#[cfg(feature = "rustc")]
impl<I: rustc_index::Idx, T> Into<IndexVec<I, T>> for rustc_index::IndexVec<I, T> {
fn into(self) -> IndexVec<I, T> {
impl<I: rustc_index::Idx, T> From<rustc_index::IndexVec<I, T>> for IndexVec<I, T> {
fn from(val: rustc_index::IndexVec<I, T>) -> Self {
IndexVec {
raw: self.raw,
raw: val.raw,
_marker: std::marker::PhantomData,
}
}
Expand Down
11 changes: 5 additions & 6 deletions frontend/exporter/src/rustc_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn get_variant_information<'s, S: UnderOwnerState<'s>>(
s_assert!(s, !adt_def.is_union() || *CORE_EXTRACTION_MODE);
fn is_record<'s, I: std::iter::Iterator<Item = &'s ty::FieldDef> + Clone>(it: I) -> bool {
it.clone()
.any(|field| !field.name.to_ident_string().parse::<u64>().is_ok())
.any(|field| field.name.to_ident_string().parse::<u64>().is_err())
}
let variant_def = adt_def.variant(variant_index);
let variant = variant_def.def_id;
Expand Down Expand Up @@ -109,7 +109,7 @@ pub(crate) fn read_span_from_file(span: &Span) -> Result<String, ReadSpanErr> {
let first = first.chars().skip(span.lo.col).collect();
let last = last.chars().take(span.hi.col).collect();
Ok(std::iter::once(first)
.chain(middle.into_iter().cloned())
.chain(middle.iter().cloned())
.chain(std::iter::once(last))
.collect::<Vec<String>>()
.join("\n"))
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'tcx, S: UnderOwnerState<'tcx>> ParamEnv<'tcx> for S {

#[tracing::instrument]
pub fn argument_span_of_mac_call(mac_call: &rustc_ast::ast::MacCall) -> rustc_span::Span {
(*mac_call.args).dspan.entire()
mac_call.args.dspan.entire()
}
#[tracing::instrument(skip(state))]
pub(crate) fn raw_macro_invocation_of_span<'t, S: BaseState<'t>>(
Expand Down Expand Up @@ -224,7 +224,7 @@ pub(crate) fn attribute_from_scope<'tcx, S: ExprState<'tcx>>(
let map = tcx.hir();
let attributes = hir_id
.map(|hir_id| map.attrs(hir_id).sinto(s))
.unwrap_or(vec![]);
.unwrap_or_default();
(hir_id, attributes)
}

Expand All @@ -247,7 +247,7 @@ pub fn inline_macro_invocations<'t, S: BaseState<'t>, Body: IsBody>(
ids.map(|id| tcx.hir().item(id))
.group_by(|item| SpanEq(raw_macro_invocation_of_span(item.span, s)))
.into_iter()
.map(|(mac, items)| match mac.0 {
.flat_map(|(mac, items)| match mac.0 {
Some((macro_ident, expn_data)) => {
let owner_id: rustc_hir::hir_id::OwnerId =
items.into_iter().map(|x| x.owner_id).next().s_unwrap(s);
Expand All @@ -267,6 +267,5 @@ pub fn inline_macro_invocations<'t, S: BaseState<'t>, Body: IsBody>(
}
_ => items.map(|item| item.sinto(s)).collect(),
})
.flatten()
.collect()
}
10 changes: 5 additions & 5 deletions frontend/exporter/src/sinto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod test {
pub struct Foo(usize);
impl Foo {
pub fn as_usize(&self) -> usize {
self.0.clone()
self.0
}
}
}
Expand Down Expand Up @@ -76,24 +76,24 @@ impl<S, D, T: SInto<S, D>> SInto<S, D> for &T {
}
impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for [T] {
fn sinto(&self, s: &S) -> Vec<D> {
self.into_iter().map(|x| x.sinto(s)).collect()
self.iter().map(|x| x.sinto(s)).collect()
}
}
impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Box<[T]> {
fn sinto(&self, s: &S) -> Vec<D> {
(&*self).into_iter().map(|x| x.sinto(s)).collect()
self.into_iter().map(|x| x.sinto(s)).collect()
}
}

impl<S, D: Clone, T: SInto<S, D>> SInto<S, Vec<D>> for Vec<T> {
fn sinto(&self, s: &S) -> Vec<D> {
self.into_iter().map(|x| x.sinto(s)).collect()
self.iter().map(|x| x.sinto(s)).collect()
}
}
#[cfg(feature = "rustc")]
impl<S> SInto<S, Vec<u8>> for rustc_data_structures::sync::Lrc<[u8]> {
fn sinto(&self, _s: &S) -> Vec<u8> {
(**self).iter().cloned().collect()
(**self).to_vec()
}
}

Expand Down
Loading

0 comments on commit 52d7d71

Please sign in to comment.