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