Skip to content

Commit dfe37f1

Browse files
committed
Auto merge of #9850 - pheki:fix-7499-missing-ref, r=dswij
Preserve `ref` on `infallible_destructuring_match` suggestion Fixes #7499 changelog: [`infallible_destructuring_match`]: Preserve `ref` on suggestion
2 parents 1c9c34d + 93ac0f5 commit dfe37f1

4 files changed

+42
-7
lines changed

clippy_lints/src/matches/infallible_destructuring_match.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::{path_to_local_id, peel_blocks, strip_pat_refs};
44
use rustc_errors::Applicability;
5-
use rustc_hir::{ExprKind, Local, MatchSource, PatKind, QPath};
5+
use rustc_hir::{ByRef, ExprKind, Local, MatchSource, PatKind, QPath};
66
use rustc_lint::LateContext;
77

88
use super::INFALLIBLE_DESTRUCTURING_MATCH;
@@ -16,7 +16,7 @@ pub(crate) fn check(cx: &LateContext<'_>, local: &Local<'_>) -> bool {
1616
if let PatKind::TupleStruct(
1717
QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind;
1818
if args.len() == 1;
19-
if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind;
19+
if let PatKind::Binding(binding, arg, ..) = strip_pat_refs(&args[0]).kind;
2020
let body = peel_blocks(arms[0].body);
2121
if path_to_local_id(body, arg);
2222

@@ -30,8 +30,9 @@ pub(crate) fn check(cx: &LateContext<'_>, local: &Local<'_>) -> bool {
3030
Consider using `let`",
3131
"try this",
3232
format!(
33-
"let {}({}) = {};",
33+
"let {}({}{}) = {};",
3434
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
35+
if binding.0 == ByRef::Yes { "ref " } else { "" },
3536
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
3637
snippet_with_applicability(cx, target.span, "..", &mut applicability),
3738
),

tests/ui/infallible_destructuring_match.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ enum SingleVariantEnum {
99

1010
struct TupleStruct(i32);
1111

12+
struct NonCopy;
13+
struct TupleStructWithNonCopy(NonCopy);
14+
1215
enum EmptyEnum {}
1316

1417
macro_rules! match_enum {
@@ -71,6 +74,15 @@ fn infallible_destructuring_match_struct() {
7174
let TupleStruct(data) = wrapper;
7275
}
7376

77+
fn infallible_destructuring_match_struct_with_noncopy() {
78+
let wrapper = TupleStructWithNonCopy(NonCopy);
79+
80+
// This should lint! (keeping `ref` in the suggestion)
81+
let TupleStructWithNonCopy(ref data) = wrapper;
82+
83+
let TupleStructWithNonCopy(ref data) = wrapper;
84+
}
85+
7486
macro_rules! match_never_enum {
7587
($param:expr) => {
7688
let data = match $param {

tests/ui/infallible_destructuring_match.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ enum SingleVariantEnum {
99

1010
struct TupleStruct(i32);
1111

12+
struct NonCopy;
13+
struct TupleStructWithNonCopy(NonCopy);
14+
1215
enum EmptyEnum {}
1316

1417
macro_rules! match_enum {
@@ -75,6 +78,17 @@ fn infallible_destructuring_match_struct() {
7578
let TupleStruct(data) = wrapper;
7679
}
7780

81+
fn infallible_destructuring_match_struct_with_noncopy() {
82+
let wrapper = TupleStructWithNonCopy(NonCopy);
83+
84+
// This should lint! (keeping `ref` in the suggestion)
85+
let data = match wrapper {
86+
TupleStructWithNonCopy(ref n) => n,
87+
};
88+
89+
let TupleStructWithNonCopy(ref data) = wrapper;
90+
}
91+
7892
macro_rules! match_never_enum {
7993
($param:expr) => {
8094
let data = match $param {
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
2-
--> $DIR/infallible_destructuring_match.rs:26:5
2+
--> $DIR/infallible_destructuring_match.rs:29:5
33
|
44
LL | / let data = match wrapper {
55
LL | | SingleVariantEnum::Variant(i) => i,
@@ -9,20 +9,28 @@ LL | | };
99
= note: `-D clippy::infallible-destructuring-match` implied by `-D warnings`
1010

1111
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
12-
--> $DIR/infallible_destructuring_match.rs:58:5
12+
--> $DIR/infallible_destructuring_match.rs:61:5
1313
|
1414
LL | / let data = match wrapper {
1515
LL | | TupleStruct(i) => i,
1616
LL | | };
1717
| |______^ help: try this: `let TupleStruct(data) = wrapper;`
1818

1919
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
20-
--> $DIR/infallible_destructuring_match.rs:90:5
20+
--> $DIR/infallible_destructuring_match.rs:85:5
21+
|
22+
LL | / let data = match wrapper {
23+
LL | | TupleStructWithNonCopy(ref n) => n,
24+
LL | | };
25+
| |______^ help: try this: `let TupleStructWithNonCopy(ref data) = wrapper;`
26+
27+
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
28+
--> $DIR/infallible_destructuring_match.rs:104:5
2129
|
2230
LL | / let data = match wrapper {
2331
LL | | Ok(i) => i,
2432
LL | | };
2533
| |______^ help: try this: `let Ok(data) = wrapper;`
2634

27-
error: aborting due to 3 previous errors
35+
error: aborting due to 4 previous errors
2836

0 commit comments

Comments
 (0)