|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_help, is_from_proc_macro, path_to_local}; |
| 2 | +use rustc_hir::*; |
| 3 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 4 | +use rustc_middle::{lint::in_external_macro, ty}; |
| 5 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 6 | + |
| 7 | +declare_clippy_lint! { |
| 8 | + /// ### What it does |
| 9 | + /// |
| 10 | + /// ### Why is this bad? |
| 11 | + /// |
| 12 | + /// ### Example |
| 13 | + /// ```rust,ignore |
| 14 | + /// let t1 = &[(1, 2), (3, 4)]; |
| 15 | + /// let v1: Vec<[u32; 2]> = t1.iter().map(|&(a, b)| [a, b]).collect(); |
| 16 | + /// ``` |
| 17 | + /// Use instead: |
| 18 | + /// ```rust,ignore |
| 19 | + /// let t1 = &[(1, 2), (3, 4)]; |
| 20 | + /// let v1: Vec<[u32; 2]> = t1.iter().map(|&t| t.into()).collect(); |
| 21 | + /// ``` |
| 22 | + #[clippy::version = "1.72.0"] |
| 23 | + pub TUPLE_ARRAY_CONVERSIONS, |
| 24 | + complexity, |
| 25 | + "default lint description" |
| 26 | +} |
| 27 | +declare_lint_pass!(TupleArrayConversions => [TUPLE_ARRAY_CONVERSIONS]); |
| 28 | + |
| 29 | +impl LateLintPass<'_> for TupleArrayConversions { |
| 30 | + fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 31 | + if !in_external_macro(cx.sess(), expr.span) { |
| 32 | + _ = check_array(cx, expr) || check_tuple(cx, expr); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool { |
| 38 | + let ExprKind::Array(elements) = expr.kind else { |
| 39 | + return false; |
| 40 | + }; |
| 41 | + if !(1..=12).contains(&elements.len()) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + if let Some(locals) = path_to_locals(cx, elements) |
| 46 | + && locals.iter().all(|local| { |
| 47 | + matches!( |
| 48 | + local, |
| 49 | + Node::Pat(pat) if matches!( |
| 50 | + cx.typeck_results().pat_ty(backtrack_pat(cx, pat)).peel_refs().kind(), |
| 51 | + ty::Tuple(_), |
| 52 | + ), |
| 53 | + ) |
| 54 | + }) |
| 55 | + { |
| 56 | + return emit_lint(cx, expr, ToType::Array); |
| 57 | + } |
| 58 | + |
| 59 | + if let Some(elements) = elements |
| 60 | + .iter() |
| 61 | + .map(|expr| { |
| 62 | + if let ExprKind::Field(path, _) = expr.kind { |
| 63 | + return Some(path); |
| 64 | + }; |
| 65 | + |
| 66 | + None |
| 67 | + }) |
| 68 | + .collect::<Option<Vec<&Expr<'_>>>>() |
| 69 | + && let Some(locals) = path_to_locals(cx, elements) |
| 70 | + && locals.iter().all(|local| { |
| 71 | + matches!( |
| 72 | + local, |
| 73 | + Node::Pat(pat) if matches!( |
| 74 | + cx.typeck_results().pat_ty(backtrack_pat(cx, pat)).peel_refs().kind(), |
| 75 | + ty::Tuple(_), |
| 76 | + ), |
| 77 | + ) |
| 78 | + }) |
| 79 | + { |
| 80 | + return emit_lint(cx, expr, ToType::Array); |
| 81 | + } |
| 82 | + |
| 83 | + false |
| 84 | +} |
| 85 | + |
| 86 | +fn check_tuple<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool { |
| 87 | + let ExprKind::Tup(elements) = expr.kind else { |
| 88 | + return false; |
| 89 | + }; |
| 90 | + if !(1..=12).contains(&elements.len()) { |
| 91 | + return false; |
| 92 | + }; |
| 93 | + if let Some(locals) = path_to_locals(cx, elements) |
| 94 | + && locals.iter().all(|local| { |
| 95 | + matches!( |
| 96 | + local, |
| 97 | + Node::Pat(pat) if matches!( |
| 98 | + cx.typeck_results().pat_ty(backtrack_pat(cx, pat)).peel_refs().kind(), |
| 99 | + ty::Array(_, _), |
| 100 | + ), |
| 101 | + ) |
| 102 | + }) |
| 103 | + { |
| 104 | + return emit_lint(cx, expr, ToType::Tuple); |
| 105 | + } |
| 106 | + |
| 107 | + if let Some(elements) = elements |
| 108 | + .iter() |
| 109 | + .map(|expr| { |
| 110 | + if let ExprKind::Index(path, _) = expr.kind { |
| 111 | + return Some(path); |
| 112 | + }; |
| 113 | + |
| 114 | + None |
| 115 | + }) |
| 116 | + .collect::<Option<Vec<&Expr<'_>>>>() |
| 117 | + && let Some(locals) = path_to_locals(cx, elements.clone()) |
| 118 | + && locals.iter().all(|local| { |
| 119 | + matches!( |
| 120 | + local, |
| 121 | + Node::Pat(pat) if cx.typeck_results() |
| 122 | + .pat_ty(backtrack_pat(cx, pat)) |
| 123 | + .peel_refs() |
| 124 | + .is_array() |
| 125 | + ) |
| 126 | + }) |
| 127 | + { |
| 128 | + return emit_lint(cx, expr, ToType::Tuple); |
| 129 | + } |
| 130 | + |
| 131 | + false |
| 132 | +} |
| 133 | + |
| 134 | +/// Walks up the `Pat` until it's reached the final containing `Pat`. |
| 135 | +fn backtrack_pat<'tcx>(cx: &LateContext<'tcx>, start: &'tcx Pat<'tcx>) -> &'tcx Pat<'tcx> { |
| 136 | + let mut end = start; |
| 137 | + for (_, node) in cx.tcx.hir().parent_iter(start.hir_id) { |
| 138 | + if let Node::Pat(pat) = node { |
| 139 | + end = pat; |
| 140 | + } else { |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + end |
| 145 | +} |
| 146 | + |
| 147 | +fn path_to_locals<'tcx>( |
| 148 | + cx: &LateContext<'tcx>, |
| 149 | + exprs: impl IntoIterator<Item = &'tcx Expr<'tcx>>, |
| 150 | +) -> Option<Vec<Node<'tcx>>> { |
| 151 | + exprs |
| 152 | + .into_iter() |
| 153 | + .map(|element| path_to_local(element).and_then(|local| cx.tcx.hir().find(local))) |
| 154 | + .collect() |
| 155 | +} |
| 156 | + |
| 157 | +#[derive(Clone, Copy)] |
| 158 | +enum ToType { |
| 159 | + Array, |
| 160 | + Tuple, |
| 161 | +} |
| 162 | + |
| 163 | +impl ToType { |
| 164 | + fn help(self) -> &'static str { |
| 165 | + match self { |
| 166 | + ToType::Array => "it looks like you're trying to convert a tuple to an array", |
| 167 | + ToType::Tuple => "it looks like you're trying to convert an array to a tuple", |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +fn emit_lint<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, to_type: ToType) -> bool { |
| 173 | + if !is_from_proc_macro(cx, expr) { |
| 174 | + span_lint_and_help( |
| 175 | + cx, |
| 176 | + TUPLE_ARRAY_CONVERSIONS, |
| 177 | + expr.span, |
| 178 | + to_type.help(), |
| 179 | + None, |
| 180 | + "use `.into()` instead", |
| 181 | + ); |
| 182 | + |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
| 186 | + false |
| 187 | +} |
0 commit comments