Skip to content

Commit 895aee2

Browse files
committed
use maybe_par with Sugg
1 parent 1f8a5f2 commit 895aee2

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

clippy_lints/src/methods/unnecessary_map_or.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
use std::borrow::Cow;
2+
13
use clippy_config::msrvs::{self, Msrv};
24
use clippy_utils::diagnostics::span_lint_and_sugg;
35
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
4-
use clippy_utils::source::{snippet, snippet_opt};
6+
use clippy_utils::source::snippet_opt;
7+
use clippy_utils::sugg::{Sugg, make_binop};
58
use clippy_utils::ty::get_type_diagnostic_name;
69
use clippy_utils::visitors::is_local_used;
7-
use clippy_utils::{get_parent_expr, is_from_proc_macro, path_to_local_id};
10+
use clippy_utils::{is_from_proc_macro, path_to_local_id};
811
use rustc_ast::LitKind::Bool;
9-
use rustc_ast::util::parser::AssocOp;
1012
use rustc_errors::Applicability;
1113
use rustc_hir::{BinOpKind, Expr, ExprKind, PatKind};
1214
use rustc_lint::LateContext;
@@ -80,26 +82,23 @@ pub(super) fn check<'a>(
8082
&& typeck_results.expr_ty(l) == typeck_results.expr_ty(r)
8183
{
8284
let wrap = variant.variant_name();
83-
let comparator = op.node.as_str();
8485

8586
// we may need to add parens around the suggestion
8687
// in case the parent expression has additional method calls,
8788
// since for example `Some(5).map_or(false, |x| x == 5).then(|| 1)`
8889
// being converted to `Some(5) == Some(5).then(|| 1)` isnt
8990
// the same thing
9091

91-
let should_add_parens = get_parent_expr(cx, expr)
92-
.is_some_and(|expr| expr.precedence().order() > i8::try_from(AssocOp::Equal.precedence()).unwrap_or(0));
93-
(
94-
format!(
95-
"{}{} {comparator} {wrap}({}){}",
96-
if should_add_parens { "(" } else { "" },
97-
snippet(cx, recv.span, ".."),
98-
snippet(cx, non_binding_location.span.source_callsite(), ".."),
99-
if should_add_parens { ")" } else { "" }
100-
),
101-
"a standard comparison",
102-
)
92+
let inner_non_binding = Sugg::NonParen(Cow::Owned(format!(
93+
"{wrap}({})",
94+
Sugg::hir(cx, non_binding_location, "")
95+
)));
96+
97+
let binop = make_binop(op.node, &Sugg::hir(cx, recv, ".."), &inner_non_binding)
98+
.maybe_par()
99+
.into_string();
100+
101+
(binop, "a standard comparison")
103102
} else if !def_bool
104103
&& msrv.meets(msrvs::OPTION_RESULT_IS_VARIANT_AND)
105104
&& let Some(recv_callsite) = snippet_opt(cx, recv.span.source_callsite())

tests/ui/unnecessary_map_or.fixed

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ extern crate proc_macros;
99

1010
fn main() {
1111
// should trigger
12-
let _ = Some(5) == Some(5);
13-
let _ = Some(5) != Some(5);
14-
let _ = Some(5) == Some(5);
12+
let _ = (Some(5) == Some(5));
13+
let _ = (Some(5) != Some(5));
14+
let _ = (Some(5) == Some(5));
1515
let _ = Some(5).is_some_and(|n| {
1616
let _ = n;
1717
6 >= 5
@@ -21,7 +21,7 @@ fn main() {
2121
let _ = Some(5).is_some_and(|n| n == n);
2222
let _ = Some(5).is_some_and(|n| n == if 2 > 1 { n } else { 0 });
2323
let _ = Ok::<Vec<i32>, i32>(vec![5]).is_ok_and(|n| n == [5]);
24-
let _ = Ok::<i32, i32>(5) == Ok(5);
24+
let _ = (Ok::<i32, i32>(5) == Ok(5));
2525
let _ = (Some(5) == Some(5)).then(|| 1);
2626

2727
// shouldnt trigger

tests/ui/unnecessary_map_or.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: this `map_or` is redundant
22
--> tests/ui/unnecessary_map_or.rs:12:13
33
|
44
LL | let _ = Some(5).map_or(false, |n| n == 5);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `Some(5) == Some(5)`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `(Some(5) == Some(5))`
66
|
77
= note: `-D clippy::unnecessary-map-or` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_map_or)]`
@@ -11,7 +11,7 @@ error: this `map_or` is redundant
1111
--> tests/ui/unnecessary_map_or.rs:13:13
1212
|
1313
LL | let _ = Some(5).map_or(true, |n| n != 5);
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `Some(5) != Some(5)`
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `(Some(5) != Some(5))`
1515

1616
error: this `map_or` is redundant
1717
--> tests/ui/unnecessary_map_or.rs:14:13
@@ -21,7 +21,7 @@ LL | let _ = Some(5).map_or(false, |n| {
2121
LL | | let _ = 1;
2222
LL | | n == 5
2323
LL | | });
24-
| |______^ help: use a standard comparison instead: `Some(5) == Some(5)`
24+
| |______^ help: use a standard comparison instead: `(Some(5) == Some(5))`
2525

2626
error: this `map_or` is redundant
2727
--> tests/ui/unnecessary_map_or.rs:18:13
@@ -75,7 +75,7 @@ error: this `map_or` is redundant
7575
--> tests/ui/unnecessary_map_or.rs:27:13
7676
|
7777
LL | let _ = Ok::<i32, i32>(5).map_or(false, |n| n == 5);
78-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `Ok::<i32, i32>(5) == Ok(5)`
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `(Ok::<i32, i32>(5) == Ok(5))`
7979

8080
error: this `map_or` is redundant
8181
--> tests/ui/unnecessary_map_or.rs:28:13

0 commit comments

Comments
 (0)