]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs
Merge commit '4f3ab69ea0a0908260944443c739426cc384ae1a' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / matches / rest_pat_in_fully_bound_struct.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use rustc_hir::{Pat, PatKind, QPath};
3 use rustc_lint::LateContext;
4 use rustc_middle::ty;
5
6 use super::REST_PAT_IN_FULLY_BOUND_STRUCTS;
7
8 pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
9     if_chain! {
10         if !pat.span.from_expansion();
11         if let PatKind::Struct(QPath::Resolved(_, path), fields, true) = pat.kind;
12         if let Some(def_id) = path.res.opt_def_id();
13         let ty = cx.tcx.type_of(def_id);
14         if let ty::Adt(def, _) = ty.kind();
15         if def.is_struct() || def.is_union();
16         if fields.len() == def.non_enum_variant().fields.len();
17         if !def.non_enum_variant().is_field_list_non_exhaustive();
18
19         then {
20             span_lint_and_help(
21                 cx,
22                 REST_PAT_IN_FULLY_BOUND_STRUCTS,
23                 pat.span,
24                 "unnecessary use of `..` pattern in struct binding. All fields were already bound",
25                 None,
26                 "consider removing `..` from this binding",
27             );
28         }
29     }
30 }