|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::last_path_segment; |
| 3 | +use clippy_utils::source::snippet_with_applicability; |
| 4 | +use clippy_utils::{match_def_path, paths}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// It checks for `std::iter::Empty::default()` and suggests replacing it with |
| 13 | + /// `std::iter::empty()`. |
| 14 | + /// ### Why is this bad? |
| 15 | + /// `std::iter::empty()` is the more idiomatic way. |
| 16 | + /// ### Example |
| 17 | + /// ```rust |
| 18 | + /// let _ = std::iter::Empty::<usize>::default(); |
| 19 | + /// let iter: std::iter::Empty<usize> = std::iter::Empty::default(); |
| 20 | + /// ``` |
| 21 | + /// Use instead: |
| 22 | + /// ```rust |
| 23 | + /// let _ = std::iter::empty::<usize>(); |
| 24 | + /// let iter: std::iter::Empty<usize> = std::iter::empty(); |
| 25 | + /// ``` |
| 26 | + #[clippy::version = "1.63.0"] |
| 27 | + pub DEFAULT_INSTEAD_OF_ITER_EMPTY, |
| 28 | + style, |
| 29 | + "check `std::iter::Empty::default()` and replace with `std::iter::empty()`" |
| 30 | +} |
| 31 | +declare_lint_pass!(DefaultIterEmpty => [DEFAULT_INSTEAD_OF_ITER_EMPTY]); |
| 32 | + |
| 33 | +impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty { |
| 34 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 35 | + if let ExprKind::Call(iter_expr, []) = &expr.kind |
| 36 | + && let ExprKind::Path(QPath::TypeRelative(ty, _)) = &iter_expr.kind |
| 37 | + && let TyKind::Path(ty_path) = &ty.kind |
| 38 | + && let QPath::Resolved(None, path) = ty_path |
| 39 | + && let def::Res::Def(_, def_id) = &path.res |
| 40 | + && match_def_path(cx, *def_id, &paths::ITER_EMPTY) |
| 41 | + { |
| 42 | + let mut applicability = Applicability::MachineApplicable; |
| 43 | + let sugg = make_sugg(cx, ty_path, &mut applicability); |
| 44 | + span_lint_and_sugg( |
| 45 | + cx, |
| 46 | + DEFAULT_INSTEAD_OF_ITER_EMPTY, |
| 47 | + expr.span, |
| 48 | + "`std::iter::empty()` is the more idiomatic way", |
| 49 | + "try", |
| 50 | + sugg, |
| 51 | + applicability, |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn make_sugg(cx: &LateContext<'_>, ty_path: &rustc_hir::QPath<'_>, applicability: &mut Applicability) -> String { |
| 58 | + if let Some(last) = last_path_segment(ty_path).args |
| 59 | + && let Some(iter_ty) = last.args.iter().find_map(|arg| match arg { |
| 60 | + GenericArg::Type(ty) => Some(ty), |
| 61 | + _ => None, |
| 62 | + }) |
| 63 | + { |
| 64 | + format!("std::iter::empty::<{}>()", snippet_with_applicability(cx, iter_ty.span, "..", applicability)) |
| 65 | + } else { |
| 66 | + "std::iter::empty()".to_owned() |
| 67 | + } |
| 68 | +} |
0 commit comments