Skip to content

Commit

Permalink
Auto merge of rust-lang#78825 - Nicholas-Baron:unwrap_or_corrected, r…
Browse files Browse the repository at this point in the history
…=lcnr

`unwrap_or` lint corrected

rust-lang#78814 (comment)

This pull request fixes the lint from clippy where `unwrap_or` could be better done as a `unwrap_or_else` or a `unwrap_or_default`.
  • Loading branch information
bors committed Nov 11, 2020
2 parents 38030ff + 261ca04 commit d4ea0b3
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 30 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl NestedMetaItem {
self.meta_item().and_then(|meta_item| meta_item.ident())
}
pub fn name_or_empty(&self) -> Symbol {
self.ident().unwrap_or(Ident::invalid()).name
self.ident().unwrap_or_else(Ident::invalid).name
}

/// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Attribute {
}
}
pub fn name_or_empty(&self) -> Symbol {
self.ident().unwrap_or(Ident::invalid()).name
self.ident().unwrap_or_else(Ident::invalid).name
}

pub fn value_str(&self) -> Option<Symbol> {
Expand Down Expand Up @@ -183,7 +183,7 @@ impl MetaItem {
if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None }
}
pub fn name_or_empty(&self) -> Symbol {
self.ident().unwrap_or(Ident::invalid()).name
self.ident().unwrap_or_else(Ident::invalid).name
}

// Example:
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
let mut test_runner = cx
.test_runner
.clone()
.unwrap_or(ecx.path(sp, vec![test_id, Ident::from_str_and_span(runner_name, sp)]));
.unwrap_or_else(|| ecx.path(sp, vec![test_id, Ident::from_str_and_span(runner_name, sp)]));

