mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-19 11:12:43 +00:00
Keep ref
on infallible_destructuring_match
suggestion
This commit is contained in:
parent
3c7f74de0b
commit
93ac0f58bf
@ -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;
|
||||
@ -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);
|
||||
|
||||
@ -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),
|
||||
),
|
||||
|
@ -9,6 +9,9 @@ enum SingleVariantEnum {
|
||||
|
||||
struct TupleStruct(i32);
|
||||
|
||||
struct NonCopy;
|
||||
struct TupleStructWithNonCopy(NonCopy);
|
||||
|
||||
enum EmptyEnum {}
|
||||
|
||||
macro_rules! match_enum {
|
||||
@ -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 {
|
||||
|
@ -9,6 +9,9 @@ enum SingleVariantEnum {
|
||||
|
||||
struct TupleStruct(i32);
|
||||
|
||||
struct NonCopy;
|
||||
struct TupleStructWithNonCopy(NonCopy);
|
||||
|
||||
enum EmptyEnum {}
|
||||
|
||||
macro_rules! match_enum {
|
||||
@ -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 {
|
||||
|
@ -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,
|
||||
@ -9,7 +9,7 @@ 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,
|
||||
@ -17,12 +17,20 @@ 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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user