From 23799784e484e4238e31f8511f8dcf43d4447a49 Mon Sep 17 00:00:00 2001 From: Vincent Lafeychine <vincent.lafeychine@proton.me> Date: Fri, 10 May 2024 12:10:43 +0200 Subject: [PATCH] clippy: Fix if_then_some_else_none --- rr_frontend/.cargo/config.toml | 1 - rr_frontend/attribute_parse/src/parse.rs | 9 ++++---- .../src/environment/polonius_info.rs | 11 ++++----- rr_frontend/translation/src/function_body.rs | 4 ++-- rr_frontend/translation/src/utils.rs | 23 ++++++++++--------- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/rr_frontend/.cargo/config.toml b/rr_frontend/.cargo/config.toml index 073b18d4..9f24d00e 100644 --- a/rr_frontend/.cargo/config.toml +++ b/rr_frontend/.cargo/config.toml @@ -32,7 +32,6 @@ rustflags = [ "-Aclippy::string_lit_as_bytes", # clippy::restriction - "-Aclippy::if_then_some_else_none", "-Aclippy::impl_trait_in_params", "-Aclippy::mod_module_files", "-Aclippy::non_ascii_literal", diff --git a/rr_frontend/attribute_parse/src/parse.rs b/rr_frontend/attribute_parse/src/parse.rs index cb9366a9..da1ce9b1 100644 --- a/rr_frontend/attribute_parse/src/parse.rs +++ b/rr_frontend/attribute_parse/src/parse.rs @@ -610,15 +610,14 @@ mod value { } let suffix = s; - if suffix.is_empty() || crate::parse::xid_ok(suffix) { + + (suffix.is_empty() || crate::parse::xid_ok(suffix)).then(|| { let mut repr = value.to_string(); if negative { repr.insert(0, '-'); } - Some((repr.into_boxed_str(), suffix.to_owned().into_boxed_str())) - } else { - None - } + (repr.into_boxed_str(), suffix.to_owned().into_boxed_str()) + }) } /// Get the byte at offset idx, or a default of `b'\0'` if we're looking diff --git a/rr_frontend/translation/src/environment/polonius_info.rs b/rr_frontend/translation/src/environment/polonius_info.rs index 03b3102d..ab0b9605 100644 --- a/rr_frontend/translation/src/environment/polonius_info.rs +++ b/rr_frontend/translation/src/environment/polonius_info.rs @@ -603,7 +603,7 @@ impl<'a, 'tcx: 'a> PoloniusInfo<'a, 'tcx> { .borrowck_in_facts .loan_issued_at .iter() - .filter_map(|(r, loan, _)| if r == ®ion { Some(*loan) } else { None }) + .filter_map(|(r, loan, _)| (r == ®ion).then_some(*loan)) .collect(); if !v.is_empty() { @@ -1105,11 +1105,7 @@ impl<'a, 'tcx: 'a> PoloniusInfo<'a, 'tcx> { /// TODO: for aggregates, this only finds one loan #[must_use] pub fn get_optional_loan_at_location(&self, location: mir::Location) -> Option<facts::Loan> { - if self.loan_at_position.contains_key(&location) { - Some(self.loan_at_position[&location]) - } else { - None - } + self.loan_at_position.contains_key(&location).then(|| self.loan_at_position[&location]) } /// Get a map from loans to locations at which they were created. @@ -1217,8 +1213,9 @@ impl<'a, 'tcx: 'a> PoloniusInfo<'a, 'tcx> { .borrowck_in_facts .loan_issued_at .iter() - .filter_map(|&(r, l, p)| if p == point && l == loan { Some(r) } else { None }) + .filter_map(|&(r, l, p)| (p == point && l == loan).then_some(r)) .collect::<Vec<_>>(); + if regions.len() == 1 { regions[0] } else { diff --git a/rr_frontend/translation/src/function_body.rs b/rr_frontend/translation/src/function_body.rs index bd6a5080..ce21a7b8 100644 --- a/rr_frontend/translation/src/function_body.rs +++ b/rr_frontend/translation/src/function_body.rs @@ -2325,7 +2325,7 @@ impl<'a, 'def: 'a, 'tcx: 'def> BodyTranslator<'a, 'def, 'tcx> { /// Generate an annotation on an expression needed to update the region name map. fn generate_strong_update_annot(&self, ty: PlaceTy<'tcx>) -> Option<radium::Annotation> { let (interesting, tree) = self.generate_strong_update_annot_rec(ty.ty); - if interesting { Some(radium::Annotation::GetLftNames(tree)) } else { None } + interesting.then(|| radium::Annotation::GetLftNames(tree)) } /// Returns a tree for giving names to Coq lifetimes based on RR types. @@ -2400,7 +2400,7 @@ impl<'a, 'def: 'a, 'tcx: 'def> BodyTranslator<'a, 'def, 'tcx> { Ok(self .procedure_registry .lookup_function_mode(*did) - .and_then(|m| if m.is_ignore() { Some(*did) } else { None })) + .and_then(|m| m.is_ignore().then_some(*did))) } fn region_to_region_vid(r: ty::Region<'tcx>) -> facts::Region { diff --git a/rr_frontend/translation/src/utils.rs b/rr_frontend/translation/src/utils.rs index ecaf56a6..7bc4080e 100644 --- a/rr_frontend/translation/src/utils.rs +++ b/rr_frontend/translation/src/utils.rs @@ -554,22 +554,23 @@ pub fn try_pop_one_level<'tcx>( tcx: TyCtxt<'tcx>, place: mir::Place<'tcx>, ) -> Option<(mir::PlaceElem<'tcx>, mir::Place<'tcx>)> { - if place.projection.len() > 0 { - let last_index = place.projection.len() - 1; - let new_place = mir::Place { - local: place.local, - projection: tcx.mk_place_elems(&place.projection[..last_index]), - }; - Some((place.projection[last_index], new_place)) - } else { - None + if place.projection.is_empty() { + return None; } + + let last_index = place.projection.len() - 1; + let new_place = mir::Place { + local: place.local, + projection: tcx.mk_place_elems(&place.projection[..last_index]), + }; + + Some((place.projection[last_index], new_place)) } /// Pop the last element from the place if it is a dereference. pub fn try_pop_deref<'tcx>(tcx: TyCtxt<'tcx>, place: mir::Place<'tcx>) -> Option<mir::Place<'tcx>> { try_pop_one_level(tcx, place) - .and_then(|(elem, base)| if elem == mir::ProjectionElem::Deref { Some(base) } else { None }) + .and_then(|(elem, base)| (elem == mir::ProjectionElem::Deref).then_some(base)) } /// Subtract the `subtrahend` place from the `minuend` place. The @@ -724,7 +725,7 @@ pub fn filter_tool_attrs(attrs: &[ast::Attribute]) -> Vec<&ast::AttrItem> { let seg = item.path.segments.get(0)?; - if seg.ident.name.as_str() == config::spec_hotword() { Some(item) } else { None } + (seg.ident.name.as_str() == config::spec_hotword()).then_some(item) }, _ => None, }) -- GitLab