From e5ead5fc5875ec2e32f001f25ec0007cd5d527fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Tue, 29 Dec 2020 22:24:15 +0100 Subject: [PATCH] remove unused return types such as empty Results or Options that would always be Some(..) remove unused return type of dropck::check_drop_obligations() don't wrap return type in Option in get_macro_by_def_id() since we would always return Some(..) remove redundant return type of back::write::optimize() don't Option-wrap return type of compute_type_parameters() since we always return Some(..) don't return empty Result in assemble_generator_candidates() don't return empty Result in assemble_closure_candidates() don't return empty result in assemble_fn_pointer_candidates() don't return empty result in assemble_candidates_from_impls() don't return empty result in assemble_candidates_from_auto_impls() don't return emtpy result in assemble_candidates_for_trait_alias() don't return empty result in assemble_builtin_bound_candidates() don't return empty results in assemble_extension_candidates_for_traits_in_scope() and assemble_extension_candidates_for_trait() remove redundant wrapping of return type of StripItem::strip() since it always returns Some(..) remove unused return type of assemble_extension_candidates_for_all_traits() --- compiler/rustc_codegen_llvm/src/back/write.rs | 5 +- .../src/debuginfo/metadata.rs | 8 +-- compiler/rustc_codegen_llvm/src/lib.rs | 2 +- .../rustc_resolve/src/build_reduced_graph.rs | 8 +-- compiler/rustc_resolve/src/lib.rs | 15 +++-- .../src/traits/select/candidate_assembly.rs | 56 +++++++------------ compiler/rustc_typeck/src/check/dropck.rs | 4 +- .../rustc_typeck/src/check/method/probe.rs | 23 +++----- compiler/rustc_typeck/src/check/regionck.rs | 4 +- src/librustdoc/fold.rs | 6 +- src/librustdoc/passes/strip_hidden.rs | 2 +- src/librustdoc/passes/stripper.rs | 4 +- 12 files changed, 56 insertions(+), 81 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 3fda1e26dae..230e11f274e 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -485,7 +485,7 @@ pub(crate) unsafe fn optimize( diag_handler: &Handler, module: &ModuleCodegen, config: &ModuleConfig, -) -> Result<(), FatalError> { +) { let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &module.name[..]); let llmod = module.module_llvm.llmod(); @@ -511,7 +511,7 @@ pub(crate) unsafe fn optimize( _ => llvm::OptStage::PreLinkNoLTO, }; optimize_with_new_llvm_pass_manager(cgcx, module, config, opt_level, opt_stage); - return Ok(()); + return; } if cgcx.prof.llvm_recording_enabled() { @@ -634,7 +634,6 @@ pub(crate) unsafe fn optimize( llvm::LLVMDisposePassManager(fpm); llvm::LLVMDisposePassManager(mpm); } - Ok(()) } unsafe fn add_sanitizer_passes(config: &ModuleConfig, passes: &mut Vec<&'static mut llvm::Pass>) { diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index fa285f3488f..36a21b38c03 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -2322,13 +2322,13 @@ fn set_members_of_composite_type( DIB(cx), composite_type_metadata, Some(type_array), - type_params, + Some(type_params), ); } } /// Computes the type parameters for a type, if any, for the given metadata. -fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&'ll DIArray> { +fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> &'ll DIArray { if let ty::Adt(def, substs) = *ty.kind() { if substs.types().next().is_some() { let generics = cx.tcx.generics_of(def.did); @@ -2358,10 +2358,10 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&' }) .collect(); - return Some(create_DIArray(DIB(cx), &template_params[..])); + return create_DIArray(DIB(cx), &template_params[..]); } } - return Some(create_DIArray(DIB(cx), &[])); + return create_DIArray(DIB(cx), &[]); fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec { let mut names = generics diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index f33464f83da..92ac770aca5 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -160,7 +160,7 @@ unsafe fn optimize( module: &ModuleCodegen, config: &ModuleConfig, ) -> Result<(), FatalError> { - back::write::optimize(cgcx, diag_handler, module, config) + Ok(back::write::optimize(cgcx, diag_handler, module, config)) } unsafe fn optimize_thin( cgcx: &CodegenContext, diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index a5adfb27e93..907a08cee2a 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -185,15 +185,15 @@ fn nearest_mod_parent(&mut self, def_id: DefId) -> Module<'a> { crate fn get_macro(&mut self, res: Res) -> Option> { match res { - Res::Def(DefKind::Macro(..), def_id) => self.get_macro_by_def_id(def_id), + Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)), Res::NonMacroAttr(attr_kind) => Some(self.non_macro_attr(attr_kind.is_used())), _ => None, } } - crate fn get_macro_by_def_id(&mut self, def_id: DefId) -> Option> { + crate fn get_macro_by_def_id(&mut self, def_id: DefId) -> Lrc { if let Some(ext) = self.macro_map.get(&def_id) { - return Some(ext.clone()); + return ext.clone(); } let ext = Lrc::new(match self.cstore().load_macro_untracked(def_id, &self.session) { @@ -202,7 +202,7 @@ fn nearest_mod_parent(&mut self, def_id: DefId) -> Module<'a> { }); self.macro_map.insert(def_id, ext.clone()); - Some(ext) + ext } crate fn build_reduced_graph( diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 1f4bd00c3e2..5eb30eacf07 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1991,14 +1991,13 @@ fn hygienic_lexical_parent_with_compatibility_fallback( { // The macro is a proc macro derive if let Some(def_id) = module.expansion.expn_data().macro_def_id { - if let Some(ext) = self.get_macro_by_def_id(def_id) { - if !ext.is_builtin - && ext.macro_kind() == MacroKind::Derive - && parent.expansion.outer_expn_is_descendant_of(span.ctxt()) - { - *poisoned = Some(node_id); - return module.parent; - } + let ext = self.get_macro_by_def_id(def_id); + if !ext.is_builtin + && ext.macro_kind() == MacroKind::Derive + && parent.expansion.outer_expn_is_descendant_of(span.ctxt()) + { + *poisoned = Some(node_id); + return module.parent; } } } diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 12029f7bc75..f09ce8d64ed 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -247,7 +247,7 @@ pub(super) fn assemble_candidates<'o>( let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false }; - self.assemble_candidates_for_trait_alias(obligation, &mut candidates)?; + self.assemble_candidates_for_trait_alias(obligation, &mut candidates); // Other bounds. Consider both in-scope bounds from fn decl // and applicable impls. There is a certain set of precedence rules here. @@ -259,11 +259,11 @@ pub(super) fn assemble_candidates<'o>( // User-defined copy impls are permitted, but only for // structs and enums. - self.assemble_candidates_from_impls(obligation, &mut candidates)?; + self.assemble_candidates_from_impls(obligation, &mut candidates); // For other types, we'll use the builtin rules. let copy_conditions = self.copy_clone_conditions(obligation); - self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates)?; + self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates); } else if lang_items.discriminant_kind_trait() == Some(def_id) { // `DiscriminantKind` is automatically implemented for every type. candidates.vec.push(DiscriminantKindCandidate); @@ -271,7 +271,7 @@ pub(super) fn assemble_candidates<'o>( // Sized is never implementable by end-users, it is // always automatically computed. let sized_conditions = self.sized_conditions(obligation); - self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates)?; + self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates); } else if lang_items.unsize_trait() == Some(def_id) { self.assemble_candidates_for_unsizing(obligation, &mut candidates); } else { @@ -280,13 +280,13 @@ pub(super) fn assemble_candidates<'o>( // for `Copy` also has builtin support for `Clone`, and tuples/arrays of `Clone` // types have builtin support for `Clone`. let clone_conditions = self.copy_clone_conditions(obligation); - self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates)?; + self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates); } - self.assemble_generator_candidates(obligation, &mut candidates)?; - self.assemble_closure_candidates(obligation, &mut candidates)?; - self.assemble_fn_pointer_candidates(obligation, &mut candidates)?; - self.assemble_candidates_from_impls(obligation, &mut candidates)?; + self.assemble_generator_candidates(obligation, &mut candidates); + self.assemble_closure_candidates(obligation, &mut candidates); + self.assemble_fn_pointer_candidates(obligation, &mut candidates); + self.assemble_candidates_from_impls(obligation, &mut candidates); self.assemble_candidates_from_object_ty(obligation, &mut candidates); } @@ -295,7 +295,7 @@ pub(super) fn assemble_candidates<'o>( // Auto implementations have lower priority, so we only // consider triggering a default if there is no other impl that can apply. if candidates.vec.is_empty() { - self.assemble_candidates_from_auto_impls(obligation, &mut candidates)?; + self.assemble_candidates_from_auto_impls(obligation, &mut candidates); } debug!("candidate list size: {}", candidates.vec.len()); Ok(candidates) @@ -367,9 +367,9 @@ fn assemble_generator_candidates( &mut self, obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { if self.tcx().lang_items().gen_trait() != Some(obligation.predicate.def_id()) { - return Ok(()); + return; } // Okay to skip binder because the substs on generator types never @@ -388,8 +388,6 @@ fn assemble_generator_candidates( } _ => {} } - - Ok(()) } /// Checks for the artificial impl that the compiler will create for an obligation like `X : @@ -402,11 +400,11 @@ fn assemble_closure_candidates( &mut self, obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { let kind = match self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) { Some(k) => k, None => { - return Ok(()); + return; } }; @@ -435,8 +433,6 @@ fn assemble_closure_candidates( } _ => {} } - - Ok(()) } /// Implements one of the `Fn()` family for a fn pointer. @@ -444,10 +440,10 @@ fn assemble_fn_pointer_candidates( &mut self, obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { // We provide impl of all fn traits for fn pointers. if self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()).is_none() { - return Ok(()); + return; } // Okay to skip binder because what we are inspecting doesn't involve bound regions. @@ -485,8 +481,6 @@ fn assemble_fn_pointer_candidates( } _ => {} } - - Ok(()) } /// Searches for impls that might apply to `obligation`. @@ -494,7 +488,7 @@ fn assemble_candidates_from_impls( &mut self, obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { debug!(?obligation, "assemble_candidates_from_impls"); // Essentially any user-written impl will match with an error type, @@ -504,7 +498,7 @@ fn assemble_candidates_from_impls( // Since compilation is already guaranteed to fail, this is just // to try to show the 'nicest' possible errors to the user. if obligation.references_error() { - return Ok(()); + return; } self.tcx().for_each_relevant_impl( @@ -518,15 +512,13 @@ fn assemble_candidates_from_impls( }); }, ); - - Ok(()) } fn assemble_candidates_from_auto_impls( &mut self, obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { // Okay to skip binder here because the tests we do below do not involve bound regions. let self_ty = obligation.self_ty().skip_binder(); debug!(?self_ty, "assemble_candidates_from_auto_impls"); @@ -585,8 +577,6 @@ fn assemble_candidates_from_auto_impls( _ => candidates.vec.push(AutoImplCandidate(def_id)), } } - - Ok(()) } /// Searches for impls that might apply to `obligation`. @@ -753,7 +743,7 @@ fn assemble_candidates_for_trait_alias( &mut self, obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { // Okay to skip binder here because the tests we do below do not involve bound regions. let self_ty = obligation.self_ty().skip_binder(); debug!(?self_ty, "assemble_candidates_for_trait_alias"); @@ -763,8 +753,6 @@ fn assemble_candidates_for_trait_alias( if self.tcx().is_trait_alias(def_id) { candidates.vec.push(TraitAliasCandidate(def_id)); } - - Ok(()) } /// Assembles the trait which are built-in to the language itself: @@ -773,7 +761,7 @@ fn assemble_builtin_bound_candidates( &mut self, conditions: BuiltinImplConditions<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, - ) -> Result<(), SelectionError<'tcx>> { + ) { match conditions { BuiltinImplConditions::Where(nested) => { debug!(?nested, "builtin_bound"); @@ -787,7 +775,5 @@ fn assemble_builtin_bound_candidates( candidates.ambiguous = true; } } - - Ok(()) } } diff --git a/compiler/rustc_typeck/src/check/dropck.rs b/compiler/rustc_typeck/src/check/dropck.rs index ad675f1e383..be19919c0ea 100644 --- a/compiler/rustc_typeck/src/check/dropck.rs +++ b/compiler/rustc_typeck/src/check/dropck.rs @@ -267,15 +267,13 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( ty: Ty<'tcx>, span: Span, body_id: hir::HirId, -) -> Result<(), ErrorReported> { +) { debug!("check_drop_obligations typ: {:?}", ty); let cause = &ObligationCause::misc(span, body_id); let infer_ok = rcx.infcx.at(cause, rcx.fcx.param_env).dropck_outlives(ty); debug!("dropck_outlives = {:#?}", infer_ok); rcx.fcx.register_infer_ok_obligations(infer_ok); - - Ok(()) } // This is an implementation of the TypeRelation trait with the diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index 891dd8b2f02..d4631c465a3 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -423,9 +423,9 @@ fn probe_op( probe_cx.assemble_inherent_candidates(); match scope { ProbeScope::TraitsInScope => { - probe_cx.assemble_extension_candidates_for_traits_in_scope(scope_expr_id)? + probe_cx.assemble_extension_candidates_for_traits_in_scope(scope_expr_id) } - ProbeScope::AllTraits => probe_cx.assemble_extension_candidates_for_all_traits()?, + ProbeScope::AllTraits => probe_cx.assemble_extension_candidates_for_all_traits(), }; op(probe_cx) }) @@ -866,35 +866,29 @@ fn elaborate_bounds( } } - fn assemble_extension_candidates_for_traits_in_scope( - &mut self, - expr_hir_id: hir::HirId, - ) -> Result<(), MethodError<'tcx>> { + fn assemble_extension_candidates_for_traits_in_scope(&mut self, expr_hir_id: hir::HirId) { let mut duplicates = FxHashSet::default(); let opt_applicable_traits = self.tcx.in_scope_traits(expr_hir_id); if let Some(applicable_traits) = opt_applicable_traits { for trait_candidate in applicable_traits.iter() { let trait_did = trait_candidate.def_id; if duplicates.insert(trait_did) { - let result = self.assemble_extension_candidates_for_trait( + self.assemble_extension_candidates_for_trait( &trait_candidate.import_ids, trait_did, ); - result?; } } } - Ok(()) } - fn assemble_extension_candidates_for_all_traits(&mut self) -> Result<(), MethodError<'tcx>> { + fn assemble_extension_candidates_for_all_traits(&mut self) { let mut duplicates = FxHashSet::default(); for trait_info in suggest::all_traits(self.tcx) { if duplicates.insert(trait_info.def_id) { - self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id)?; + self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id); } } - Ok(()) } pub fn matches_return_type( @@ -932,7 +926,7 @@ fn assemble_extension_candidates_for_trait( &mut self, import_ids: &SmallVec<[LocalDefId; 1]>, trait_def_id: DefId, - ) -> Result<(), MethodError<'tcx>> { + ) { debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id); let trait_substs = self.fresh_item_substs(trait_def_id); let trait_ref = ty::TraitRef::new(trait_def_id, trait_substs); @@ -980,7 +974,6 @@ fn assemble_extension_candidates_for_trait( ); } } - Ok(()) } fn candidate_method_names(&self) -> Vec { @@ -1027,7 +1020,7 @@ fn pick(mut self) -> PickResult<'tcx> { let span = self.span; let tcx = self.tcx; - self.assemble_extension_candidates_for_all_traits()?; + self.assemble_extension_candidates_for_all_traits(); let out_of_scope_traits = match self.pick_core() { Some(Ok(p)) => vec![p.item.container.id()], diff --git a/compiler/rustc_typeck/src/check/regionck.rs b/compiler/rustc_typeck/src/check/regionck.rs index eca6ce1ecdb..88e8dd3cb12 100644 --- a/compiler/rustc_typeck/src/check/regionck.rs +++ b/compiler/rustc_typeck/src/check/regionck.rs @@ -325,7 +325,7 @@ fn constrain_bindings_in_pat(&mut self, pat: &hir::Pat<'_>) { pat.each_binding(|_, hir_id, span, _| { let typ = self.resolve_node_type(hir_id); let body_id = self.body_id; - let _ = dropck::check_drop_obligations(self, typ, span, body_id); + dropck::check_drop_obligations(self, typ, span, body_id); }) } } @@ -488,7 +488,7 @@ fn check_safety_of_rvalue_destructor_if_necessary( if place_with_id.place.projections.is_empty() { let typ = self.resolve_type(place_with_id.place.ty()); let body_id = self.body_id; - let _ = dropck::check_drop_obligations(self, typ, span, body_id); + dropck::check_drop_obligations(self, typ, span, body_id); } } } diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs index 343c6e779c1..c39cc3ca397 100644 --- a/src/librustdoc/fold.rs +++ b/src/librustdoc/fold.rs @@ -3,12 +3,12 @@ crate struct StripItem(pub Item); impl StripItem { - crate fn strip(self) -> Option { + crate fn strip(self) -> Item { match self.0 { - Item { kind: box StrippedItem(..), .. } => Some(self.0), + Item { kind: box StrippedItem(..), .. } => self.0, mut i => { i.kind = box StrippedItem(i.kind); - Some(i) + i } } } diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs index 5694ce27de8..01e3d0acaa8 100644 --- a/src/librustdoc/passes/strip_hidden.rs +++ b/src/librustdoc/passes/strip_hidden.rs @@ -49,7 +49,7 @@ fn fold_item(&mut self, i: Item) -> Option { let old = mem::replace(&mut self.update_retained, false); let ret = StripItem(self.fold_item_recur(i)).strip(); self.update_retained = old; - return ret; + return Some(ret); } _ => return None, } diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index f872e403ab0..a1924422f0e 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -51,7 +51,7 @@ fn fold_item(&mut self, i: Item) -> Option { clean::StructFieldItem(..) => { if !i.visibility.is_public() { - return StripItem(i).strip(); + return Some(StripItem(i).strip()); } } @@ -61,7 +61,7 @@ fn fold_item(&mut self, i: Item) -> Option { let old = mem::replace(&mut self.update_retained, false); let ret = StripItem(self.fold_item_recur(i)).strip(); self.update_retained = old; - return ret; + return Some(ret); } }