Skip to content

Commit

Permalink
Fix parentheses when replacing matches!(…, None) with .is_none() (#…
Browse files Browse the repository at this point in the history
…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()`
  • Loading branch information
y21 authored Dec 30, 2024
2 parents be49f86 + e4b11a7 commit 2aea7a0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
8 changes: 5 additions & 3 deletions clippy_lints/src/matches/redundant_pattern_match.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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,
);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/redundant_pattern_matching_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ fn issue10803() {
// Don't lint
let _ = matches!(x, Some(16));
}

fn issue13902() {
let x = Some(0);
let p = &raw const x;
unsafe {
let _ = (*p).is_none();
}
}
8 changes: 8 additions & 0 deletions tests/ui/redundant_pattern_matching_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,11 @@ fn issue10803() {
// Don't lint
let _ = matches!(x, Some(16));
}

fn issue13902() {
let x = Some(0);
let p = &raw const x;
unsafe {
let _ = matches!(*p, None);
}
}
8 changes: 7 additions & 1 deletion tests/ui/redundant_pattern_matching_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,11 @@ error: redundant pattern matching, consider using `is_none()`
LL | let _ = matches!(x, None);
| ^^^^^^^^^^^^^^^^^ help: try: `x.is_none()`

error: aborting due to 30 previous errors
error: redundant pattern matching, consider using `is_none()`
--> tests/ui/redundant_pattern_matching_option.rs:172:17
|
LL | let _ = matches!(*p, None);
| ^^^^^^^^^^^^^^^^^^ help: try: `(*p).is_none()`

error: aborting due to 31 previous errors

0 comments on commit 2aea7a0

Please sign in to comment.