|
| 1 | +use clippy_utils::{ |
| 2 | + diagnostics::span_lint_and_then, |
| 3 | + get_parent_expr, |
| 4 | + higher::VecArgs, |
| 5 | + is_diagnostic_item_or_ctor, is_lang_item_or_ctor, last_path_segment, |
| 6 | + macros::root_macro_call_first_node, |
| 7 | + msrvs::{Msrv, ITER_ONCE_AND_EMPTY}, |
| 8 | + path_res, |
| 9 | + source::snippet_opt, |
| 10 | + std_or_core, |
| 11 | +}; |
| 12 | +use rustc_errors::{Applicability, Diagnostic}; |
| 13 | +use rustc_hir::{Expr, ExprKind, GenericArg, LangItem}; |
| 14 | +use rustc_lint::{LateContext, Lint, LintContext}; |
| 15 | +use rustc_middle::{ |
| 16 | + lint::in_external_macro, |
| 17 | + ty::{Clause, PredicateKind}, |
| 18 | +}; |
| 19 | +use rustc_span::{sym, Span}; |
| 20 | + |
| 21 | +use super::{ITER_ON_EMPTY_COLLECTIONS, ITER_ON_SINGLE_ITEMS}; |
| 22 | + |
| 23 | +#[derive(Clone, Copy, Debug)] |
| 24 | +enum Variant<'tcx> { |
| 25 | + SomeToOnce, |
| 26 | + NoneToEmpty, |
| 27 | + OneLenToOnce(&'tcx Expr<'tcx>), |
| 28 | + ZeroLenToEmpty, |
| 29 | +} |
| 30 | + |
| 31 | +impl<'tcx> Variant<'tcx> { |
| 32 | + fn as_lint(self) -> &'static Lint { |
| 33 | + match self { |
| 34 | + Self::SomeToOnce | Self::OneLenToOnce(_) => ITER_ON_SINGLE_ITEMS, |
| 35 | + Self::NoneToEmpty | Self::ZeroLenToEmpty => ITER_ON_EMPTY_COLLECTIONS, |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fn as_str(self) -> &'static str { |
| 40 | + match self { |
| 41 | + Self::SomeToOnce => "Some", |
| 42 | + Self::NoneToEmpty => "None", |
| 43 | + Self::OneLenToOnce(_) => "[T; 1]", |
| 44 | + Self::ZeroLenToEmpty => "[T; 0]", |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fn desc(self) -> &'static str { |
| 49 | + match self { |
| 50 | + Self::SomeToOnce | Self::OneLenToOnce(_) => "iterator with only one element", |
| 51 | + Self::NoneToEmpty | Self::ZeroLenToEmpty => "empty iterator", |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + fn sugg_fn(self, cx: &LateContext<'_>) -> Option<String> { |
| 56 | + Some(format!( |
| 57 | + "{}::iter::{}", |
| 58 | + std_or_core(cx)?, |
| 59 | + match self { |
| 60 | + Self::SomeToOnce | Self::OneLenToOnce(_) => "once", |
| 61 | + Self::NoneToEmpty | Self::ZeroLenToEmpty => "empty", |
| 62 | + }, |
| 63 | + )) |
| 64 | + } |
| 65 | + |
| 66 | + fn turbofish_or_args_snippet( |
| 67 | + self, |
| 68 | + cx: &LateContext<'_>, |
| 69 | + expr: &Expr<'_>, |
| 70 | + method_name: Option<&str>, |
| 71 | + ) -> Option<String> { |
| 72 | + let ref_prefix = ref_prefix(method_name.unwrap_or("")); |
| 73 | + |
| 74 | + match self { |
| 75 | + Self::SomeToOnce if let ExprKind::Call(_, [arg]) = expr.kind => { |
| 76 | + snippet_opt(cx, arg.span).map(|s| format!("({ref_prefix}{s})")) |
| 77 | + }, |
| 78 | + Self::NoneToEmpty if let ExprKind::Path(qpath) = expr.kind |
| 79 | + && let Some(args) = last_path_segment(&qpath).args |
| 80 | + && let [GenericArg::Type(ty)] = args.args => |
| 81 | + { |
| 82 | + snippet_opt(cx, ty.span).map(|s| format!("::<{s}>()")) |
| 83 | + }, |
| 84 | + Self::OneLenToOnce(one) => { |
| 85 | + snippet_opt(cx, one.span).map(|s| format!("({ref_prefix}{s})")) |
| 86 | + } |
| 87 | + Self::NoneToEmpty | Self::ZeroLenToEmpty => Some("()".to_owned()), |
| 88 | + Self::SomeToOnce => None, |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, msrv: &Msrv) { |
| 94 | + if !msrv.meets(ITER_ONCE_AND_EMPTY) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + let (variant, is_vec, sugg_span) = match expr.kind { |
| 99 | + // `[T; 1]` |
| 100 | + ExprKind::Array([one]) => (Variant::OneLenToOnce(one), false, expr.span), |
| 101 | + // `[T; 0]` |
| 102 | + ExprKind::Array([]) => (Variant::ZeroLenToEmpty, false, expr.span), |
| 103 | + // `Some` |
| 104 | + ExprKind::Call(path, _) if let Some(def_id) = path_res(cx, path).opt_def_id() |
| 105 | + && is_lang_item_or_ctor(cx, def_id, LangItem::OptionSome) => |
| 106 | + { |
| 107 | + (Variant::SomeToOnce, false, expr.span) |
| 108 | + }, |
| 109 | + // `None` |
| 110 | + ExprKind::Path(qpath) if let Some(def_id) = cx.qpath_res(&qpath, expr.hir_id).opt_def_id() |
| 111 | + && is_lang_item_or_ctor(cx, def_id, LangItem::OptionNone) => |
| 112 | + { |
| 113 | + (Variant::NoneToEmpty, false, expr.span) |
| 114 | + } |
| 115 | + // `vec![]` |
| 116 | + _ if let Some(mac_call) = root_macro_call_first_node(cx, expr) |
| 117 | + && let Some(VecArgs::Vec(elems)) = VecArgs::hir(cx, expr) => |
| 118 | + { |
| 119 | + if let [one] = elems { |
| 120 | + (Variant::OneLenToOnce(one), true, mac_call.span.source_callsite()) |
| 121 | + } else if elems.is_empty() { |
| 122 | + (Variant::ZeroLenToEmpty, true, mac_call.span.source_callsite()) |
| 123 | + } else { |
| 124 | + return; |
| 125 | + } |
| 126 | + }, |
| 127 | + _ => return, |
| 128 | + }; |
| 129 | + |
| 130 | + // `vec![]` must be external |
| 131 | + if !is_vec && in_external_macro(cx.sess(), expr.span) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // FIXME: `get_expr_use_or_unification_node` doesn't work here. Even with the exact same check |
| 136 | + // that the old code used. Please help |
| 137 | + |
| 138 | + if let Some(parent) = get_parent_expr(cx, expr) |
| 139 | + && let ExprKind::MethodCall(path, recv, args, _) = parent |
| 140 | + .peel_blocks() |
| 141 | + .peel_borrows() |
| 142 | + .kind |
| 143 | + { |
| 144 | + if recv.hir_id == expr.hir_id |
| 145 | + && let method_name = path.ident.as_str() |
| 146 | + && matches!(method_name, "iter" | "iter_mut" | "into_iter") |
| 147 | + { |
| 148 | + emit_lint(cx, expr, Some(path.ident.as_str()), parent.span, variant, |_| {}); |
| 149 | + } else if args.iter().any(|arg| arg.hir_id == expr.hir_id) |
| 150 | + && let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id) |
| 151 | + { |
| 152 | + for predicate in cx.tcx.param_env(def_id).caller_bounds() { |
| 153 | + match predicate.kind().skip_binder() { |
| 154 | + // FIXME: This doesn't take into account whether this `Clause` is related |
| 155 | + // to `arg`, afaik |
| 156 | + PredicateKind::Clause(Clause::Trait(trait_predicate)) |
| 157 | + if is_diagnostic_item_or_ctor(cx, trait_predicate.def_id(), sym::Iterator) => |
| 158 | + { |
| 159 | + emit_lint(cx, expr, None, sugg_span, variant, |diag| { |
| 160 | + diag.note("this method is generic over `Iterator`"); |
| 161 | + }); |
| 162 | + }, |
| 163 | + _ => {}, |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + {} |
| 169 | +} |
| 170 | + |
| 171 | +fn emit_lint<'tcx>( |
| 172 | + cx: &LateContext<'tcx>, |
| 173 | + expr: &Expr<'tcx>, |
| 174 | + method_name: Option<&str>, |
| 175 | + sugg_span: Span, |
| 176 | + variant: Variant<'_>, |
| 177 | + f: impl FnOnce(&mut Diagnostic), |
| 178 | +) { |
| 179 | + let Some(sugg_fn) = variant.sugg_fn(cx) else { |
| 180 | + return; |
| 181 | + }; |
| 182 | + let Some(turbofish_or_args) = variant.turbofish_or_args_snippet(cx, expr, method_name) else { |
| 183 | + return; |
| 184 | + }; |
| 185 | + |
| 186 | + span_lint_and_then( |
| 187 | + cx, |
| 188 | + variant.as_lint(), |
| 189 | + expr.span.source_callsite(), |
| 190 | + &format!("usage of `{}` to create an {}", variant.as_str(), variant.desc()), |
| 191 | + |diag| { |
| 192 | + diag.span_suggestion( |
| 193 | + sugg_span, |
| 194 | + format!("use `{sugg_fn}` instead"), |
| 195 | + format!("{sugg_fn}{turbofish_or_args}"), |
| 196 | + Applicability::MachineApplicable, |
| 197 | + ); |
| 198 | + f(diag); |
| 199 | + }, |
| 200 | + ); |
| 201 | +} |
| 202 | + |
| 203 | +fn ref_prefix(method_name: &str) -> &str { |
| 204 | + match method_name { |
| 205 | + "iter" => "&", |
| 206 | + "iter_mut" => "&mut ", |
| 207 | + _ => "", |
| 208 | + } |
| 209 | +} |
0 commit comments