Avoid unused Option::map results

These are changes that would be needed if we add `#[must_use]` to
`Option::map`, per #71484.
This commit is contained in:
Josh Stone 2020-04-24 13:58:41 -07:00
parent 3360cc3a0e
commit 2325c20925
21 changed files with 111 additions and 64 deletions

View File

@ -98,7 +98,7 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
}
}
diagnostic.map(|d| {
if let Some(d) = diagnostic {
struct_span_err!(d, attr.span, E0633, "malformed `unwind` attribute input")
.span_label(attr.span, "invalid argument")
.span_suggestions(
@ -110,7 +110,7 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
Applicability::MachineApplicable,
)
.emit();
});
};
}
}
}

View File

@ -1172,10 +1172,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
// ignore derives so they remain unused
let (attr, after_derive) = self.classify_nonitem(&mut expr);
if attr.is_some() {
if let Some(ref attr_value) = attr {
// Collect the invoc regardless of whether or not attributes are permitted here
// expansion will eat the attribute so it won't error later.
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
self.cfg.maybe_emit_expr_attr_err(attr_value);
// AstFragmentKind::Expr requires the macro to emit an expression.
return self
@ -1322,8 +1322,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
// Ignore derives so they remain unused.
let (attr, after_derive) = self.classify_nonitem(&mut expr);
if attr.is_some() {
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
if let Some(ref attr_value) = attr {
self.cfg.maybe_emit_expr_attr_err(attr_value);
return self
.collect_attr(

View File

@ -246,7 +246,7 @@ impl DefPath {
let mut opt_delimiter = None;
for component in &self.data {
opt_delimiter.map(|d| s.push(d));
s.extend(opt_delimiter);
opt_delimiter = Some('-');
if component.disambiguator == 0 {
write!(s, "{}", component.data.as_symbol()).unwrap();

View File

@ -755,7 +755,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
},
ObligationCauseCode::IfExpression(box IfExpressionCause { then, outer, semicolon }) => {
err.span_label(then, "expected because of this");
outer.map(|sp| err.span_label(sp, "`if` and `else` have incompatible types"));
if let Some(sp) = outer {
err.span_label(sp, "`if` and `else` have incompatible types");
}
if let Some(sp) = semicolon {
err.span_suggestion_short(
sp,

View File

@ -101,9 +101,15 @@ fn dump_crates(cstore: &CStore) {
info!(" hash: {}", data.hash());
info!(" reqd: {:?}", data.dep_kind());
let CrateSource { dylib, rlib, rmeta } = data.source();
dylib.as_ref().map(|dl| info!(" dylib: {}", dl.0.display()));
rlib.as_ref().map(|rl| info!(" rlib: {}", rl.0.display()));
rmeta.as_ref().map(|rl| info!(" rmeta: {}", rl.0.display()));
if let Some(dylib) = dylib {
info!(" dylib: {}", dylib.0.display());
}
if let Some(rlib) = rlib {
info!(" rlib: {}", rlib.0.display());
}
if let Some(rmeta) = rmeta {
info!(" rmeta: {}", rmeta.0.display());
}
});
}

View File

@ -1388,7 +1388,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
// Insert a Shallow borrow of any places that is switched on.
fake_borrows.as_mut().map(|fb| fb.insert(match_place));
if let Some(fb) = fake_borrows {
fb.insert(match_place);
}
// perform the test, branching to one of N blocks. For each of
// those N possible outcomes, create a (initially empty)

View File

@ -1206,8 +1206,8 @@ pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, sess: &Pa
*sess.reached_eof.borrow_mut() |=
unclosed_delims.iter().any(|unmatched_delim| unmatched_delim.found_delim.is_none());
for unmatched in unclosed_delims.drain(..) {
make_unclosed_delims_error(unmatched, sess).map(|mut e| {
if let Some(mut e) = make_unclosed_delims_error(unmatched, sess) {
e.emit();
});
}
}
}

View File

@ -68,7 +68,9 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
self.with_context(LabeledBlock, |v| v.visit_block(&b));
}
hir::ExprKind::Break(label, ref opt_expr) => {
opt_expr.as_ref().map(|e| self.visit_expr(e));
if let Some(e) = opt_expr {
self.visit_expr(e);
}
if self.require_label_in_labeled_block(e.span, &label, "break") {
// If we emitted an error about an unlabeled break in a labeled

View File

@ -133,7 +133,11 @@ impl<CTX: QueryContext> QueryJob<CTX> {
/// as there are no concurrent jobs which could be waiting on us
pub fn signal_complete(self) {
#[cfg(parallel_compiler)]
self.latch.map(|latch| latch.set());
{
if let Some(latch) = self.latch {
latch.set();
}
}
}
}

View File

@ -1996,7 +1996,9 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
this.visit_expr(cond);
this.visit_block(then);
});
opt_else.as_ref().map(|expr| self.visit_expr(expr));
if let Some(expr) = opt_else {
self.visit_expr(expr);
}
}
ExprKind::Loop(ref block, label) => self.resolve_labeled_block(label, expr.id, &block),

View File

@ -499,7 +499,9 @@ impl<'a> ModuleData<'a> {
F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>),
{
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
name_resolution.borrow().binding.map(|binding| f(resolver, key.ident, key.ns, binding));
if let Some(binding) = name_resolution.borrow().binding {
f(resolver, key.ident, key.ns, binding);
}
}
}

View File

@ -979,20 +979,21 @@ impl Target {
macro_rules! key {
($key_name:ident) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..]).map(|o| o.as_string()
.map(|s| base.options.$key_name = s.to_string()));
if let Some(s) = obj.find(&name).and_then(Json::as_string) {
base.options.$key_name = s.to_string();
}
} );
($key_name:ident, bool) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..])
.map(|o| o.as_boolean()
.map(|s| base.options.$key_name = s));
if let Some(s) = obj.find(&name).and_then(Json::as_boolean) {
base.options.$key_name = s;
}
} );
($key_name:ident, Option<u64>) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..])
.map(|o| o.as_u64()
.map(|s| base.options.$key_name = Some(s)));
if let Some(s) = obj.find(&name).and_then(Json::as_u64) {
base.options.$key_name = Some(s);
}
} );
($key_name:ident, MergeFunctions) => ( {
let name = (stringify!($key_name)).replace("_", "-");
@ -1034,19 +1035,19 @@ impl Target {
} );
($key_name:ident, list) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..]).map(|o| o.as_array()
.map(|v| base.options.$key_name = v.iter()
.map(|a| a.as_string().unwrap().to_string()).collect()
)
);
if let Some(v) = obj.find(&name).and_then(Json::as_array) {
base.options.$key_name = v.iter()
.map(|a| a.as_string().unwrap().to_string())
.collect();
}
} );
($key_name:ident, opt_list) => ( {
let name = (stringify!($key_name)).replace("_", "-");
obj.find(&name[..]).map(|o| o.as_array()
.map(|v| base.options.$key_name = Some(v.iter()
.map(|a| a.as_string().unwrap().to_string()).collect())
)
);
if let Some(v) = obj.find(&name).and_then(Json::as_array) {
base.options.$key_name = Some(v.iter()
.map(|a| a.as_string().unwrap().to_string())
.collect());
}
} );
($key_name:ident, optional) => ( {
let name = (stringify!($key_name)).replace("_", "-");

View File

@ -213,7 +213,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
None,
);
assoc_bindings.first().map(|b| Self::prohibit_assoc_ty_binding(self.tcx(), b.span));
if let Some(b) = assoc_bindings.first() {
Self::prohibit_assoc_ty_binding(self.tcx(), b.span);
}
substs
}
@ -1095,7 +1097,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
) -> ty::TraitRef<'tcx> {
let (substs, assoc_bindings, _) =
self.create_substs_for_ast_trait_ref(span, trait_def_id, self_ty, trait_segment);
assoc_bindings.first().map(|b| AstConv::prohibit_assoc_ty_binding(self.tcx(), b.span));
if let Some(b) = assoc_bindings.first() {
AstConv::prohibit_assoc_ty_binding(self.tcx(), b.span);
}
ty::TraitRef::new(trait_def_id, substs)
}

