Skip to content

Preserve ref on infallible_destructuring_match suggestion #9850

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
Nov 18, 2022
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
7 changes: 4 additions & 3 deletions clippy_lints/src/matches/infallible_destructuring_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{path_to_local_id, peel_blocks, strip_pat_refs};
use rustc_errors::Applicability;
use rustc_hir::{ExprKind, Local, MatchSource, PatKind, QPath};
use rustc_hir::{ByRef, ExprKind, Local, MatchSource, PatKind, QPath};
use rustc_lint::LateContext;

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

Expand All @@ -30,8 +30,9 @@ pub(crate) fn check(cx: &LateContext<'_>, local: &Local<'_>) -> bool {
Consider using `let`",
"try this",
format!(
"let {}({}) = {};",
"let {}({}{}) = {};",
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
if binding.0 == ByRef::Yes { "ref " } else { "" },
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
snippet_with_applicability(cx, target.span, "..", &mut applicability),
),
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/infallible_destructuring_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ enum SingleVariantEnum {

struct TupleStruct(i32);

struct NonCopy;
struct TupleStructWithNonCopy(NonCopy);

enum EmptyEnum {}

macro_rules! match_enum {
Expand Down Expand Up @@ -71,6 +74,15 @@ fn infallible_destructuring_match_struct() {
let TupleStruct(data) = wrapper;
}

fn infallible_destructuring_match_struct_with_noncopy() {
let wrapper = TupleStructWithNonCopy(NonCopy);

// This should lint! (keeping `ref` in the suggestion)
let TupleStructWithNonCopy(ref data) = wrapper;

let TupleStructWithNonCopy(ref data) = wrapper;
}

macro_rules! match_never_enum {
($param:expr) => {
let data = match $param {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/infallible_destructuring_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ enum SingleVariantEnum {

struct TupleStruct(i32);

struct NonCopy;
struct TupleStructWithNonCopy(NonCopy);

enum EmptyEnum {}

macro_rules! match_enum {
Expand Down Expand Up @@ -75,6 +78,17 @@ fn infallible_destructuring_match_struct() {
let TupleStruct(data) = wrapper;
}

fn infallible_destructuring_match_struct_with_noncopy() {
let wrapper = TupleStructWithNonCopy(NonCopy);

// This should lint! (keeping `ref` in the suggestion)
let data = match wrapper {
TupleStructWithNonCopy(ref n) => n,
};

let TupleStructWithNonCopy(ref data) = wrapper;
}

macro_rules! match_never_enum {
($param:expr) => {
let data = match $param {
Expand Down
16 changes: 12 additions & 4 deletions tests/ui/infallible_destructuring_match.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
--> $DIR/infallible_destructuring_match.rs:26:5
--> $DIR/infallible_destructuring_match.rs:29:5
|
LL | / let data = match wrapper {
LL | | SingleVariantEnum::Variant(i) => i,
Expand All @@ -9,20 +9,28 @@ LL | | };
= note: `-D clippy::infallible-destructuring-match` implied by `-D warnings`

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

error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
--> $DIR/infallible_destructuring_match.rs:90:5
--> $DIR/infallible_destructuring_match.rs:85:5
|
LL | / let data = match wrapper {
LL | | TupleStructWithNonCopy(ref n) => n,
LL | | };
| |______^ help: try this: `let TupleStructWithNonCopy(ref data) = wrapper;`

error: you seem to be trying to use `match` to destructure a single infallible pattern. Consider using `let`
--> $DIR/infallible_destructuring_match.rs:104:5
|
LL | / let data = match wrapper {
LL | | Ok(i) => i,
LL | | };
| |______^ help: try this: `let Ok(data) = wrapper;`

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors