Skip to content

Commit 9e14e5d

Browse files
committed
new lint unnecessary_map_or
1 parent a79db2a commit 9e14e5d

File tree

77 files changed

+508
-146
lines changed

Some content is hidden

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

77 files changed

+508
-146
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5740,6 +5740,7 @@ Released 2018-09-13
57405740
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
57415741
[`unnecessary_literal_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap
57425742
[`unnecessary_map_on_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_map_on_constructor
5743+
[`unnecessary_map_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_map_or
57435744
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
57445745
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
57455746
[`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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute])
1616
return;
1717
}
1818
if let Some(lint_list) = &attr.meta_item_list() {
19-
if attr.ident().map_or(false, |ident| is_lint_level(ident.name, attr.id)) {
19+
if attr.ident().is_some_and(|ident| is_lint_level(ident.name, attr.id)) {
2020
for lint in lint_list {
2121
match item.kind {
2222
ItemKind::Use(..) => {
@@ -25,7 +25,7 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute])
2525
|| is_word(lint, sym!(unreachable_pub))
2626
|| is_word(lint, sym!(unused))
2727
|| is_word(lint, sym!(unused_import_braces))
28-
|| extract_clippy_lint(lint).map_or(false, |s| {
28+
|| extract_clippy_lint(lint).is_some_and(|s| {
2929
matches!(
3030
s.as_str(),
3131
"wildcard_imports"

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::Local(_) => 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
@@ -577,7 +577,7 @@ fn implements_ord(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
577577
let ty = cx.typeck_results().expr_ty(expr);
578578
cx.tcx
579579
.get_diagnostic_item(sym::Ord)
580-
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
580+
.is_some_and(|id| implements_trait(cx, ty, id, &[]))
581581
}
582582

583583
struct NotSimplificationVisitor<'a, 'tcx> {

clippy_lints/src/box_default.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl LateLintPass<'_> for BoxDefault {
5050
// And that method is `new`
5151
&& seg.ident.name == sym::new
5252
// And the call is that of a `Box` method
53-
&& path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box())
53+
&& path_def_id(cx, ty).is_some_and(|id| Some(id) == cx.tcx.lang_items().owned_box())
5454
// And the single argument to the call is another function call
5555
// This is the `T::default()` of `Box::new(T::default())`
5656
&& let ExprKind::Call(arg_path, inner_call_args) = arg.kind
@@ -60,6 +60,7 @@ impl LateLintPass<'_> for BoxDefault {
6060
// or that we are inside a `vec!` macro expansion
6161
&& (expr.span.eq_ctxt(arg.span) || is_local_vec_expn(cx, arg, expr))
6262
// And the argument is equivalent to `Default::default()`
63+
&& path_def_id(cx, ty).is_some_and(|id| Some(id) == cx.tcx.lang_items().owned_box())
6364
&& is_default_equivalent(cx, arg)
6465
{
6566
span_lint_and_sugg(
@@ -118,9 +119,9 @@ fn explicit_default_type<'a>(arg_path: &'a Expr<'_>) -> Option<&'a Ty<'a>> {
118119
}
119120

120121
fn is_local_vec_expn(cx: &LateContext<'_>, expr: &Expr<'_>, ref_expr: &Expr<'_>) -> bool {
121-
macro_backtrace(expr.span).next().map_or(false, |call| {
122-
cx.tcx.is_diagnostic_item(sym::vec_macro, call.def_id) && call.span.eq_ctxt(ref_expr.span)
123-
})
122+
macro_backtrace(expr.span)
123+
.next()
124+
.is_some_and(|call| cx.tcx.is_diagnostic_item(sym::vec_macro, call.def_id) && call.span.eq_ctxt(ref_expr.span))
124125
}
125126

126127
#[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
@@ -272,7 +272,7 @@ fn lint_branches_sharing_code<'tcx>(
272272
let span = span.with_hi(last_block.span.hi());
273273
// Improve formatting if the inner block has indention (i.e. normal Rust formatting)
274274
let test_span = Span::new(span.lo() - BytePos(4), span.lo(), span.ctxt(), span.parent());
275-
let span = if snippet_opt(cx, test_span).map_or(false, |snip| snip == " ") {
275+
let span = if snippet_opt(cx, test_span).is_some_and(|snip| snip == " ") {
276276
span.with_lo(test_span.lo())
277277
} else {
278278
span
@@ -353,7 +353,7 @@ fn eq_binding_names(s: &Stmt<'_>, names: &[(HirId, Symbol)]) -> bool {
353353
let mut i = 0usize;
354354
let mut res = true;
355355
l.pat.each_binding_or_first(&mut |_, _, _, name| {
356-
if names.get(i).map_or(false, |&(_, n)| n == name.name) {
356+
if names.get(i).is_some_and(|&(_, n)| n == name.name) {
357357
i += 1;
358358
} else {
359359
res = false;
@@ -397,12 +397,10 @@ fn eq_stmts(
397397
let new_bindings = &moved_bindings[old_count..];
398398
blocks
399399
.iter()
400-
.all(|b| get_stmt(b).map_or(false, |s| eq_binding_names(s, new_bindings)))
400+
.all(|b| get_stmt(b).is_some_and(|s| eq_binding_names(s, new_bindings)))
401401
} else {
402402
true
403-
}) && blocks
404-
.iter()
405-
.all(|b| get_stmt(b).map_or(false, |s| eq.eq_stmt(s, stmt)))
403+
}) && blocks.iter().all(|b| get_stmt(b).is_some_and(|s| eq.eq_stmt(s, stmt)))
406404
}
407405

408406
#[expect(clippy::too_many_lines)]
@@ -459,9 +457,7 @@ fn scan_block_for_eq<'tcx>(
459457
// x + 50
460458
let expr_hash_eq = if let Some(e) = block.expr {
461459
let hash = hash_expr(cx, e);
462-
blocks
463-
.iter()
464-
.all(|b| b.expr.map_or(false, |e| hash_expr(cx, e) == hash))
460+
blocks.iter().all(|b| b.expr.is_some_and(|e| hash_expr(cx, e) == hash))
465461
} else {
466462
blocks.iter().all(|b| b.expr.is_none())
467463
};
@@ -522,7 +518,7 @@ fn scan_block_for_eq<'tcx>(
522518
});
523519
if let Some(e) = block.expr {
524520
for block in blocks {
525-
if block.expr.map_or(false, |expr| !eq.eq_expr(expr, e)) {
521+
if block.expr.is_some_and(|expr| !eq.eq_expr(expr, e)) {
526522
moved_locals.truncate(moved_locals_at_start);
527523
return BlockEq {
528524
start_end_eq,
@@ -541,7 +537,7 @@ fn scan_block_for_eq<'tcx>(
541537
}
542538

543539
fn check_for_warn_of_moved_symbol(cx: &LateContext<'_>, symbols: &[(HirId, Symbol)], if_expr: &Expr<'_>) -> bool {
544-
get_enclosing_block(cx, if_expr.hir_id).map_or(false, |block| {
540+
get_enclosing_block(cx, if_expr.hir_id).is_some_and(|block| {
545541
let ignore_span = block.span.shrink_to_lo().to(if_expr.span);
546542

547543
symbols

0 commit comments

Comments
 (0)