View File

@ -36,7 +36,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Requires that the two types unify, and prints an error message if
// they don't.
pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
if let Some(mut e) = self.demand_suptype_diag(sp, expected, actual) {
e.emit();
}
}
pub fn demand_suptype_diag(

View File

@ -4492,15 +4492,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => Err(ErrorReported),
};
if item_name.name != kw::Invalid {
self.report_method_error(
if let Some(mut e) = self.report_method_error(
span,
ty,
item_name,
SelfSource::QPath(qself),
error,
None,
)
.map(|mut e| e.emit());
) {
e.emit();
}
}
result
});

View File

@ -104,7 +104,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
actual: Ty<'tcx>,
ti: TopInfo<'tcx>,
) {
self.demand_eqtype_pat_diag(cause_span, expected, actual, ti).map(|mut err| err.emit());
if let Some(mut err) = self.demand_eqtype_pat_diag(cause_span, expected, actual, ti) {
err.emit();
}
}
}
@ -449,12 +451,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Subtyping doesn't matter here, as the value is some kind of scalar.
let demand_eqtype = |x, y| {
if let Some((_, x_ty, x_span)) = x {
self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti).map(|mut err| {
if let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti) {
if let Some((_, y_ty, y_span)) = y {
self.endpoint_has_type(&mut err, y_span, y_ty);
}
err.emit();
});
};
}
};
demand_eqtype(lhs, rhs);
@ -852,8 +854,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Type-check the tuple struct pattern against the expected type.
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, ti);
let had_err = diag.is_some();
diag.map(|mut err| err.emit());
let had_err = if let Some(mut err) = diag {
err.emit();
true
} else {
false
};
// Type-check subpatterns.
if subpats.len() == variant.fields.len()

