From ff9341a9e3c23494cbd51fabafd1275097d41203 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Matthias=20Kr=C3=BCger?= Date: Fri, 28 Feb 2020 13:36:45 +0100 Subject: [PATCH] remove redundant clones, references to operands, explicit boolean comparisons and filter(x).next() calls. --- src/librustc_infer/traits/wf.rs | 12 ++-- src/librustc_mir/dataflow/mod.rs | 6 +- src/librustc_parse/parser/stmt.rs | 2 +- src/librustc_resolve/late/diagnostics.rs | 70 +++++++++++------------- src/librustdoc/test.rs | 2 +- 5 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/librustc_infer/traits/wf.rs b/src/librustc_infer/traits/wf.rs index 993eb41b9b1..980a3f04781 100644 --- a/src/librustc_infer/traits/wf.rs +++ b/src/librustc_infer/traits/wf.rs @@ -232,10 +232,8 @@ fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elabo // found type `()` if let Some(hir::ItemKind::Impl { items, .. }) = item.map(|i| &i.kind) { let trait_assoc_item = tcx.associated_item(proj.projection_def_id()); - if let Some(impl_item) = items - .iter() - .filter(|item| item.ident == trait_assoc_item.ident) - .next() + if let Some(impl_item) = + items.iter().find(|item| item.ident == trait_assoc_item.ident) { cause.span = impl_item.span; cause.code = traits::AssocTypeBound(Box::new(AssocTypeBoundData { @@ -285,13 +283,11 @@ fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elabo { if let Some((impl_item, trait_assoc_item)) = trait_assoc_items .iter() - .filter(|i| i.def_id == *item_def_id) - .next() + .find(|i| i.def_id == *item_def_id) .and_then(|trait_assoc_item| { items .iter() - .filter(|i| i.ident == trait_assoc_item.ident) - .next() + .find(|i| i.ident == trait_assoc_item.ident) .map(|impl_item| (impl_item, trait_assoc_item)) }) { diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs index eccdac2fb99..a0d93d6d19a 100644 --- a/src/librustc_mir/dataflow/mod.rs +++ b/src/librustc_mir/dataflow/mod.rs @@ -689,11 +689,7 @@ pub trait BottomValue { /// 3. Override `join` to do the opposite from what it's doing now. #[inline] fn join(&self, inout_set: &mut BitSet, in_set: &BitSet) -> bool { - if Self::BOTTOM_VALUE == false { - inout_set.union(in_set) - } else { - inout_set.intersect(in_set) - } + if !Self::BOTTOM_VALUE { inout_set.union(in_set) } else { inout_set.intersect(in_set) } } } diff --git a/src/librustc_parse/parser/stmt.rs b/src/librustc_parse/parser/stmt.rs index 257292ae072..9073e131f70 100644 --- a/src/librustc_parse/parser/stmt.rs +++ b/src/librustc_parse/parser/stmt.rs @@ -89,7 +89,7 @@ fn parse_stmt_without_recovery(&mut self) -> PResult<'a, Option> { fn parse_stmt_item(&mut self, attrs: Vec) -> PResult<'a, Option> { let old = mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock); - let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?; + let item = self.parse_item_common(attrs, false, true, |_| true)?; self.directory.ownership = old; Ok(item) } diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index 957574cced7..e2aa853e78c 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -968,18 +968,14 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { for missing in &self.missing_named_lifetime_spots { match missing { MissingLifetimeSpot::Generics(generics) => { - let (span, sugg) = if let Some(param) = generics - .params - .iter() - .filter(|p| match p.kind { + let (span, sugg) = if let Some(param) = + generics.params.iter().find(|p| match p.kind { hir::GenericParamKind::Type { synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), .. } => false, _ => true, - }) - .next() - { + }) { (param.span.shrink_to_lo(), format!("{}, ", lifetime_ref)) } else { (generics.span, format!("<{}>", lifetime_ref)) @@ -1053,25 +1049,24 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { Applicability::MaybeIncorrect, ); }; - let suggest_new = - |err: &mut DiagnosticBuilder<'_>, sugg: &str| { - err.span_label(span, "expected named lifetime parameter"); + let suggest_new = |err: &mut DiagnosticBuilder<'_>, sugg: &str| { + err.span_label(span, "expected named lifetime parameter"); - for missing in self.missing_named_lifetime_spots.iter().rev() { - let mut introduce_suggestion = vec![]; - let msg; - let should_break; - introduce_suggestion.push(match missing { + for missing in self.missing_named_lifetime_spots.iter().rev() { + let mut introduce_suggestion = vec![]; + let msg; + let should_break; + introduce_suggestion.push(match missing { MissingLifetimeSpot::Generics(generics) => { msg = "consider introducing a named lifetime parameter".to_string(); should_break = true; - if let Some(param) = generics.params.iter().filter(|p| match p.kind { + if let Some(param) = generics.params.iter().find(|p| match p.kind { hir::GenericParamKind::Type { synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), .. } => false, _ => true, - }).next() { + }) { (param.span.shrink_to_lo(), "'a, ".to_string()) } else { (generics.span, "<'a>".to_string()) @@ -1090,30 +1085,29 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { (*span, span_type.suggestion("'a")) } }); - for param in params { - if let Ok(snippet) = - self.tcx.sess.source_map().span_to_snippet(param.span) - { - if snippet.starts_with("&") && !snippet.starts_with("&'") { - introduce_suggestion - .push((param.span, format!("&'a {}", &snippet[1..]))); - } else if snippet.starts_with("&'_ ") { - introduce_suggestion - .push((param.span, format!("&'a {}", &snippet[4..]))); - } + for param in params { + if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) + { + if snippet.starts_with("&") && !snippet.starts_with("&'") { + introduce_suggestion + .push((param.span, format!("&'a {}", &snippet[1..]))); + } else if snippet.starts_with("&'_ ") { + introduce_suggestion + .push((param.span, format!("&'a {}", &snippet[4..]))); } } - introduce_suggestion.push((span, sugg.to_string())); - err.multipart_suggestion( - &msg, - introduce_suggestion, - Applicability::MaybeIncorrect, - ); - if should_break { - break; - } } - }; + introduce_suggestion.push((span, sugg.to_string())); + err.multipart_suggestion( + &msg, + introduce_suggestion, + Applicability::MaybeIncorrect, + ); + if should_break { + break; + } + } + }; match ( lifetime_names.len(), diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 5dd7bd82755..4d51b63b740 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -398,7 +398,7 @@ pub fn make_test( use rustc_span::source_map::FilePathMapping; let filename = FileName::anon_source_code(s); - let source = crates + &everything_else; + let source = crates + everything_else; // Any errors in parsing should also appear when the doctest is compiled for real, so just // send all the errors that libsyntax emits directly into a `Sink` instead of stderr. -- 2.44.0