Skip to content

Commit 3add1a4

Browse files
committed
new lint unnecessary_map_or
1 parent 3e84ca8 commit 3add1a4

File tree

80 files changed

+554
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+554
-154
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5918,6 +5918,7 @@ Released 2018-09-13
59185918
[`unnecessary_literal_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap
59195919
[`unnecessary_map_on_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_map_on_constructor
59205920
[`unnecessary_min_or_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_min_or_max
5921+
[`unnecessary_map_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_map_or
59215922
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
59225923
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
59235924
[`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings

clippy_dev/src/setup/vscode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn delete_vs_task_file(path: &Path) -> bool {
8484
/// It may fail silently.
8585
fn try_delete_vs_directory_if_empty() {
8686
let path = Path::new(VSCODE_DIR);
87-
if path.read_dir().map_or(false, |mut iter| iter.next().is_none()) {
87+
if path.read_dir().is_ok_and(|mut iter| iter.next().is_none()) {
8888
// The directory is empty. We just try to delete it but allow a silence
8989
// fail as an empty `.vscode` directory is still valid
9090
let _silence_result = fs::remove_dir(path);

clippy_lints/src/attrs/useless_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute])
1717
return;
1818
}
1919
if let Some(lint_list) = &attr.meta_item_list() {
20-
if attr.ident().map_or(false, |ident| is_lint_level(ident.name, attr.id)) {
20+
if attr.ident().is_some_and(|ident| is_lint_level(ident.name, attr.id)) {
2121
for lint in lint_list {
2222
match item.kind {
2323
ItemKind::Use(..) => {

clippy_lints/src/attrs/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn is_relevant_block(cx: &LateContext<'_>, typeck_results: &ty::TypeckResults<'_
5050
block
5151
.expr
5252
.as_ref()
53-
.map_or(false, |e| is_relevant_expr(cx, typeck_results, e)),
53+
.is_some_and(|e| is_relevant_expr(cx, typeck_results, e)),
5454
|stmt| match &stmt.kind {
5555
StmtKind::Let(_) => true,
5656
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, typeck_results, expr),
@@ -60,7 +60,7 @@ fn is_relevant_block(cx: &LateContext<'_>, typeck_results: &ty::TypeckResults<'_
6060
}
6161

6262
fn is_relevant_expr(cx: &LateContext<'_>, typeck_results: &ty::TypeckResults<'_>, expr: &Expr<'_>) -> bool {
63-
if macro_backtrace(expr.span).last().map_or(false, |macro_call| {
63+
if macro_backtrace(expr.span).last().is_some_and(|macro_call| {
6464
is_panic(cx, macro_call.def_id) || cx.tcx.item_name(macro_call.def_id) == sym::unreachable
6565
}) {
6666
return false;

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -
6060
trait_id,
6161
)
6262
})
63-
.map_or(false, |assoc_item| {
63+
.is_some_and(|assoc_item| {
6464
let proj = Ty::new_projection(cx.tcx, assoc_item.def_id, cx.tcx.mk_args_trait(ty, []));
6565
let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj);
6666

clippy_lints/src/booleans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,5 +607,5 @@ fn implements_ord(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
607607
let ty = cx.typeck_results().expr_ty(expr);
608608
cx.tcx
609609
.get_diagnostic_item(sym::Ord)
610-
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
610+
.is_some_and(|id| implements_trait(cx, ty, id, &[]))
611611
}

clippy_lints/src/box_default.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl LateLintPass<'_> for BoxDefault {
4545
// And that method is `new`
4646
&& seg.ident.name == sym::new
4747
// And the call is that of a `Box` method
48-
&& path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box())
48+
&& path_def_id(cx, ty).is_some_and(|id| Some(id) == cx.tcx.lang_items().owned_box())
4949
// And the single argument to the call is another function call
5050
// This is the `T::default()` of `Box::new(T::default())`
5151
&& let ExprKind::Call(arg_path, _) = arg.kind
@@ -83,9 +83,9 @@ fn is_plain_default(cx: &LateContext<'_>, arg_path: &Expr<'_>) -> bool {
8383
}
8484

8585
fn is_local_vec_expn(cx: &LateContext<'_>, expr: &Expr<'_>, ref_expr: &Expr<'_>) -> bool {
86-
macro_backtrace(expr.span).next().map_or(false, |call| {
87-
cx.tcx.is_diagnostic_item(sym::vec_macro, call.def_id) && call.span.eq_ctxt(ref_expr.span)
88-
})
86+
macro_backtrace(expr.span)
87+
.next()
88+
.is_some_and(|call| cx.tcx.is_diagnostic_item(sym::vec_macro, call.def_id) && call.span.eq_ctxt(ref_expr.span))
8989
}
9090

9191
#[derive(Default)]

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ pub(super) fn check<'tcx>(
159159
// The same is true if the expression encompassing the cast expression is a unary
160160
// expression or an addressof expression.
161161
let needs_block = matches!(cast_expr.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..))
162-
|| get_parent_expr(cx, expr)
163-
.map_or(false, |e| matches!(e.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..)));
162+
|| get_parent_expr(cx, expr).is_some_and(|e| matches!(e.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..)));
164163

165164
span_lint_and_sugg(
166165
cx,

clippy_lints/src/comparison_chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
110110
let is_ord = cx
111111
.tcx
112112
.get_diagnostic_item(sym::Ord)
113-
.map_or(false, |id| implements_trait(cx, ty, id, &[]));
113+
.is_some_and(|id| implements_trait(cx, ty, id, &[]));
114114

115115
if !is_ord {
116116
return;

clippy_lints/src/copies.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn lint_branches_sharing_code<'tcx>(
267267
let span = span.with_hi(last_block.span.hi());
268268
// Improve formatting if the inner block has indention (i.e. normal Rust formatting)
269269
let test_span = Span::new(span.lo() - BytePos(4), span.lo(), span.ctxt(), span.parent());
270-
let span = if snippet_opt(cx, test_span).map_or(false, |snip| snip == " ") {
270+
let span = if snippet_opt(cx, test_span).is_some_and(|snip| snip == " ") {
271271
span.with_lo(test_span.lo())
272272
} else {
273273
span
@@ -348,7 +348,7 @@ fn eq_binding_names(s: &Stmt<'_>, names: &[(HirId, Symbol)]) -> bool {
348348
let mut i = 0usize;
349349
let mut res = true;
350350
l.pat.each_binding_or_first(&mut |_, _, _, name| {
351-
if names.get(i).map_or(false, |&(_, n)| n == name.name) {
351+
if names.get(i).is_some_and(|&(_, n)| n == name.name) {
352352
i += 1;
353353
} else {
354354
res = false;
@@ -392,12 +392,10 @@ fn eq_stmts(
392392
let new_bindings = &moved_bindings[old_count..];
393393
blocks
394394
.iter()
395-
.all(|b| get_stmt(b).map_or(false, |s| eq_binding_names(s, new_bindings)))
395+
.all(|b| get_stmt(b).is_some_and(|s| eq_binding_names(s, new_bindings)))
396396
} else {
397397
true
398-
}) && blocks
399-
.iter()
400-
.all(|b| get_stmt(b).map_or(false, |s| eq.eq_stmt(s, stmt)))
398+
}) && blocks.iter().all(|b| get_stmt(b).is_some_and(|s| eq.eq_stmt(s, stmt)))
401399
}
402400

403401
#[expect(clippy::too_many_lines)]
@@ -454,9 +452,7 @@ fn scan_block_for_eq<'tcx>(
454452
// x + 50
455453
let expr_hash_eq = if let Some(e) = block.expr {
456454
let hash = hash_expr(cx, e);
457-
blocks
458-
.iter()
459-
.all(|b| b.expr.map_or(false, |e| hash_expr(cx, e) == hash))
455+
blocks.iter().all(|b| b.expr.is_some_and(|e| hash_expr(cx, e) == hash))
460456
} else {
461457
blocks.iter().all(|b| b.expr.is_none())
462458
};
@@ -517,7 +513,7 @@ fn scan_block_for_eq<'tcx>(
517513
});
518514
if let Some(e) = block.expr {
519515
for block in blocks {
520-
if block.expr.map_or(false, |expr| !eq.eq_expr(expr, e)) {
516+
if block.expr.is_some_and(|expr| !eq.eq_expr(expr, e)) {
521517
moved_locals.truncate(moved_locals_at_start);
522518
return BlockEq {
523519
start_end_eq,
@@ -536,7 +532,7 @@ fn scan_block_for_eq<'tcx>(
536532
}
537533

538534
fn check_for_warn_of_moved_symbol(cx: &LateContext<'_>, symbols: &[(HirId, Symbol)], if_expr: &Expr<'_>) -> bool {
539-
get_enclosing_block(cx, if_expr.hir_id).map_or(false, |block| {
535+
get_enclosing_block(cx, if_expr.hir_id).is_some_and(|block| {
540536
let ignore_span = block.span.shrink_to_lo().to(if_expr.span);
541537

542538
symbols

0 commit comments

Comments
 (0)