View File

@ -165,12 +165,18 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
hir::ExprKind::Binary(..) => {
if !op.node.is_by_value() {
let mut adjustments = tables.adjustments_mut();
adjustments.get_mut(lhs.hir_id).map(|a| a.pop());
adjustments.get_mut(rhs.hir_id).map(|a| a.pop());
if let Some(a) = adjustments.get_mut(lhs.hir_id) {
a.pop();
}
if let Some(a) = adjustments.get_mut(rhs.hir_id) {
a.pop();
}
}
}
hir::ExprKind::AssignOp(..) => {
tables.adjustments_mut().get_mut(lhs.hir_id).map(|a| a.pop());
if let Some(a) = tables.adjustments_mut().get_mut(lhs.hir_id) {
a.pop();
}
}
_ => {}
}
@ -215,7 +221,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
tables.type_dependent_defs_mut().remove(e.hir_id);
tables.node_substs_mut().remove(e.hir_id);
tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
if let Some(a) = tables.adjustments_mut().get_mut(base.hir_id) {
// Discard the need for a mutable borrow
// Extra adjustment made when indexing causes a drop
@ -229,7 +235,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
// So the borrow discard actually happens here
a.pop();
}
});
}
}
}
}

View File

@ -1599,7 +1599,9 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
inline::record_extern_fqn(cx, did, TypeKind::Trait);
let mut param_names = vec![];
reg.clean(cx).map(|b| param_names.push(GenericBound::Outlives(b)));
if let Some(b) = reg.clean(cx) {
param_names.push(GenericBound::Outlives(b));
}
for did in dids {
let empty = cx.tcx.intern_substs(&[]);
let path =
@ -1662,10 +1664,9 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
tr
} else if let ty::Predicate::TypeOutlives(pred) = *predicate {
// these should turn up at the end
pred.skip_binder()
.1
.clean(cx)
.map(|r| regions.push(GenericBound::Outlives(r)));
if let Some(r) = pred.skip_binder().1.clean(cx) {
regions.push(GenericBound::Outlives(r));
}
return None;
} else {
return None;

View File

@ -94,7 +94,7 @@ impl TocBuilder {
loop {
match self.chain.pop() {
Some(mut next) => {
this.map(|e| next.children.entries.push(e));
next.children.entries.extend(this);
if next.level < level {
// this is the parent we want, so return it to
// its rightful place.
@ -105,7 +105,7 @@ impl TocBuilder {
}
}
None => {
this.map(|e| self.top_level.entries.push(e));
self.top_level.entries.extend(this);
return;
}
}

View File

@ -91,7 +91,7 @@ impl<T> Packet<T> {
//
// This can only be called at channel-creation time
pub fn inherit_blocker(&self, token: Option<SignalToken>, guard: MutexGuard<'_, ()>) {
token.map(|token| {
if let Some(token) = token {
assert_eq!(self.cnt.load(Ordering::SeqCst), 0);
assert_eq!(self.to_wake.load(Ordering::SeqCst), 0);
self.to_wake.store(unsafe { token.cast_to_usize() }, Ordering::SeqCst);
@ -118,7 +118,7 @@ impl<T> Packet<T> {
unsafe {
*self.steals.get() = -1;
}
});
}
// When the shared packet is constructed, we grabbed this lock. The
// purpose of this lock is to ensure that abort_selection() doesn't

View File

@ -343,8 +343,12 @@ impl<T> Packet<T> {
mem::drop(guard);
// only outside of the lock do we wake up the pending threads
pending_sender1.map(|t| t.signal());
pending_sender2.map(|t| t.signal());
if let Some(token) = pending_sender1 {
token.signal();
}
if let Some(token) = pending_sender2 {
token.signal();
}
}
// Prepares this shared packet for a channel clone, essentially just bumping
@ -410,7 +414,9 @@ impl<T> Packet<T> {
while let Some(token) = queue.dequeue() {
token.signal();
}
waiter.map(|t| t.signal());
if let Some(token) = waiter {
token.signal();
}
}
}