diff --git a/clippy_lints/src/matches/match_like_matches.rs b/clippy_lints/src/matches/match_like_matches.rs
index b5f631e8fea3..89411115f730 100644
--- a/clippy_lints/src/matches/match_like_matches.rs
+++ b/clippy_lints/src/matches/match_like_matches.rs
@@ -1,7 +1,8 @@
//! Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
use super::REDUNDANT_PATTERN_MATCHING;
-use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::higher::has_let_expr;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{is_lint_allowed, is_wild, span_contains_comment};
use rustc_ast::LitKind;
@@ -43,18 +44,23 @@ pub(crate) fn check_if_let<'tcx>(
{
ex_new = ex_inner;
}
- span_lint_and_sugg(
+ span_lint_and_then(
cx,
MATCH_LIKE_MATCHES_MACRO,
expr.span,
- "if let .. else expression looks like `matches!` macro",
- "try",
- format!(
- "{}matches!({}, {pat})",
- if b0 { "" } else { "!" },
- snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
- ),
- applicability,
+ "`if let .. else` expression looks like `matches!` macro",
+ |diag| {
+ diag.span_suggestion_verbose(
+ expr.span,
+ "use `matches!` directly",
+ format!(
+ "{}matches!({}, {pat})",
+ if b0 { "" } else { "!" },
+ snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
+ ),
+ applicability,
+ );
+ },
);
}
}
@@ -87,7 +93,10 @@ pub(super) fn check_match<'tcx>(
// ```rs
// matches!(e, Either::Left $(if $guard)|+)
// ```
- middle_arms.is_empty()
+ //
+ // But if the guard _is_ present, it may not be an `if-let` guard, as `matches!` doesn't
+ // support these (currently?)
+ (middle_arms.is_empty() && first_arm.guard.is_none_or(|g| !has_let_expr(g)))
// - (added in #6216) There are middle arms
//
@@ -169,18 +178,23 @@ pub(super) fn check_match<'tcx>(
{
ex_new = ex_inner;
}
- span_lint_and_sugg(
+ span_lint_and_then(
cx,
MATCH_LIKE_MATCHES_MACRO,
e.span,
"match expression looks like `matches!` macro",
- "try",
- format!(
- "{}matches!({}, {pat_and_guard})",
- if b0 { "" } else { "!" },
- snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
- ),
- applicability,
+ |diag| {
+ diag.span_suggestion_verbose(
+ e.span,
+ "use `matches!` directly",
+ format!(
+ "{}matches!({}, {pat_and_guard})",
+ if b0 { "" } else { "!" },
+ snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
+ ),
+ applicability,
+ );
+ },
);
true
} else {
diff --git a/tests/ui/match_like_matches_macro.fixed b/tests/ui/match_like_matches_macro.fixed
index a1c95e8a94f1..dad59c1ce6e4 100644
--- a/tests/ui/match_like_matches_macro.fixed
+++ b/tests/ui/match_like_matches_macro.fixed
@@ -223,3 +223,10 @@ fn msrv_1_42() {
let _y = matches!(Some(5), Some(0));
//~^^^^ match_like_matches_macro
}
+
+#[expect(clippy::option_option)]
+fn issue15841(opt: Option