Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parentheses when replacing matches!(…, None) with .is_none() #13906

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Loading