Skip to content

Commit 2aea7a0

Browse files
authored
Fix parentheses when replacing matches!(…, None) with .is_none() (rust-lang#13906)
Proper parentheses need to be added to some expressions in receiver position. Fix rust-lang#13902 changelog: [`redundant_pattern_matching`]: use proper parentheses when suggesting replacing `matches!(…, None)` by `.is_none()`
2 parents be49f86 + e4b11a7 commit 2aea7a0

4 files changed

+28
-4
lines changed

clippy_lints/src/matches/redundant_pattern_match.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::REDUNDANT_PATTERN_MATCHING;
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
3-
use clippy_utils::source::{snippet, walk_span_to_context};
3+
use clippy_utils::source::walk_span_to_context;
44
use clippy_utils::sugg::{Sugg, make_unop};
55
use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop};
66
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
274274
ExprKind::AddrOf(_, _, borrowed) => borrowed,
275275
_ => op,
276276
};
277-
let mut sugg = format!("{}.{good_method}", snippet(cx, result_expr.span, "_"));
277+
let mut app = Applicability::MachineApplicable;
278+
let receiver_sugg = Sugg::hir_with_applicability(cx, result_expr, "_", &mut app).maybe_par();
279+
let mut sugg = format!("{receiver_sugg}.{good_method}");
278280

279281
if let Some(guard) = maybe_guard {
280282
// 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
307309
format!("redundant pattern matching, consider using `{good_method}`"),
308310
"try",
309311
sugg,
310-
Applicability::MachineApplicable,
312+
app,
311313
);
312314
}
313315
}

tests/ui/redundant_pattern_matching_option.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ fn issue10803() {
137137
// Don't lint
138138
let _ = matches!(x, Some(16));
139139
}
140+
141+
fn issue13902() {
142+
let x = Some(0);
143+
let p = &raw const x;
144+
unsafe {
145+
let _ = (*p).is_none();
146+
}
147+
}

tests/ui/redundant_pattern_matching_option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,11 @@ fn issue10803() {
164164
// Don't lint
165165
let _ = matches!(x, Some(16));
166166
}
167+
168+
fn issue13902() {
169+
let x = Some(0);
170+
let p = &raw const x;
171+
unsafe {
172+
let _ = matches!(*p, None);
173+
}
174+
}

tests/ui/redundant_pattern_matching_option.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,11 @@ error: redundant pattern matching, consider using `is_none()`
209209
LL | let _ = matches!(x, None);
210210
| ^^^^^^^^^^^^^^^^^ help: try: `x.is_none()`
211211

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

0 commit comments

Comments
 (0)