Skip to content

Commit 2325c20

Browse files
committed
Avoid unused Option::map results
These are changes that would be needed if we add `#[must_use]` to `Option::map`, per #71484.
1 parent 3360cc3 commit 2325c20

File tree

21 files changed

+111
-64
lines changed

21 files changed

+111
-64
lines changed

src/librustc_attr/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
9898
}
9999
}
100100

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

src/librustc_expand/expand.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
11721172
// ignore derives so they remain unused
11731173
let (attr, after_derive) = self.classify_nonitem(&mut expr);
11741174

1175-
if attr.is_some() {
1175+
if let Some(ref attr_value) = attr {
11761176
// Collect the invoc regardless of whether or not attributes are permitted here
11771177
// expansion will eat the attribute so it won't error later.
1178-
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1178+
self.cfg.maybe_emit_expr_attr_err(attr_value);
11791179

11801180
// AstFragmentKind::Expr requires the macro to emit an expression.
11811181
return self
@@ -1322,8 +1322,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13221322
// Ignore derives so they remain unused.
13231323
let (attr, after_derive) = self.classify_nonitem(&mut expr);
13241324

1325-
if attr.is_some() {
1326-
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1325+
if let Some(ref attr_value) = attr {
1326+
self.cfg.maybe_emit_expr_attr_err(attr_value);
13271327

13281328
return self
13291329
.collect_attr(

src/librustc_hir/definitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl DefPath {
246246

247247
let mut opt_delimiter = None;
248248
for component in &self.data {
249-
opt_delimiter.map(|d| s.push(d));
249+
s.extend(opt_delimiter);
250250
opt_delimiter = Some('-');
251251
if component.disambiguator == 0 {
252252
write!(s, "{}", component.data.as_symbol()).unwrap();

src/librustc_infer/infer/error_reporting/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
755755
},
756756
ObligationCauseCode::IfExpression(box IfExpressionCause { then, outer, semicolon }) => {
757757
err.span_label(then, "expected because of this");
758-
outer.map(|sp| err.span_label(sp, "`if` and `else` have incompatible types"));
758+
if let Some(sp) = outer {
759+
err.span_label(sp, "`if` and `else` have incompatible types");
760+
}
759761
if let Some(sp) = semicolon {
760762
err.span_suggestion_short(
761763
sp,

src/librustc_metadata/creader.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,15 @@ fn dump_crates(cstore: &CStore) {
101101
info!(" hash: {}", data.hash());
102102
info!(" reqd: {:?}", data.dep_kind());
103103
let CrateSource { dylib, rlib, rmeta } = data.source();
104-
dylib.as_ref().map(|dl| info!(" dylib: {}", dl.0.display()));
105-
rlib.as_ref().map(|rl| info!(" rlib: {}", rl.0.display()));
106-
rmeta.as_ref().map(|rl| info!(" rmeta: {}", rl.0.display()));
104+
if let Some(dylib) = dylib {
105+
info!(" dylib: {}", dylib.0.display());
106+
}
107+
if let Some(rlib) = rlib {
108+
info!(" rlib: {}", rlib.0.display());
109+
}
110+
if let Some(rmeta) = rmeta {
111+
info!(" rmeta: {}", rmeta.0.display());
112+
}
107113
});
108114
}
109115

src/librustc_mir_build/build/matches/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13881388
}
13891389

13901390
// Insert a Shallow borrow of any places that is switched on.
1391-
fake_borrows.as_mut().map(|fb| fb.insert(match_place));
1391+
if let Some(fb) = fake_borrows {
1392+
fb.insert(match_place);
1393+
}
13921394

13931395
// perform the test, branching to one of N blocks. For each of
13941396
// those N possible outcomes, create a (initially empty)

src/librustc_parse/parser/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,8 @@ pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, sess: &Pa
12061206
*sess.reached_eof.borrow_mut() |=
12071207
unclosed_delims.iter().any(|unmatched_delim| unmatched_delim.found_delim.is_none());
12081208
for unmatched in unclosed_delims.drain(..) {
1209-
make_unclosed_delims_error(unmatched, sess).map(|mut e| {
1209+
if let Some(mut e) = make_unclosed_delims_error(unmatched, sess) {
12101210
e.emit();
1211-
});
1211+
}
12121212
}
12131213
}

src/librustc_passes/loops.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
6868
self.with_context(LabeledBlock, |v| v.visit_block(&b));
6969
}
7070
hir::ExprKind::Break(label, ref opt_expr) => {
71-
opt_expr.as_ref().map(|e| self.visit_expr(e));
71+
if let Some(e) = opt_expr {
72+
self.visit_expr(e);
73+
}
7274

7375
if self.require_label_in_labeled_block(e.span, &label, "break") {
7476
// If we emitted an error about an unlabeled break in a labeled

src/librustc_query_system/query/job.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ impl<CTX: QueryContext> QueryJob<CTX> {
133133
/// as there are no concurrent jobs which could be waiting on us
134134
pub fn signal_complete(self) {
135135
#[cfg(parallel_compiler)]
136-
self.latch.map(|latch| latch.set());
136+
{
137+
if let Some(latch) = self.latch {
138+
latch.set();
139+
}
140+
}
137141
}
138142
}
139143

src/librustc_resolve/late.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,9 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19961996
this.visit_expr(cond);
19971997
this.visit_block(then);
19981998
});
1999-
opt_else.as_ref().map(|expr| self.visit_expr(expr));
1999+
if let Some(expr) = opt_else {
2000+
self.visit_expr(expr);
2001+
}
20002002
}
20012003

20022004
ExprKind::Loop(ref block, label) => self.resolve_labeled_block(label, expr.id, &block),

src/librustc_resolve/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ impl<'a> ModuleData<'a> {
499499
F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>),
500500
{
501501
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
502-
name_resolution.borrow().binding.map(|binding| f(resolver, key.ident, key.ns, binding));
502+
if let Some(binding) = name_resolution.borrow().binding {
503+
f(resolver, key.ident, key.ns, binding);
504+
}
503505
}
504506
}
505507

src/librustc_target/spec/mod.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -979,20 +979,21 @@ impl Target {
979979
macro_rules! key {
980980
($key_name:ident) => ( {
981981
let name = (stringify!($key_name)).replace("_", "-");
982-
obj.find(&name[..]).map(|o| o.as_string()
983-
.map(|s| base.options.$key_name = s.to_string()));
982+
if let Some(s) = obj.find(&name).and_then(Json::as_string) {
983+
base.options.$key_name = s.to_string();
984+
}
984985
} );
985986
($key_name:ident, bool) => ( {
986987
let name = (stringify!($key_name)).replace("_", "-");
987-
obj.find(&name[..])
988-
.map(|o| o.as_boolean()
989-
.map(|s| base.options.$key_name = s));
988+
if let Some(s) = obj.find(&name).and_then(Json::as_boolean) {
989+
base.options.$key_name = s;
990+
}
990991
} );
991992
($key_name:ident, Option<u64>) => ( {
992993
let name = (stringify!($key_name)).replace("_", "-");
993-
obj.find(&name[..])
994-
.map(|o| o.as_u64()
995-
.map(|s| base.options.$key_name = Some(s)));
994+
if let Some(s) = obj.find(&name).and_then(Json::as_u64) {
995+
base.options.$key_name = Some(s);
996+
}
996997
} );
997998
($key_name:ident, MergeFunctions) => ( {
998999
let name = (stringify!($key_name)).replace("_", "-");
@@ -1034,19 +1035,19 @@ impl Target {
10341035
} );
10351036
($key_name:ident, list) => ( {
10361037
let name = (stringify!($key_name)).replace("_", "-");
1037-
obj.find(&name[..]).map(|o| o.as_array()
1038-
.map(|v| base.options.$key_name = v.iter()
1039-
.map(|a| a.as_string().unwrap().to_string()).collect()
1040-
)
1041-
);
1038+
if let Some(v) = obj.find(&name).and_then(Json::as_array) {
1039+
base.options.$key_name = v.iter()
1040+
.map(|a| a.as_string().unwrap().to_string())
1041+
.collect();
1042+
}
10421043
} );
10431044
($key_name:ident, opt_list) => ( {
10441045
let name = (stringify!($key_name)).replace("_", "-");
1045-
obj.find(&name[..]).map(|o| o.as_array()
1046-
.map(|v| base.options.$key_name = Some(v.iter()
1047-
.map(|a| a.as_string().unwrap().to_string()).collect())
1048-
)
1049-
);
1046+
if let Some(v) = obj.find(&name).and_then(Json::as_array) {
1047+
base.options.$key_name = Some(v.iter()
1048+
.map(|a| a.as_string().unwrap().to_string())
1049+
.collect());
1050+
}
10501051
} );
10511052
($key_name:ident, optional) => ( {
10521053
let name = (stringify!($key_name)).replace("_", "-");

src/librustc_typeck/astconv.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
213213
None,
214214
);
215215

216-
assoc_bindings.first().map(|b| Self::prohibit_assoc_ty_binding(self.tcx(), b.span));
216+
if let Some(b) = assoc_bindings.first() {
217+
Self::prohibit_assoc_ty_binding(self.tcx(), b.span);
218+
}
217219

218220
substs
219221
}
@@ -1095,7 +1097,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10951097
) -> ty::TraitRef<'tcx> {
10961098
let (substs, assoc_bindings, _) =
10971099
self.create_substs_for_ast_trait_ref(span, trait_def_id, self_ty, trait_segment);
1098-
assoc_bindings.first().map(|b| AstConv::prohibit_assoc_ty_binding(self.tcx(), b.span));
1100+
if let Some(b) = assoc_bindings.first() {
1101+
AstConv::prohibit_assoc_ty_binding(self.tcx(), b.span);
1102+
}
10991103
ty::TraitRef::new(trait_def_id, substs)
11001104
}
11011105

src/librustc_typeck/check/demand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3636
// Requires that the two types unify, and prints an error message if
3737
// they don't.
3838
pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
39-
self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
39+
if let Some(mut e) = self.demand_suptype_diag(sp, expected, actual) {
40+
e.emit();
41+
}
4042
}
4143

4244
pub fn demand_suptype_diag(

src/librustc_typeck/check/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4492,15 +4492,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
44924492
_ => Err(ErrorReported),
44934493
};
44944494
if item_name.name != kw::Invalid {
4495-
self.report_method_error(
4495+
if let Some(mut e) = self.report_method_error(
44964496
span,
44974497
ty,
44984498
item_name,
44994499
SelfSource::QPath(qself),
45004500
error,
45014501
None,
4502-
)
4503-
.map(|mut e| e.emit());
4502+
) {
4503+
e.emit();
4504+
}
45044505
}
45054506
result
45064507
});

src/librustc_typeck/check/pat.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
104104
actual: Ty<'tcx>,
105105
ti: TopInfo<'tcx>,
106106
) {
107-
self.demand_eqtype_pat_diag(cause_span, expected, actual, ti).map(|mut err| err.emit());
107+
if let Some(mut err) = self.demand_eqtype_pat_diag(cause_span, expected, actual, ti) {
108+
err.emit();
109+
}
108110
}
109111
}
110112

