Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0708373

Browse files
authoredDec 30, 2020
Rollup merge of #80492 - matthiaskrgr:tasty_wraps, r=varkor
remove empty wraps, don't return Results from from infallible functions This makes code easier to understand because it is more obvious when a function actually can't fail (return Err or None) Make functions that only ever return Some(x), return x directly Remove return type from functions that return Option<(), Err> but would only ever return Ok(()). Found with `clippy::unnecessary_wraps`
2 parents 7494aef + e5ead5f commit 0708373

File tree

12 files changed

+56
-81
lines changed

12 files changed

+56
-81
lines changed
 

‎compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ pub(crate) unsafe fn optimize(
485485
diag_handler: &Handler,
486486
module: &ModuleCodegen<ModuleLlvm>,
487487
config: &ModuleConfig,
488-
) -> Result<(), FatalError> {
488+
) {
489489
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &module.name[..]);
490490

491491
let llmod = module.module_llvm.llmod();
@@ -511,7 +511,7 @@ pub(crate) unsafe fn optimize(
511511
_ => llvm::OptStage::PreLinkNoLTO,
512512
};
513513
optimize_with_new_llvm_pass_manager(cgcx, module, config, opt_level, opt_stage);
514-
return Ok(());
514+
return;
515515
}
516516

517517
if cgcx.prof.llvm_recording_enabled() {
@@ -634,7 +634,6 @@ pub(crate) unsafe fn optimize(
634634
llvm::LLVMDisposePassManager(fpm);
635635
llvm::LLVMDisposePassManager(mpm);
636636
}
637-
Ok(())
638637
}
639638

640639
unsafe fn add_sanitizer_passes(config: &ModuleConfig, passes: &mut Vec<&'static mut llvm::Pass>) {

‎compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,13 +2322,13 @@ fn set_members_of_composite_type(
23222322
DIB(cx),
23232323
composite_type_metadata,
23242324
Some(type_array),
2325-
type_params,
2325+
Some(type_params),
23262326
);
23272327
}
23282328
}
23292329

23302330
/// Computes the type parameters for a type, if any, for the given metadata.
2331-
fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&'ll DIArray> {
2331+
fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> &'ll DIArray {
23322332
if let ty::Adt(def, substs) = *ty.kind() {
23332333
if substs.types().next().is_some() {
23342334
let generics = cx.tcx.generics_of(def.did);
@@ -2358,10 +2358,10 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> Option<&'
23582358
})
23592359
.collect();
23602360

2361-
return Some(create_DIArray(DIB(cx), &template_params[..]));
2361+
return create_DIArray(DIB(cx), &template_params[..]);
23622362
}
23632363
}
2364-
return Some(create_DIArray(DIB(cx), &[]));
2364+
return create_DIArray(DIB(cx), &[]);
23652365

23662366
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
23672367
let mut names = generics

