|
| 1 | +use std::borrow::Cow; |
| 2 | + |
1 | 3 | use clippy_config::msrvs::{self, Msrv}; |
2 | 4 | use clippy_utils::diagnostics::span_lint_and_sugg; |
3 | 5 | 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}; |
5 | 8 | use clippy_utils::ty::get_type_diagnostic_name; |
6 | 9 | 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}; |
8 | 11 | use rustc_ast::LitKind::Bool; |
9 | | -use rustc_ast::util::parser::AssocOp; |
10 | 12 | use rustc_errors::Applicability; |
11 | 13 | use rustc_hir::{BinOpKind, Expr, ExprKind, PatKind}; |
12 | 14 | use rustc_lint::LateContext; |
@@ -80,26 +82,23 @@ pub(super) fn check<'a>( |
80 | 82 | && typeck_results.expr_ty(l) == typeck_results.expr_ty(r) |
81 | 83 | { |
82 | 84 | let wrap = variant.variant_name(); |
83 | | - let comparator = op.node.as_str(); |
84 | 85 |
|
85 | 86 | // we may need to add parens around the suggestion |
86 | 87 | // in case the parent expression has additional method calls, |
87 | 88 | // since for example `Some(5).map_or(false, |x| x == 5).then(|| 1)` |
88 | 89 | // being converted to `Some(5) == Some(5).then(|| 1)` isnt |
89 | 90 | // the same thing |
90 | 91 |
|
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") |
103 | 102 | } else if !def_bool |
104 | 103 | && msrv.meets(msrvs::OPTION_RESULT_IS_VARIANT_AND) |
105 | 104 | && let Some(recv_callsite) = snippet_opt(cx, recv.span.source_callsite()) |
|
0 commit comments