1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::last_path_segment;
4 intravisit, Body, Expr, ExprKind, FnDecl, HirId, LocalSource, MatchSource, Mutability, Pat, PatField, PatKind,
7 use rustc_lint::{LateContext, LateLintPass, LintContext};
8 use rustc_middle::lint::in_external_macro;
9 use rustc_middle::ty::subst::SubstsRef;
10 use rustc_middle::ty::{AdtDef, FieldDef, Ty, TyKind, VariantDef};
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 use rustc_span::source_map::Span;
15 declare_clippy_lint! {
17 /// Checks for patterns that aren't exact representations of the types
18 /// they are applied to.
20 /// To satisfy this lint, you will have to adjust either the expression that is matched
21 /// against or the pattern itself, as well as the bindings that are introduced by the
22 /// adjusted patterns. For matching you will have to either dereference the expression
23 /// with the `*` operator, or amend the patterns to explicitly match against `&<pattern>`
24 /// or `&mut <pattern>` depending on the reference mutability. For the bindings you need
25 /// to use the inverse. You can leave them as plain bindings if you wish for the value
26 /// to be copied, but you must use `ref mut <variable>` or `ref <variable>` to construct
27 /// a reference into the matched structure.
29 /// If you are looking for a way to learn about ownership semantics in more detail, it
30 /// is recommended to look at IDE options available to you to highlight types, lifetimes
31 /// and reference semantics in your code. The available tooling would expose these things
32 /// in a general way even outside of the various pattern matching mechanics. Of course
33 /// this lint can still be used to highlight areas of interest and ensure a good understanding
34 /// of ownership semantics.
36 /// ### Why is this bad?
37 /// It isn't bad in general. But in some contexts it can be desirable
38 /// because it increases ownership hints in the code, and will guard against some changes
42 /// This example shows the basic adjustments necessary to satisfy the lint. Note how
43 /// the matched expression is explicitly dereferenced with `*` and the `inner` variable
44 /// is bound to a shared borrow via `ref inner`.
48 /// let value = &Some(Box::new(23));
50 /// Some(inner) => println!("{}", inner),
51 /// None => println!("none"),
55 /// let value = &Some(Box::new(23));
57 /// Some(ref inner) => println!("{}", inner),
58 /// None => println!("none"),
62 /// The following example demonstrates one of the advantages of the more verbose style.
63 /// Note how the second version uses `ref mut a` to explicitly declare `a` a shared mutable
64 /// borrow, while `b` is simply taken by value. This ensures that the loop body cannot
65 /// accidentally modify the wrong part of the structure.
69 /// let mut values = vec![(2, 3), (3, 4)];
70 /// for (a, b) in &mut values {
75 /// let mut values = vec![(2, 3), (3, 4)];
76 /// for &mut (ref mut a, b) in &mut values {
80 #[clippy::version = "1.47.0"]
81 pub PATTERN_TYPE_MISMATCH,
83 "type of pattern does not match the expression type"
86 declare_lint_pass!(PatternTypeMismatch => [PATTERN_TYPE_MISMATCH]);
88 impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
89 fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
90 if let StmtKind::Local(local) = stmt.kind {
91 if let Some(init) = &local.init {
92 if let Some(init_ty) = cx.typeck_results().node_type_opt(init.hir_id) {
94 if in_external_macro(cx.sess(), pat.span) {
97 let deref_possible = match local.source {
98 LocalSource::Normal => DerefPossible::Possible,
99 _ => DerefPossible::Impossible,
101 apply_lint(cx, pat, init_ty, deref_possible);
107 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
108 if let ExprKind::Match(scrutinee, arms, MatchSource::Normal) = expr.kind {
109 if let Some(expr_ty) = cx.typeck_results().node_type_opt(scrutinee.hir_id) {
110 'pattern_checks: for arm in arms {
112 if in_external_macro(cx.sess(), pat.span) {
113 continue 'pattern_checks;
115 if apply_lint(cx, pat, expr_ty, DerefPossible::Possible) {
116 break 'pattern_checks;
121 if let ExprKind::Let(let_pat, let_expr, _) = expr.kind {
122 if let Some(expr_ty) = cx.typeck_results().node_type_opt(let_expr.hir_id) {
123 if in_external_macro(cx.sess(), let_pat.span) {
126 apply_lint(cx, let_pat, expr_ty, DerefPossible::Possible);
133 cx: &LateContext<'tcx>,
134 _: intravisit::FnKind<'tcx>,
136 body: &'tcx Body<'_>,
140 if let Some(fn_sig) = cx.typeck_results().liberated_fn_sigs().get(hir_id) {
141 for (param, ty) in iter::zip(body.params, fn_sig.inputs()) {
142 apply_lint(cx, param.pat, ty, DerefPossible::Impossible);
148 #[derive(Debug, Clone, Copy)]
154 fn apply_lint<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'_>, expr_ty: Ty<'tcx>, deref_possible: DerefPossible) -> bool {
155 let maybe_mismatch = find_first_mismatch(cx, pat, expr_ty, Level::Top);
156 if let Some((span, mutability, level)) = maybe_mismatch {
159 PATTERN_TYPE_MISMATCH,
161 "type of pattern does not match the expression type",
164 "{}explicitly match against a `{}` pattern and adjust the enclosed variable bindings",
165 match (deref_possible, level) {
166 (DerefPossible::Possible, Level::Top) => "use `*` to dereference the match expression or ",
170 Mutability::Mut => "&mut _",
171 Mutability::Not => "&_",
181 #[derive(Debug, Copy, Clone)]
187 #[allow(rustc::usage_of_ty_tykind)]
188 fn find_first_mismatch<'tcx>(
189 cx: &LateContext<'tcx>,
193 ) -> Option<(Span, Mutability, Level)> {
194 if let PatKind::Ref(sub_pat, _) = pat.kind {
195 if let TyKind::Ref(_, sub_ty, _) = ty.kind() {
196 return find_first_mismatch(cx, sub_pat, sub_ty, Level::Lower);
200 if let TyKind::Ref(_, _, mutability) = *ty.kind() {
201 if is_non_ref_pattern(&pat.kind) {
202 return Some((pat.span, mutability, level));
206 if let PatKind::Struct(ref qpath, field_pats, _) = pat.kind {
207 if let TyKind::Adt(adt_def, substs_ref) = ty.kind() {
208 if let Some(variant) = get_variant(adt_def, qpath) {
209 let field_defs = &variant.fields;
210 return find_first_mismatch_in_struct(cx, field_pats, field_defs, substs_ref);
215 if let PatKind::TupleStruct(ref qpath, pats, _) = pat.kind {
216 if let TyKind::Adt(adt_def, substs_ref) = ty.kind() {
217 if let Some(variant) = get_variant(adt_def, qpath) {
218 let field_defs = &variant.fields;
219 let ty_iter = field_defs.iter().map(|field_def| field_def.ty(cx.tcx, substs_ref));
220 return find_first_mismatch_in_tuple(cx, pats, ty_iter);
225 if let PatKind::Tuple(pats, _) = pat.kind {
226 if let TyKind::Tuple(..) = ty.kind() {
227 return find_first_mismatch_in_tuple(cx, pats, ty.tuple_fields());
231 if let PatKind::Or(sub_pats) = pat.kind {
232 for pat in sub_pats {
233 let maybe_mismatch = find_first_mismatch(cx, pat, ty, level);
234 if let Some(mismatch) = maybe_mismatch {
235 return Some(mismatch);
243 fn get_variant<'a>(adt_def: &'a AdtDef, qpath: &QPath<'_>) -> Option<&'a VariantDef> {
244 if adt_def.is_struct() {
245 if let Some(variant) = adt_def.variants.iter().next() {
246 return Some(variant);
250 if adt_def.is_enum() {
251 let pat_ident = last_path_segment(qpath).ident;
252 for variant in &adt_def.variants {
253 if variant.ident == pat_ident {
254 return Some(variant);
262 fn find_first_mismatch_in_tuple<'tcx, I>(
263 cx: &LateContext<'tcx>,
266 ) -> Option<(Span, Mutability, Level)>
268 I: IntoIterator<Item = Ty<'tcx>>,
270 let mut field_tys = ty_iter_src.into_iter();
271 'fields: for pat in pats {
272 let field_ty = if let Some(ty) = field_tys.next() {
278 let maybe_mismatch = find_first_mismatch(cx, pat, field_ty, Level::Lower);
279 if let Some(mismatch) = maybe_mismatch {
280 return Some(mismatch);
287 fn find_first_mismatch_in_struct<'tcx>(
288 cx: &LateContext<'tcx>,
289 field_pats: &[PatField<'_>],
290 field_defs: &[FieldDef],
291 substs_ref: SubstsRef<'tcx>,
292 ) -> Option<(Span, Mutability, Level)> {
293 for field_pat in field_pats {
294 'definitions: for field_def in field_defs {
295 if field_pat.ident == field_def.ident {
296 let field_ty = field_def.ty(cx.tcx, substs_ref);
297 let pat = &field_pat.pat;
298 let maybe_mismatch = find_first_mismatch(cx, pat, field_ty, Level::Lower);
299 if let Some(mismatch) = maybe_mismatch {
300 return Some(mismatch);
310 fn is_non_ref_pattern(pat_kind: &PatKind<'_>) -> bool {
312 PatKind::Struct(..) | PatKind::Tuple(..) | PatKind::TupleStruct(..) | PatKind::Path(..) => true,
313 PatKind::Or(sub_pats) => sub_pats.iter().any(|pat| is_non_ref_pattern(&pat.kind)),