‎compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
160160
module: &ModuleCodegen<Self::Module>,
161161
config: &ModuleConfig,
162162
) -> Result<(), FatalError> {
163-
back::write::optimize(cgcx, diag_handler, module, config)
163+
Ok(back::write::optimize(cgcx, diag_handler, module, config))
164164
}
165165
unsafe fn optimize_thin(
166166
cgcx: &CodegenContext<Self>,

‎compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ impl<'a> Resolver<'a> {
185185

186186
crate fn get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
187187
match res {
188-
Res::Def(DefKind::Macro(..), def_id) => self.get_macro_by_def_id(def_id),
188+
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
189189
Res::NonMacroAttr(attr_kind) => Some(self.non_macro_attr(attr_kind.is_used())),
190190
_ => None,
191191
}
192192
}
193193

194-
crate fn get_macro_by_def_id(&mut self, def_id: DefId) -> Option<Lrc<SyntaxExtension>> {
194+
crate fn get_macro_by_def_id(&mut self, def_id: DefId) -> Lrc<SyntaxExtension> {
195195
if let Some(ext) = self.macro_map.get(&def_id) {
196-
return Some(ext.clone());
196+
return ext.clone();
197197
}
198198

199199
let ext = Lrc::new(match self.cstore().load_macro_untracked(def_id, &self.session) {
@@ -202,7 +202,7 @@ impl<'a> Resolver<'a> {
202202
});
203203

204204
self.macro_map.insert(def_id, ext.clone());
205-
Some(ext)
205+
ext
206206
}
207207

208208
crate fn build_reduced_graph(

‎compiler/rustc_resolve/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,14 +1991,13 @@ impl<'a> Resolver<'a> {
19911991
{
19921992
// The macro is a proc macro derive
19931993
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
1994-
if let Some(ext) = self.get_macro_by_def_id(def_id) {
1995-
if !ext.is_builtin
1996-
&& ext.macro_kind() == MacroKind::Derive
1997-
&& parent.expansion.outer_expn_is_descendant_of(span.ctxt())
1998-
{
1999-
*poisoned = Some(node_id);
2000-
return module.parent;
2001-
}
1994+
let ext = self.get_macro_by_def_id(def_id);
1995+
if !ext.is_builtin
1996+
&& ext.macro_kind() == MacroKind::Derive
1997+
&& parent.expansion.outer_expn_is_descendant_of(span.ctxt())
1998+
{
1999+
*poisoned = Some(node_id);
2000+
return module.parent;
20022001
}
20032002
}
20042003
}

‎compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
247247

248248
let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
249249

250-
self.assemble_candidates_for_trait_alias(obligation, &mut candidates)?;
250+
self.assemble_candidates_for_trait_alias(obligation, &mut candidates);
251251

252252
// Other bounds. Consider both in-scope bounds from fn decl
253253
// and applicable impls. There is a certain set of precedence rules here.
@@ -259,19 +259,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
259259

260260
// User-defined copy impls are permitted, but only for
261261
// structs and enums.
262-
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
262+
self.assemble_candidates_from_impls(obligation, &mut candidates);
263263

264264
// For other types, we'll use the builtin rules.
265265
let copy_conditions = self.copy_clone_conditions(obligation);
266-
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates)?;
266+
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates);
267267
} else if lang_items.discriminant_kind_trait() == Some(def_id) {
268268
// `DiscriminantKind` is automatically implemented for every type.
269269
candidates.vec.push(DiscriminantKindCandidate);
270270
} else if lang_items.sized_trait() == Some(def_id) {
271271
// Sized is never implementable by end-users, it is
272272
// always automatically computed.
273273
let sized_conditions = self.sized_conditions(obligation);
274-
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates)?;
274+
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
275275
} else if lang_items.unsize_trait() == Some(def_id) {
276276
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
277277
} else {
@@ -280,13 +280,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
280280
// for `Copy` also has builtin support for `Clone`, and tuples/arrays of `Clone`
281281
// types have builtin support for `Clone`.
282282
let clone_conditions = self.copy_clone_conditions(obligation);
283-
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates)?;
283+
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates);
284284
}
285285

286-
self.assemble_generator_candidates(obligation, &mut candidates)?;
287-
self.assemble_closure_candidates(obligation, &mut candidates)?;
288-
self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
289-
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
286+
self.assemble_generator_candidates(obligation, &mut candidates);
287+
self.assemble_closure_candidates(obligation, &mut candidates);
288+
self.assemble_fn_pointer_candidates(obligation, &mut candidates);
289+
self.assemble_candidates_from_impls(obligation, &mut candidates);
290290
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
291291
}
292292

@@ -295,7 +295,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
295295
// Auto implementations have lower priority, so we only
296296
// consider triggering a default if there is no other impl that can apply.
297297
if candidates.vec.is_empty() {
298-
self.assemble_candidates_from_auto_impls(obligation, &mut candidates)?;
298+
self.assemble_candidates_from_auto_impls(obligation, &mut candidates);
299299
}
300300
debug!("candidate list size: {}", candidates.vec.len());
301301
Ok(candidates)
@@ -367,9 +367,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
367367
&mut self,
368368
obligation: &TraitObligation<'tcx>,
369369
candidates: &mut SelectionCandidateSet<'tcx>,
370-
) -> Result<(), SelectionError<'tcx>> {
370+
) {
371371
if self.tcx().lang_items().gen_trait() != Some(obligation.predicate.def_id()) {
372-
return Ok(());
372+
return;
373373
}
374374

375375
// Okay to skip binder because the substs on generator types never
@@ -388,8 +388,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
388388
}
389389
_ => {}
390390
}
391-
392-
Ok(())
393391
}
394392

395393
/// Checks for the artificial impl that the compiler will create for an obligation like `X :
@@ -402,11 +400,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
402400
&mut self,
403401
obligation: &TraitObligation<'tcx>,
404402
candidates: &mut SelectionCandidateSet<'tcx>,
405-
) -> Result<(), SelectionError<'tcx>> {
403+
) {
406404
let kind = match self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) {
407405
Some(k) => k,
408406
None => {
409-
return Ok(());
407+
return;
410408
}
411409
};
412410

@@ -435,19 +433,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
435433
}
436434
_ => {}
437435
}
438-
439-
Ok(())
440436
}
441437

442438
/// Implements one of the `Fn()` family for a fn pointer.
443439
fn assemble_fn_pointer_candidates(
444440
&mut self,
445441
obligation: &TraitObligation<'tcx>,
446442
candidates: &mut SelectionCandidateSet<'tcx>,
447-
) -> Result<(), SelectionError<'tcx>> {
443+
) {
448444
// We provide impl of all fn traits for fn pointers.
449445
if self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()).is_none() {
450-
return Ok(());
446+
return;
451447
}
452448

