From 08f2904dfaf5e75fbcc1305c8b0aad5fae71a4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 29 Mar 2020 20:19:14 +0200 Subject: [PATCH] more clippy fixes use is_empty() instead of len comparison (clippy::len_zero) use if let instead of while let loop that never loops (clippy::never_loop) remove redundant returns (clippy::needless_return) remove redundant closures (clippy::redundant_closure) use if let instead of match and wildcard pattern (clippy::single_match) don't repeat field names redundantly (clippy::redundant_field_names) --- src/librustc_interface/passes.rs | 2 +- src/librustc_mir/interpret/terminator.rs | 2 +- src/librustc_save_analysis/lib.rs | 2 +- src/librustc_session/config.rs | 2 +- src/librustc_target/spec/i686_apple_darwin.rs | 2 +- .../spec/x86_64_apple_darwin.rs | 2 +- src/librustdoc/clean/auto_trait.rs | 7 +- src/librustdoc/clean/mod.rs | 32 ++--- src/librustdoc/clean/utils.rs | 31 ++--- src/librustdoc/html/markdown.rs | 123 +++++++++--------- src/librustdoc/html/render.rs | 13 +- src/librustdoc/html/render/cache.rs | 2 +- src/libstd/io/stdio.rs | 4 +- src/libstd/sys/unix/process/process_unix.rs | 2 +- 14 files changed, 102 insertions(+), 124 deletions(-) diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 7d066a31a0797..e3fc4fa52fb61 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -711,7 +711,7 @@ impl<'tcx> QueryContext<'tcx> { } pub fn print_stats(&mut self) { - self.enter(|tcx| ty::query::print_stats(tcx)) + self.enter(ty::query::print_stats) } } diff --git a/src/librustc_mir/interpret/terminator.rs b/src/librustc_mir/interpret/terminator.rs index a490a0f9bcc1d..8e7abcd09c233 100644 --- a/src/librustc_mir/interpret/terminator.rs +++ b/src/librustc_mir/interpret/terminator.rs @@ -30,7 +30,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { trace!("SwitchInt({:?})", *discr); // Branch to the `otherwise` case by default, if no match is found. - assert!(targets.len() > 0); + assert!(!targets.is_empty()); let mut target_block = targets[targets.len() - 1]; for (index, &const_int) in values.iter().enumerate() { diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 277848a7d6172..24b7be0c9b306 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -591,7 +591,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> { Some(Data::RefData(Ref { kind: RefKind::Function, span, - ref_id: def_id.or(decl_id).map(id_from_def_id).unwrap_or_else(|| null_id()), + ref_id: def_id.or(decl_id).map(id_from_def_id).unwrap_or_else(null_id), })) } ast::ExprKind::Path(_, ref path) => { diff --git a/src/librustc_session/config.rs b/src/librustc_session/config.rs index 663cfa223c7a8..54481bd124df5 100644 --- a/src/librustc_session/config.rs +++ b/src/librustc_session/config.rs @@ -1650,7 +1650,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { check_thread_count(&debugging_opts, error_format); - let incremental = cg.incremental.as_ref().map(|m| PathBuf::from(m)); + let incremental = cg.incremental.as_ref().map(PathBuf::from); if debugging_opts.profile && incremental.is_some() { early_error( diff --git a/src/librustc_target/spec/i686_apple_darwin.rs b/src/librustc_target/spec/i686_apple_darwin.rs index 033b87b110e12..b7a34f9740ac4 100644 --- a/src/librustc_target/spec/i686_apple_darwin.rs +++ b/src/librustc_target/spec/i686_apple_darwin.rs @@ -16,7 +16,7 @@ pub fn target() -> TargetResult { let llvm_target = super::apple_base::macos_llvm_target(&arch); Ok(Target { - llvm_target: llvm_target, + llvm_target, target_endian: "little".to_string(), target_pointer_width: "32".to_string(), target_c_int_width: "32".to_string(), diff --git a/src/librustc_target/spec/x86_64_apple_darwin.rs b/src/librustc_target/spec/x86_64_apple_darwin.rs index e846f42f8f849..31011e8474958 100644 --- a/src/librustc_target/spec/x86_64_apple_darwin.rs +++ b/src/librustc_target/spec/x86_64_apple_darwin.rs @@ -16,7 +16,7 @@ pub fn target() -> TargetResult { let llvm_target = super::apple_base::macos_llvm_target(&arch); Ok(Target { - llvm_target: llvm_target, + llvm_target, target_endian: "little".to_string(), target_pointer_width: "64".to_string(), target_c_int_width: "32".to_string(), diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index bf64da56ade08..9f327b0b743d8 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -497,11 +497,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { // of the type. // Therefore, we make sure that we never add a ?Sized // bound for projections - match &ty { - &Type::QPath { .. } => { - has_sized.insert(ty.clone()); - } - _ => {} + if let Type::QPath { .. } = ty { + has_sized.insert(ty.clone()); } if bounds.is_empty() { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c09e74118644c..f8e44bc5a1ab7 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -521,11 +521,8 @@ impl<'tcx> Clean> fn clean(&self, cx: &DocContext<'_>) -> Option { let ty::OutlivesPredicate(ref a, ref b) = *self; - match (a, b) { - (ty::ReEmpty(_), ty::ReEmpty(_)) => { - return None; - } - _ => {} + if let (ty::ReEmpty(_), ty::ReEmpty(_)) = (a, b) { + return None; } Some(WherePredicate::RegionPredicate { @@ -539,9 +536,8 @@ impl<'tcx> Clean> for ty::OutlivesPredicate, ty: fn clean(&self, cx: &DocContext<'_>) -> Option { let ty::OutlivesPredicate(ref ty, ref lt) = *self; - match lt { - ty::ReEmpty(_) => return None, - _ => {} + if let ty::ReEmpty(_) = lt { + return None; } Some(WherePredicate::BoundPredicate { @@ -2239,15 +2235,12 @@ impl Clean> for doctree::Import<'_> { } else { let name = self.name; if !please_inline { - match path.res { - Res::Def(DefKind::Mod, did) => { - if !did.is_local() && did.index == CRATE_DEF_INDEX { - // if we're `pub use`ing an extern crate root, don't inline it unless we - // were specifically asked for it - denied = true; - } + if let Res::Def(DefKind::Mod, did) = path.res { + if !did.is_local() && did.index == CRATE_DEF_INDEX { + // if we're `pub use`ing an extern crate root, don't inline it unless we + // were specifically asked for it + denied = true; } - _ => {} } } if !denied { @@ -2426,10 +2419,9 @@ impl From for SimpleBound { GenericBound::TraitBound(t, mod_) => match t.trait_ { Type::ResolvedPath { path, param_names, .. } => SimpleBound::TraitBound( path.segments, - param_names.map_or_else( - || Vec::new(), - |v| v.iter().map(|p| SimpleBound::from(p.clone())).collect(), - ), + param_names.map_or_else(Vec::new, |v| { + v.iter().map(|p| SimpleBound::from(p.clone())).collect() + }), t.generic_params, mod_, ), diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 05a8302ebab67..9e96015d306e4 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -121,7 +121,7 @@ pub fn external_generic_args( let args: Vec<_> = substs .iter() .filter_map(|kind| match kind.unpack() { - GenericArgKind::Lifetime(lt) => lt.clean(cx).map(|lt| GenericArg::Lifetime(lt)), + GenericArgKind::Lifetime(lt) => lt.clean(cx).map(GenericArg::Lifetime), GenericArgKind::Type(_) if skip_self => { skip_self = false; None @@ -198,27 +198,24 @@ pub fn get_real_types( }) { let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]); for bound in bounds.iter() { - match *bound { - GenericBound::TraitBound(ref poly_trait, _) => { - for x in poly_trait.generic_params.iter() { - if !x.is_type() { - continue; - } - if let Some(ty) = x.get_type() { - let adds = get_real_types(generics, &ty, cx, recurse + 1); - if !adds.is_empty() { - res.extend(adds); - } else if !ty.is_full_generic() { - if let Some(did) = ty.def_id() { - if let Some(kind) = cx.tcx.def_kind(did).clean(cx) { - res.insert((ty, kind)); - } + if let GenericBound::TraitBound(ref poly_trait, _) = *bound { + for x in poly_trait.generic_params.iter() { + if !x.is_type() { + continue; + } + if let Some(ty) = x.get_type() { + let adds = get_real_types(generics, &ty, cx, recurse + 1); + if !adds.is_empty() { + res.extend(adds); + } else if !ty.is_full_generic() { + if let Some(did) = ty.def_id() { + if let Some(kind) = cx.tcx.def_kind(did).clean(cx) { + res.insert((ty, kind)); } } } } } - _ => {} } } } diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index e13bf270440e2..9fe3e35d197fa 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -448,7 +448,7 @@ impl<'a, I: Iterator>> Iterator for SummaryLine<'a, I> { if !self.started { self.started = true; } - while let Some(event) = self.inner.next() { + if let Some(event) = self.inner.next() { let mut is_start = true; let is_allowed_tag = match event { Event::Start(Tag::CodeBlock(_)) | Event::End(Tag::CodeBlock(_)) => { @@ -944,75 +944,70 @@ crate fn rust_code_blocks(md: &str) -> Vec { let mut p = Parser::new_ext(md, opts()).into_offset_iter(); while let Some((event, offset)) = p.next() { - match event { - Event::Start(Tag::CodeBlock(syntax)) => { - let (syntax, code_start, code_end, range, is_fenced) = match syntax { - CodeBlockKind::Fenced(syntax) => { - let syntax = syntax.as_ref(); - let lang_string = if syntax.is_empty() { - LangString::all_false() - } else { - LangString::parse(&*syntax, ErrorCodes::Yes, false) - }; - if !lang_string.rust { + if let Event::Start(Tag::CodeBlock(syntax)) = event { + let (syntax, code_start, code_end, range, is_fenced) = match syntax { + CodeBlockKind::Fenced(syntax) => { + let syntax = syntax.as_ref(); + let lang_string = if syntax.is_empty() { + LangString::all_false() + } else { + LangString::parse(&*syntax, ErrorCodes::Yes, false) + }; + if !lang_string.rust { + continue; + } + let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) }; + let (code_start, mut code_end) = match p.next() { + Some((Event::Text(_), offset)) => (offset.start, offset.end), + Some((_, sub_offset)) => { + let code = Range { start: sub_offset.start, end: sub_offset.start }; + code_blocks.push(RustCodeBlock { + is_fenced: true, + range: offset, + code, + syntax, + }); continue; } - let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) }; - let (code_start, mut code_end) = match p.next() { - Some((Event::Text(_), offset)) => (offset.start, offset.end), - Some((_, sub_offset)) => { - let code = Range { start: sub_offset.start, end: sub_offset.start }; - code_blocks.push(RustCodeBlock { - is_fenced: true, - range: offset, - code, - syntax, - }); - continue; - } - None => { - let code = Range { start: offset.end, end: offset.end }; - code_blocks.push(RustCodeBlock { - is_fenced: true, - range: offset, - code, - syntax, - }); - continue; - } - }; - while let Some((Event::Text(_), offset)) = p.next() { - code_end = offset.end; + None => { + let code = Range { start: offset.end, end: offset.end }; + code_blocks.push(RustCodeBlock { + is_fenced: true, + range: offset, + code, + syntax, + }); + continue; } - (syntax, code_start, code_end, offset, true) + }; + while let Some((Event::Text(_), offset)) = p.next() { + code_end = offset.end; } - CodeBlockKind::Indented => { - // The ending of the offset goes too far sometime so we reduce it by one in - // these cases. - if offset.end > offset.start - && md.get(offset.end..=offset.end) == Some(&"\n") - { - ( - None, - offset.start, - offset.end, - Range { start: offset.start, end: offset.end - 1 }, - false, - ) - } else { - (None, offset.start, offset.end, offset, false) - } + (syntax, code_start, code_end, offset, true) + } + CodeBlockKind::Indented => { + // The ending of the offset goes too far sometime so we reduce it by one in + // these cases. + if offset.end > offset.start && md.get(offset.end..=offset.end) == Some(&"\n") { + ( + None, + offset.start, + offset.end, + Range { start: offset.start, end: offset.end - 1 }, + false, + ) + } else { + (None, offset.start, offset.end, offset, false) } - }; + } + }; - code_blocks.push(RustCodeBlock { - is_fenced, - range, - code: Range { start: code_start, end: code_end }, - syntax, - }); - } - _ => (), + code_blocks.push(RustCodeBlock { + is_fenced, + range, + code: Range { start: code_start, end: code_end }, + syntax, + }); } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 1377338ce4af3..f51f47a8d3387 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -782,7 +782,7 @@ themePicker.onblur = handleThemeButtonsBlur; .split('"') .next() .map(|s| s.to_owned()) - .unwrap_or_else(|| String::new()), + .unwrap_or_else(String::new), ); } } @@ -2158,7 +2158,7 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean: docs = MarkdownSummaryLine(doc_value, &myitem.links()).to_string(), class = myitem.type_(), add = add, - stab = stab.unwrap_or_else(|| String::new()), + stab = stab.unwrap_or_else(String::new), unsafety_flag = unsafety_flag, href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()), title = [full_path(cx, myitem), myitem.type_().to_string()] @@ -4593,12 +4593,9 @@ fn collect_paths_for_type(first_ty: clean::Type) -> Vec { let get_extern = || cache.external_paths.get(&did).map(|s| s.0.clone()); let fqp = cache.exact_paths.get(&did).cloned().or_else(get_extern); - match fqp { - Some(path) => { - out.push(path.join("::")); - } - _ => {} - }; + if let Some(path) = fqp { + out.push(path.join("::")); + } } clean::Type::Tuple(tys) => { work.extend(tys.into_iter()); diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 450f44ffeafc1..d9f6f7b466a1f 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -590,7 +590,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { for item in search_index { item.parent_idx = item.parent.and_then(|defid| { if defid_to_pathid.contains_key(&defid) { - defid_to_pathid.get(&defid).map(|x| *x) + defid_to_pathid.get(&defid).copied() } else { let pathid = lastpathid; defid_to_pathid.insert(defid, pathid); diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 9a82ae7626d97..a064c552c84f5 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -496,7 +496,7 @@ pub fn stdout() -> Stdout { unsafe { let ret = Arc::new(ReentrantMutex::new(RefCell::new(LineWriter::new(stdout)))); ret.init(); - return ret; + ret } } } @@ -664,7 +664,7 @@ pub fn stderr() -> Stderr { *INSTANCE.lock().borrow_mut() = Maybe::Real(stderr); } }); - return Stderr { inner: &INSTANCE }; + Stderr { inner: &INSTANCE } } impl Stderr { diff --git a/src/libstd/sys/unix/process/process_unix.rs b/src/libstd/sys/unix/process/process_unix.rs index 07d0fbf61fe22..f389c60615f24 100644 --- a/src/libstd/sys/unix/process/process_unix.rs +++ b/src/libstd/sys/unix/process/process_unix.rs @@ -72,7 +72,7 @@ impl Command { } }; - let mut p = Process { pid: pid, status: None }; + let mut p = Process { pid, status: None }; drop(output); let mut bytes = [0; 8];