Skip to content

Fix capacity overflow in single_match with deref patterns #15124

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions clippy_lints/src/matches/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,26 @@ fn report_single_pattern(
}) if lit.node.is_str() || lit.node.is_bytestr() => pat_ref_count + 1,
_ => pat_ref_count,
};
// References are only implicitly added to the pattern, so no overflow here.
// e.g. will work: match &Some(_) { Some(_) => () }
// will not: match Some(_) { &Some(_) => () }
let ref_count_diff = ty_ref_count - pat_ref_count;

// Try to remove address of expressions first.
let (ex, removed) = peel_n_hir_expr_refs(ex, ref_count_diff);
let ref_count_diff = ref_count_diff - removed;
// References are implicitly removed when `deref_patterns` are used.
// They are implicitly added when match ergonomics are used.
let (ex, ref_or_deref_adjust) = if ty_ref_count > pat_ref_count {
let ref_count_diff = ty_ref_count - pat_ref_count;

// Try to remove address of expressions first.
let (ex, removed) = peel_n_hir_expr_refs(ex, ref_count_diff);

(ex, "&".repeat(ref_count_diff - removed))
} else {
(ex, "*".repeat(pat_ref_count - ty_ref_count))
};

let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`";
let sugg = format!(
"if {} == {}{} {}{els_str}",
snippet_with_context(cx, ex.span, ctxt, "..", &mut app).0,
// PartialEq for different reference counts may not exist.
"&".repeat(ref_count_diff),
ref_or_deref_adjust,
snippet_with_applicability(cx, arm.pat.span, "..", &mut app),
expr_block(cx, arm.body, ctxt, "..", Some(expr.span), &mut app),
);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/single_match_else_deref_patterns.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(deref_patterns)]
#![allow(incomplete_features, clippy::eq_op)]
#![warn(clippy::single_match_else)]

fn string() {
if *"" == *"" {}
}
11 changes: 11 additions & 0 deletions tests/ui/single_match_else_deref_patterns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(deref_patterns)]
#![allow(incomplete_features, clippy::eq_op)]
#![warn(clippy::single_match_else)]

fn string() {
match *"" {
//~^ single_match
"" => {},
_ => {},
}
}
16 changes: 16 additions & 0 deletions tests/ui/single_match_else_deref_patterns.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: you seem to be trying to use `match` for an equality check. Consider using `if`
--> tests/ui/single_match_else_deref_patterns.rs:6:5
|
LL | / match *"" {
LL | |
LL | | "" => {},
LL | | _ => {},
LL | | }
| |_____^ help: try: `if *"" == *"" {}`
|
= note: you might want to preserve the comments from inside the `match`
= note: `-D clippy::single-match` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::single_match)]`

error: aborting due to 1 previous error

Loading