test_runner.span = sp;

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
config.opts.cg.panic = Some(PanicStrategy::Abort);
config.opts.debugging_opts.panic_abort_tests = true;
config.opts.maybe_sysroot = Some(
config.opts.maybe_sysroot.clone().unwrap_or(
std::env::current_exe()
config.opts.maybe_sysroot.clone().unwrap_or_else(
|| std::env::current_exe()
.unwrap()
.parent()
.unwrap()
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,12 +979,14 @@ fn generic_simd_intrinsic(

// Integer vector <i{in_bitwidth} x in_len>:
let (i_xn, in_elem_bitwidth) = match in_elem.kind() {
ty::Int(i) => {
(args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits()))
}
ty::Uint(i) => {
(args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits()))
}
ty::Int(i) => (
args[0].immediate(),
i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size.bits()),
),
ty::Uint(i) => (
args[0].immediate(),
i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size.bits()),
),
_ => return_error!(
"vector argument `{}`'s element type `{}`, expected integer element type",
in_ty,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl Definitions {
}

pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId {
self.expansions_that_defined.get(&id).copied().unwrap_or(ExpnId::root())
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
}

pub fn parent_module_of_macro_def(&self, expn_id: ExpnId) -> DefId {
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, index)
.unwrap_or(Lazy::empty())
.unwrap_or_else(Lazy::empty)
.decode(self)
.map(|index| ty::FieldDef {
did: self.local_def_id(index),
Expand Down Expand Up @@ -888,7 +888,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, item_id)
.unwrap_or(Lazy::empty())
.unwrap_or_else(Lazy::empty)
.decode(self)
.map(|index| self.get_variant(&self.kind(index), index, did, tcx.sess))
.collect()
Expand Down Expand Up @@ -1075,7 +1075,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

// Iterate over all children.
let macros_only = self.dep_kind.lock().macros_only();
let children = self.root.tables.children.get(self, id).unwrap_or(Lazy::empty());
let children = self.root.tables.children.get(self, id).unwrap_or_else(Lazy::empty);
for child_index in children.decode((self, sess)) {
if macros_only {
continue;
Expand All @@ -1098,7 +1098,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, child_index)
.unwrap_or(Lazy::empty());
.unwrap_or_else(Lazy::empty);
for child_index in child_children.decode((self, sess)) {
let kind = self.def_kind(child_index);
callback(Export {
Expand Down Expand Up @@ -1284,7 +1284,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

fn get_item_variances(&self, id: DefIndex) -> Vec<ty::Variance> {
self.root.tables.variances.get(self, id).unwrap_or(Lazy::empty()).decode(self).collect()
self.root.tables.variances.get(self, id).unwrap_or_else(Lazy::empty).decode(self).collect()
}

fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
Expand Down Expand Up @@ -1323,7 +1323,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.attributes
.get(self, item_id)
.unwrap_or(Lazy::empty())
.unwrap_or_else(Lazy::empty)
.decode((self, sess))
.collect::<Vec<_>>()
}
Expand All @@ -1333,7 +1333,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.children
.get(self, id)
.unwrap_or(Lazy::empty())
.unwrap_or_else(Lazy::empty)
.decode(self)
.map(|index| respan(self.get_span(index, sess), self.item_ident(index, sess).name))
.collect()
Expand All @@ -1349,7 +1349,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.tables
.inherent_impls
.get(self, id)
.unwrap_or(Lazy::empty())
.unwrap_or_else(Lazy::empty)
.decode(self)
.map(|index| self.local_def_id(index)),
)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/borrow_check/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
self.check_member_constraints(infcx, &mut errors_buffer);
}

let outlives_requirements = outlives_requirements.unwrap_or(vec![]);
let outlives_requirements = outlives_requirements.unwrap_or_default();

if outlives_requirements.is_empty() {
(None, errors_buffer)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ pub fn build_session(
early_error(sopts.error_format, &format!("Error loading host specification: {}", e))
});

let loader = file_loader.unwrap_or(Box::new(RealFileLoader));
let loader = file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
let hash_kind = sopts.debugging_opts.src_hash_algorithm.unwrap_or_else(|| {
if target_cfg.is_like_msvc {
SourceFileHashAlgorithm::Sha1
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
/// tracking is not enabled, just returns an empty vector.
pub fn take_intercrate_ambiguity_causes(&mut self) -> Vec<IntercrateAmbiguityCause> {
assert!(self.intercrate);
self.intercrate_ambiguity_causes.take().unwrap_or(vec![])
self.intercrate_ambiguity_causes.take().unwrap_or_default()
}

pub fn infcx(&self) -> &'cx InferCtxt<'cx, 'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
closure_def_id: DefId,
) -> Vec<DeferredCallResolution<'tcx>> {
let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
deferred_call_resolutions.remove(&closure_def_id).unwrap_or(vec![])
deferred_call_resolutions.remove(&closure_def_id).unwrap_or_default()
}

pub fn tag(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ProbeScope::AllTraits,
|probe_cx| Ok(probe_cx.candidate_method_names()),
)
.unwrap_or(vec![]);
.unwrap_or_default();
method_names
.iter()
.flat_map(|&method_name| {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl Instant {
/// ```
#[stable(feature = "checked_duration_since", since = "1.39.0")]
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.checked_duration_since(earlier).unwrap_or(Duration::new(0, 0))
self.checked_duration_since(earlier).unwrap_or_default()
}

/// Returns the amount of time elapsed since this instant was created.
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,7 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
.iter()
.enumerate()
.map(|(i, ty)| {
let mut name =
self.1.get(i).map(|ident| ident.to_string()).unwrap_or(String::new());
let mut name = self.1.get(i).map(|ident| ident.to_string()).unwrap_or_default();
if name.is_empty() {
name = "_".to_string();
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
.module
.as_ref()
.map(|module| shorten(plain_text_summary(module.doc_value())))
.unwrap_or(String::new());
.unwrap_or_default();

#[derive(Serialize)]
struct CrateData<'a> {
Expand Down

0 comments on commit d4ea0b3

Please sign in to comment.