Skip to content

Properly handle expansion in single_match #14495

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

Merged
merged 1 commit into from
Mar 31, 2025
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
17 changes: 9 additions & 8 deletions clippy_lints/src/matches/single_match.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::{SpanRangeExt, expr_block, snippet, snippet_block_with_context};
use clippy_utils::source::{
SpanRangeExt, expr_block, snippet, snippet_block_with_context, snippet_with_applicability, snippet_with_context,
};
use clippy_utils::ty::implements_trait;
use clippy_utils::{
is_lint_allowed, is_unit_expr, peel_blocks, peel_hir_pat_refs, peel_middle_ty_refs, peel_n_hir_expr_refs,
Expand Down Expand Up @@ -34,8 +36,7 @@ fn empty_arm_has_comment(cx: &LateContext<'_>, span: Span) -> bool {
#[rustfmt::skip]
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>], expr: &'tcx Expr<'_>, contains_comments: bool) {
if let [arm1, arm2] = arms
&& arm1.guard.is_none()
&& arm2.guard.is_none()
&& !arms.iter().any(|arm| arm.guard.is_some() || arm.pat.span.from_expansion())
&& !expr.span.from_expansion()
// don't lint for or patterns for now, this makes
// the lint noisy in unnecessary situations
Expand Down Expand Up @@ -106,7 +107,7 @@ fn report_single_pattern(
format!(" else {}", expr_block(cx, els, ctxt, "..", Some(expr.span), &mut app))
});

if snippet(cx, ex.span, "..") == snippet(cx, arm.pat.span, "..") {
if ex.span.eq_ctxt(expr.span) && snippet(cx, ex.span, "..") == snippet(cx, arm.pat.span, "..") {
let msg = "this pattern is irrefutable, `match` is useless";
let (sugg, help) = if is_unit_expr(arm.body) {
(String::new(), "`match` expression can be removed")
Expand Down Expand Up @@ -163,19 +164,19 @@ fn report_single_pattern(
let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`";
let sugg = format!(
"if {} == {}{} {}{els_str}",
snippet(cx, ex.span, ".."),
snippet_with_context(cx, ex.span, ctxt, "..", &mut app).0,
// PartialEq for different reference counts may not exist.
"&".repeat(ref_count_diff),
snippet(cx, arm.pat.span, ".."),
snippet_with_applicability(cx, arm.pat.span, "..", &mut app),
expr_block(cx, arm.body, ctxt, "..", Some(expr.span), &mut app),
);
(msg, sugg)
} else {
let msg = "you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`";
let sugg = format!(
"if let {} = {} {}{els_str}",
snippet(cx, arm.pat.span, ".."),
snippet(cx, ex.span, ".."),
snippet_with_applicability(cx, arm.pat.span, "..", &mut app),
snippet_with_context(cx, ex.span, ctxt, "..", &mut app).0,
expr_block(cx, arm.body, ctxt, "..", Some(expr.span), &mut app),
);
(msg, sugg)
Expand Down
36 changes: 36 additions & 0 deletions tests/ui/single_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,39 @@ fn irrefutable_match() {
//~^^^^^^^^^ single_match
//~| NOTE: you might want to preserve the comments from inside the `match`
}

fn issue_14493() {
macro_rules! mac {
(some) => {
Some(42)
};
(any) => {
_
};
(str) => {
"foo"
};
}

if let Some(u) = mac!(some) { println!("{u}") }
//~^^^^ single_match

// When scrutinee comes from macro, do not tell that arm will always match
// and suggest an equality check instead.
if mac!(str) == "foo" { println!("eq") }
//~^^^^ ERROR: for an equality check

// Do not lint if any match arm come from expansion
match Some(0) {
mac!(some) => println!("eq"),
mac!(any) => println!("neq"),
}
match Some(0) {
Some(42) => println!("eq"),
mac!(any) => println!("neq"),
}
match Some(0) {
mac!(some) => println!("eq"),
_ => println!("neq"),
}
}
42 changes: 42 additions & 0 deletions tests/ui/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,45 @@ fn irrefutable_match() {
//~^^^^^^^^^ single_match
//~| NOTE: you might want to preserve the comments from inside the `match`
}

fn issue_14493() {
macro_rules! mac {
(some) => {
Some(42)
};
(any) => {
_
};
(str) => {
"foo"
};
}

match mac!(some) {
Some(u) => println!("{u}"),
_ => (),
}
//~^^^^ single_match

// When scrutinee comes from macro, do not tell that arm will always match
// and suggest an equality check instead.
match mac!(str) {
"foo" => println!("eq"),
_ => (),
}
//~^^^^ ERROR: for an equality check

// Do not lint if any match arm come from expansion
match Some(0) {
mac!(some) => println!("eq"),
mac!(any) => println!("neq"),
}
match Some(0) {
Some(42) => println!("eq"),
mac!(any) => println!("neq"),
}
match Some(0) {
mac!(some) => println!("eq"),
_ => println!("neq"),
}
}
20 changes: 19 additions & 1 deletion tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,23 @@ LL + println!("{u}");
LL + }
|

error: aborting due to 29 previous errors
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> tests/ui/single_match.rs:478:5
|
LL | / match mac!(some) {
LL | | Some(u) => println!("{u}"),
LL | | _ => (),
LL | | }
| |_____^ help: try: `if let Some(u) = mac!(some) { println!("{u}") }`

error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match.rs:486:5
|
LL | / match mac!(str) {
LL | | "foo" => println!("eq"),
LL | | _ => (),
LL | | }
| |_____^ help: try: `if mac!(str) == "foo" { println!("eq") }`

error: aborting due to 31 previous errors