Skip to content

Commit 90f9640

Browse files
committed
Mark map_or as #[must_use]
1 parent 5a65be8 commit 90f9640

File tree

8 files changed

+18
-15
lines changed

8 files changed

+18
-15
lines changed

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,19 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
140140
impl1_def_id: DefId,
141141
impl2_def_id: DefId,
142142
) {
143-
traits::overlapping_impls(
143+
let maybe_overlap = traits::overlapping_impls(
144144
self.tcx,
145145
impl1_def_id,
146146
impl2_def_id,
147147
// We go ahead and just skip the leak check for
148148
// inherent impls without warning.
149149
SkipLeakCheck::Yes,
150150
overlap_mode,
151-
)
152-
.map_or(true, |overlap| {
151+
);
152+
153+
if let Some(overlap) = maybe_overlap {
153154
self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
154-
false
155-
});
155+
}
156156
}
157157

158158
fn check_item(&mut self, id: hir::ItemId) {

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ impl<'a, 'tcx> Visitor<'tcx> for DropRangeVisitor<'a, 'tcx> {
442442
// We add an edge to the hir_id of the expression/block we are breaking out of, and
443443
// then in process_deferred_edges we will map this hir_id to its PostOrderId, which
444444
// will refer to the end of the block due to the post order traversal.
445-
self.find_target_expression_from_destination(destination).map_or((), |target| {
445+
if let Ok(target) = self.find_target_expression_from_destination(destination) {
446446
self.drop_ranges.add_control_edge_hir_id(self.expr_index, target)
447-
});
447+
}
448448

449449
if let Some(value) = value {
450450
self.visit_expr(value);

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/record_consumed_borrow.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
150150
hir.node_to_string(diag_expr_id),
151151
hir.node_to_string(parent)
152152
);
153-
place_with_id
154-
.try_into()
155-
.map_or((), |tracked_value| self.mark_consumed(parent, tracked_value));
153+
154+
if let Ok(tracked_value) = place_with_id.try_into() {
155+
self.mark_consumed(parent, tracked_value)
156+
}
156157
}
157158

158159
fn borrow(

library/core/src/option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ impl<T> Option<T> {
11251125
/// ```
11261126
#[inline]
11271127
#[stable(feature = "rust1", since = "1.0.0")]
1128+
#[must_use = "if you don't need the returned value, use `if let` instead"]
11281129
pub fn map_or<U, F>(self, default: U, f: F) -> U
11291130
where
11301131
F: FnOnce(T) -> U,

library/core/src/result.rs

+1
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ impl<T, E> Result<T, E> {
768768
/// ```
769769
#[inline]
770770
#[stable(feature = "result_map_or", since = "1.41.0")]
771+
#[must_use = "if you don't need the returned value, use `if let` instead"]
771772
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U {
772773
match self {
773774
Ok(t) => f(t),

src/tools/clippy/clippy_lints/src/operators/bit_mask.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ fn check_compare(cx: &LateContext<'_>, bit_op: &Expr<'_>, cmp_op: BinOpKind, cmp
4040
if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
4141
return;
4242
}
43-
fetch_int_literal(cx, right)
44-
.or_else(|| fetch_int_literal(cx, left))
45-
.map_or((), |mask| check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span));
43+
if let Some(mask) = fetch_int_literal(cx, right).or_else(|| fetch_int_literal(cx, left)) {
44+
check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span);
45+
}
4646
}
4747
}
4848

src/tools/clippy/tests/ui/result_map_or_into_option.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn main() {
1515
// A non-Some `f` closure where the argument is not used as the
1616
// return should not emit the lint
1717
let opt: Result<u32, &str> = Ok(1);
18-
opt.map_or(None, |_x| Some(1));
18+
_ = opt.map_or(None, |_x| Some(1));
1919
}

src/tools/clippy/tests/ui/result_map_or_into_option.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn main() {
1515
// A non-Some `f` closure where the argument is not used as the
1616
// return should not emit the lint
1717
let opt: Result<u32, &str> = Ok(1);
18-
opt.map_or(None, |_x| Some(1));
18+
_ = opt.map_or(None, |_x| Some(1));
1919
}

0 commit comments

Comments
 (0)