summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs
blob: d06bcdaa27f0a26699db02bed8bd448187442587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::{Pat, PatKind, QPath};
use rustc_lint::LateContext;
use rustc_middle::ty;

use super::REST_PAT_IN_FULLY_BOUND_STRUCTS;

pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
    if_chain! {
        if !pat.span.from_expansion();
        if let PatKind::Struct(QPath::Resolved(_, path), fields, true) = pat.kind;
        if let Some(def_id) = path.res.opt_def_id();
        let ty = cx.tcx.type_of(def_id).subst_identity();
        if let ty::Adt(def, _) = ty.kind();
        if def.is_struct() || def.is_union();
        if fields.len() == def.non_enum_variant().fields.len();
        if !def.non_enum_variant().is_field_list_non_exhaustive();

        then {
            span_lint_and_help(
                cx,
                REST_PAT_IN_FULLY_BOUND_STRUCTS,
                pat.span,
                "unnecessary use of `..` pattern in struct binding. All fields were already bound",
                None,
                "consider removing `..` from this binding",
            );
        }
    }
}