@@ -449,12 +451,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
449451
// Subtyping doesn't matter here, as the value is some kind of scalar.
450452
let demand_eqtype = |x, y| {
451453
if let Some((_, x_ty, x_span)) = x {
452-
self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti).map(|mut err| {
454+
if let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti) {
453455
if let Some((_, y_ty, y_span)) = y {
454456
self.endpoint_has_type(&mut err, y_span, y_ty);
455457
}
456458
err.emit();
457-
});
459+
};
458460
}
459461
};
460462
demand_eqtype(lhs, rhs);
@@ -852,8 +854,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
852854

853855
// Type-check the tuple struct pattern against the expected type.
854856
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, ti);
855-
let had_err = diag.is_some();
856-
diag.map(|mut err| err.emit());
857+
let had_err = if let Some(mut err) = diag {
858+
err.emit();
859+
true
860+
} else {
861+
false
862+
};
857863

858864
// Type-check subpatterns.
859865
if subpats.len() == variant.fields.len()

src/librustc_typeck/check/writeback.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,18 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
165165
hir::ExprKind::Binary(..) => {
166166
if !op.node.is_by_value() {
167167
let mut adjustments = tables.adjustments_mut();
168-
adjustments.get_mut(lhs.hir_id).map(|a| a.pop());
169-
adjustments.get_mut(rhs.hir_id).map(|a| a.pop());
168+
if let Some(a) = adjustments.get_mut(lhs.hir_id) {
169+
a.pop();
170+
}
171+
if let Some(a) = adjustments.get_mut(rhs.hir_id) {
172+
a.pop();
173+
}
170174
}
171175
}
172176
hir::ExprKind::AssignOp(..) => {
173-
tables.adjustments_mut().get_mut(lhs.hir_id).map(|a| a.pop());
177+
if let Some(a) = tables.adjustments_mut().get_mut(lhs.hir_id) {
178+
a.pop();
179+
}
174180
}
175181
_ => {}
176182
}
@@ -215,7 +221,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
215221
tables.type_dependent_defs_mut().remove(e.hir_id);
216222
tables.node_substs_mut().remove(e.hir_id);
217223

