Skip to content

Commit

Permalink
Rollup merge of #71534 - cuviper:unused-option-map, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Avoid unused Option::map results

These are changes that would be needed if we add `#[must_use]` to `Option::map`, per #71484.

r? @Mark-Simulacrum
  • Loading branch information
Dylan-DPC authored Apr 24, 2020
2 parents 2ca6df7 + 2325c20 commit d0db0a8
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -110,7 +110,7 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
Applicability::MachineApplicable,
)
.emit();
});
};
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_mir_build/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_parse/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}
}
4 changes: 3 additions & 1 deletion src/librustc_passes/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_query_system/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
37 changes: 19 additions & 18 deletions src/librustc_target/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("_", "-");
Expand Down Expand Up @@ -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("_", "-");
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
16 changes: 11 additions & 5 deletions src/librustc_typeck/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 11 additions & 5 deletions src/librustc_typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
_ => {}
}
Expand Down Expand Up @@ -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
Expand All @@ -229,7 +235,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
// So the borrow discard actually happens here
a.pop();
}
});
}
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -105,7 +105,7 @@ impl TocBuilder {
}
}
None => {
this.map(|e| self.top_level.entries.push(e));
self.top_level.entries.extend(this);
return;
}
}
Expand Down
Loading

0 comments on commit d0db0a8

Please sign in to comment.