453449
// Okay to skip binder because what we are inspecting doesn't involve bound regions.
@@ -485,16 +481,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
485481
}
486482
_ => {}
487483
}
488-
489-
Ok(())
490484
}
491485

492486
/// Searches for impls that might apply to `obligation`.
493487
fn assemble_candidates_from_impls(
494488
&mut self,
495489
obligation: &TraitObligation<'tcx>,
496490
candidates: &mut SelectionCandidateSet<'tcx>,
497-
) -> Result<(), SelectionError<'tcx>> {
491+
) {
498492
debug!(?obligation, "assemble_candidates_from_impls");
499493

500494
// Essentially any user-written impl will match with an error type,
@@ -504,7 +498,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
504498
// Since compilation is already guaranteed to fail, this is just
505499
// to try to show the 'nicest' possible errors to the user.
506500
if obligation.references_error() {
507-
return Ok(());
501+
return;
508502
}
509503

510504
self.tcx().for_each_relevant_impl(
@@ -518,15 +512,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
518512
});
519513
},
520514
);
521-
522-
Ok(())
523515
}
524516

525517
fn assemble_candidates_from_auto_impls(
526518
&mut self,
527519
obligation: &TraitObligation<'tcx>,
528520
candidates: &mut SelectionCandidateSet<'tcx>,
529-
) -> Result<(), SelectionError<'tcx>> {
521+
) {
530522
// Okay to skip binder here because the tests we do below do not involve bound regions.
531523
let self_ty = obligation.self_ty().skip_binder();
532524
debug!(?self_ty, "assemble_candidates_from_auto_impls");
@@ -585,8 +577,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
585577
_ => candidates.vec.push(AutoImplCandidate(def_id)),
586578
}
587579
}
588-
589-
Ok(())
590580
}
591581

592582
/// Searches for impls that might apply to `obligation`.
@@ -753,7 +743,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
753743
&mut self,
754744
obligation: &TraitObligation<'tcx>,
755745
candidates: &mut SelectionCandidateSet<'tcx>,
756-
) -> Result<(), SelectionError<'tcx>> {
746+
) {
757747
// Okay to skip binder here because the tests we do below do not involve bound regions.
758748
let self_ty = obligation.self_ty().skip_binder();
759749
debug!(?self_ty, "assemble_candidates_for_trait_alias");
@@ -763,8 +753,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
763753
if self.tcx().is_trait_alias(def_id) {
764754
candidates.vec.push(TraitAliasCandidate(def_id));
765755
}
766-
767-
Ok(())
768756
}
769757

770758
/// Assembles the trait which are built-in to the language itself:
@@ -773,7 +761,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
773761
&mut self,
774762
conditions: BuiltinImplConditions<'tcx>,
775763
candidates: &mut SelectionCandidateSet<'tcx>,
776-
) -> Result<(), SelectionError<'tcx>> {
764+
) {
777765
match conditions {
778766
BuiltinImplConditions::Where(nested) => {
779767
debug!(?nested, "builtin_bound");
@@ -787,7 +775,5 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
787775
candidates.ambiguous = true;
788776
}
789777
}
790-
791-
Ok(())
792778
}
793779
}

‎compiler/rustc_typeck/src/check/dropck.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,13 @@ crate fn check_drop_obligations<'a, 'tcx>(
267267
ty: Ty<'tcx>,
268268
span: Span,
269269
body_id: hir::HirId,
270-
) -> Result<(), ErrorReported> {
270+
) {
271271
debug!("check_drop_obligations typ: {:?}", ty);
272272

273273
let cause = &ObligationCause::misc(span, body_id);
274274
let infer_ok = rcx.infcx.at(cause, rcx.fcx.param_env).dropck_outlives(ty);
275275
debug!("dropck_outlives = {:#?}", infer_ok);
276276
rcx.fcx.register_infer_ok_obligations(infer_ok);
277-
278-
Ok(())
279277
}
280278

281279
// This is an implementation of the TypeRelation trait with the

‎compiler/rustc_typeck/src/check/method/probe.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
423423
probe_cx.assemble_inherent_candidates();
424424
match scope {
425425
ProbeScope::TraitsInScope => {
426-
probe_cx.assemble_extension_candidates_for_traits_in_scope(scope_expr_id)?
426+
probe_cx.assemble_extension_candidates_for_traits_in_scope(scope_expr_id)
427427
}
428-
ProbeScope::AllTraits => probe_cx.assemble_extension_candidates_for_all_traits()?,
428+
ProbeScope::AllTraits => probe_cx.assemble_extension_candidates_for_all_traits(),
429429
};
430430
op(probe_cx)
431431
})
@@ -866,35 +866,29 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
866866
}
867867
}
868868