218-
tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
224+
if let Some(a) = tables.adjustments_mut().get_mut(base.hir_id) {
219225
// Discard the need for a mutable borrow
220226

221227
// Extra adjustment made when indexing causes a drop
@@ -229,7 +235,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
229235
// So the borrow discard actually happens here
230236
a.pop();
231237
}
232-
});
238+
}
233239
}
234240
}
235241
}

src/librustdoc/clean/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,9 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
15991599
inline::record_extern_fqn(cx, did, TypeKind::Trait);
16001600

16011601
let mut param_names = vec![];
1602-
reg.clean(cx).map(|b| param_names.push(GenericBound::Outlives(b)));
1602+
if let Some(b) = reg.clean(cx) {
1603+
param_names.push(GenericBound::Outlives(b));
1604+
}
16031605
for did in dids {
16041606
let empty = cx.tcx.intern_substs(&[]);
16051607
let path =
@@ -1662,10 +1664,9 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16621664
tr
16631665
} else if let ty::Predicate::TypeOutlives(pred) = *predicate {
16641666
// these should turn up at the end
1665-
pred.skip_binder()
1666-
.1
1667-
.clean(cx)
1668-
.map(|r| regions.push(GenericBound::Outlives(r)));
1667+
if let Some(r) = pred.skip_binder().1.clean(cx) {
1668+
regions.push(GenericBound::Outlives(r));
1669+
}
16691670
return None;
16701671
} else {
16711672
return None;

src/librustdoc/html/toc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl TocBuilder {
9494
loop {
9595
match self.chain.pop() {
9696
Some(mut next) => {
97-
this.map(|e| next.children.entries.push(e));
97+
next.children.entries.extend(this);
9898
if next.level < level {
9999
// this is the parent we want, so return it to
100100
// its rightful place.
@@ -105,7 +105,7 @@ impl TocBuilder {
105105
}
106106
}
107107
None => {
108-
this.map(|e| self.top_level.entries.push(e));
108+
self.top_level.entries.extend(this);
109109
return;
110110
}
111111
}

0 commit comments

Comments
 (0)