diff options
| author | Timo <30553356+y21@users.noreply.github.com> | 2024-12-30 14:06:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-30 14:06:30 +0000 |
| commit | 2aea7a080d7d04d433d76772a7b3200c3324e6a1 (patch) | |
| tree | 47dff42f0e9eaf4502ead0916852850aeed6c259 /clippy_lints/src | |
| parent | be49f8613a50a6ecfab6bed3c630ca5226182719 (diff) | |
| parent | e4b11a72665732989fc01f8f140b25469790608b (diff) | |
| download | rust-2aea7a080d7d04d433d76772a7b3200c3324e6a1.tar.gz rust-2aea7a080d7d04d433d76772a7b3200c3324e6a1.zip | |
Fix parentheses when replacing `matches!(…, None)` with `.is_none()` (#13906)
Proper parentheses need to be added to some expressions in receiver position. Fix #13902 changelog: [`redundant_pattern_matching`]: use proper parentheses when suggesting replacing `matches!(…, None)` by `.is_none()`
Diffstat (limited to 'clippy_lints/src')
| -rw-r--r-- | clippy_lints/src/matches/redundant_pattern_match.rs | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/clippy_lints/src/matches/redundant_pattern_match.rs b/clippy_lints/src/matches/redundant_pattern_match.rs index 264458a86ef..897e3f5f7f2 100644 --- a/clippy_lints/src/matches/redundant_pattern_match.rs +++ b/clippy_lints/src/matches/redundant_pattern_match.rs @@ -1,6 +1,6 @@ use super::REDUNDANT_PATTERN_MATCHING; use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; -use clippy_utils::source::{snippet, walk_span_to_context}; +use clippy_utils::source::walk_span_to_context; use clippy_utils::sugg::{Sugg, make_unop}; use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop}; use clippy_utils::visitors::{any_temporaries_need_ordered_drop, for_each_expr_without_closures}; @@ -274,7 +274,9 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op ExprKind::AddrOf(_, _, borrowed) => borrowed, _ => op, }; - let mut sugg = format!("{}.{good_method}", snippet(cx, result_expr.span, "_")); + let mut app = Applicability::MachineApplicable; + let receiver_sugg = Sugg::hir_with_applicability(cx, result_expr, "_", &mut app).maybe_par(); + let mut sugg = format!("{receiver_sugg}.{good_method}"); if let Some(guard) = maybe_guard { // wow, the HIR for match guards in `PAT if let PAT = expr && expr => ...` is annoying! @@ -307,7 +309,7 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op format!("redundant pattern matching, consider using `{good_method}`"), "try", sugg, - Applicability::MachineApplicable, + app, ); } } |