869-
fn assemble_extension_candidates_for_traits_in_scope(
870-
&mut self,
871-
expr_hir_id: hir::HirId,
872-
) -> Result<(), MethodError<'tcx>> {
869+
fn assemble_extension_candidates_for_traits_in_scope(&mut self, expr_hir_id: hir::HirId) {
873870
let mut duplicates = FxHashSet::default();
874871
let opt_applicable_traits = self.tcx.in_scope_traits(expr_hir_id);
875872
if let Some(applicable_traits) = opt_applicable_traits {
876873
for trait_candidate in applicable_traits.iter() {
877874
let trait_did = trait_candidate.def_id;
878875
if duplicates.insert(trait_did) {
879-
let result = self.assemble_extension_candidates_for_trait(
876+
self.assemble_extension_candidates_for_trait(
880877
&trait_candidate.import_ids,
881878
trait_did,
882879
);
883-
result?;
884880
}
885881
}
886882
}
887-
Ok(())
888883
}
889884

890-
fn assemble_extension_candidates_for_all_traits(&mut self) -> Result<(), MethodError<'tcx>> {
885+
fn assemble_extension_candidates_for_all_traits(&mut self) {
891886
let mut duplicates = FxHashSet::default();
892887
for trait_info in suggest::all_traits(self.tcx) {
893888
if duplicates.insert(trait_info.def_id) {
894-
self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id)?;
889+
self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id);
895890
}
896891
}
897-
Ok(())
898892
}
899893

900894
pub fn matches_return_type(
@@ -932,7 +926,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
932926
&mut self,
933927
import_ids: &SmallVec<[LocalDefId; 1]>,
934928
trait_def_id: DefId,
935-
) -> Result<(), MethodError<'tcx>> {
929+
) {
936930
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
937931
let trait_substs = self.fresh_item_substs(trait_def_id);
938932
let trait_ref = ty::TraitRef::new(trait_def_id, trait_substs);
@@ -980,7 +974,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
980974
);
981975
}
982976
}
983-
Ok(())
984977
}
985978

986979
fn candidate_method_names(&self) -> Vec<Ident> {
@@ -1027,7 +1020,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
10271020
let span = self.span;
10281021
let tcx = self.tcx;
10291022

1030-
self.assemble_extension_candidates_for_all_traits()?;
1023+
self.assemble_extension_candidates_for_all_traits();
10311024

10321025
let out_of_scope_traits = match self.pick_core() {
10331026
Some(Ok(p)) => vec![p.item.container.id()],

‎compiler/rustc_typeck/src/check/regionck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
325325
pat.each_binding(|_, hir_id, span, _| {
326326
let typ = self.resolve_node_type(hir_id);
327327
let body_id = self.body_id;
328-
let _ = dropck::check_drop_obligations(self, typ, span, body_id);
328+
dropck::check_drop_obligations(self, typ, span, body_id);
329329
})
330330
}
331331
}
@@ -488,7 +488,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
488488
if place_with_id.place.projections.is_empty() {
489489
let typ = self.resolve_type(place_with_id.place.ty());
490490
let body_id = self.body_id;
491-
let _ = dropck::check_drop_obligations(self, typ, span, body_id);
491+
dropck::check_drop_obligations(self, typ, span, body_id);
492492
}
493493
}
494494
}

‎src/librustdoc/fold.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use crate::clean::*;
33
crate struct StripItem(pub Item);
44

55
impl StripItem {
6-
crate fn strip(self) -> Option<Item> {
6+
crate fn strip(self) -> Item {
77
match self.0 {
8-
Item { kind: box StrippedItem(..), .. } => Some(self.0),
8+
Item { kind: box StrippedItem(..), .. } => self.0,
99
mut i => {
1010
i.kind = box StrippedItem(i.kind);
11-
Some(i)
11+
i
1212
}
1313
}
1414
}

‎src/librustdoc/passes/strip_hidden.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a> DocFolder for Stripper<'a> {
4949
let old = mem::replace(&mut self.update_retained, false);
5050
let ret = StripItem(self.fold_item_recur(i)).strip();
5151
self.update_retained = old;
52-
return ret;
52+
return Some(ret);
5353
}
5454
_ => return None,
5555
}

‎src/librustdoc/passes/stripper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a> DocFolder for Stripper<'a> {
5151

5252
clean::StructFieldItem(..) => {
5353
if !i.visibility.is_public() {
54-
return StripItem(i).strip();
54+
return Some(StripItem(i).strip());
5555
}
5656
}
5757

@@ -61,7 +61,7 @@ impl<'a> DocFolder for Stripper<'a> {
6161
let old = mem::replace(&mut self.update_retained, false);
6262
let ret = StripItem(self.fold_item_recur(i)).strip();
6363
self.update_retained = old;
64-
return ret;
64+
return Some(ret);
6565
}
6666
}
6767

0 commit comments

Comments
 (0)
Please sign in to comment.