2022-06-30 08:50:09 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2023-03-02 19:29:45 +00:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2024-01-15 19:53:00 +00:00
|
|
|
use clippy_utils::{last_path_segment, std_or_core};
|
2022-06-30 08:50:09 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-09-27 03:56:38 +00:00
|
|
|
use rustc_span::{sym, SyntaxContext};
|
2022-06-30 08:50:09 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// It checks for `std::iter::Empty::default()` and suggests replacing it with
|
|
|
|
/// `std::iter::empty()`.
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// `std::iter::empty()` is the more idiomatic way.
|
|
|
|
/// ### Example
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2022-06-30 08:50:09 +00:00
|
|
|
/// let _ = std::iter::Empty::<usize>::default();
|
|
|
|
/// let iter: std::iter::Empty<usize> = std::iter::Empty::default();
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2022-06-30 08:50:09 +00:00
|
|
|
/// let _ = std::iter::empty::<usize>();
|
|
|
|
/// let iter: std::iter::Empty<usize> = std::iter::empty();
|
|
|
|
/// ```
|
2022-10-06 07:44:38 +00:00
|
|
|
#[clippy::version = "1.64.0"]
|
2022-06-30 08:50:09 +00:00
|
|
|
pub DEFAULT_INSTEAD_OF_ITER_EMPTY,
|
|
|
|
style,
|
|
|
|
"check `std::iter::Empty::default()` and replace with `std::iter::empty()`"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(DefaultIterEmpty => [DEFAULT_INSTEAD_OF_ITER_EMPTY]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|
|
|
if let ExprKind::Call(iter_expr, []) = &expr.kind
|
|
|
|
&& let ExprKind::Path(QPath::TypeRelative(ty, _)) = &iter_expr.kind
|
|
|
|
&& let TyKind::Path(ty_path) = &ty.kind
|
|
|
|
&& let QPath::Resolved(None, path) = ty_path
|
|
|
|
&& let def::Res::Def(_, def_id) = &path.res
|
2023-09-27 03:56:38 +00:00
|
|
|
&& cx.tcx.is_diagnostic_item(sym::IterEmpty, *def_id)
|
2023-03-10 09:53:50 +00:00
|
|
|
&& let ctxt = expr.span.ctxt()
|
|
|
|
&& ty.span.ctxt() == ctxt
|
2022-06-30 08:50:09 +00:00
|
|
|
{
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2024-01-15 19:53:00 +00:00
|
|
|
let Some(path) = std_or_core(cx) else { return };
|
|
|
|
let path = format!("{path}::iter::empty");
|
|
|
|
let sugg = make_sugg(cx, ty_path, ctxt, &mut applicability, &path);
|
2022-06-30 08:50:09 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
DEFAULT_INSTEAD_OF_ITER_EMPTY,
|
|
|
|
expr.span,
|
2024-03-23 05:52:11 +00:00
|
|
|
format!("`{path}()` is the more idiomatic way"),
|
2022-06-30 08:50:09 +00:00
|
|
|
"try",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 09:53:50 +00:00
|
|
|
fn make_sugg(
|
|
|
|
cx: &LateContext<'_>,
|
2024-03-18 21:28:43 +00:00
|
|
|
ty_path: &QPath<'_>,
|
2023-03-10 09:53:50 +00:00
|
|
|
ctxt: SyntaxContext,
|
|
|
|
applicability: &mut Applicability,
|
2024-01-14 13:13:04 +00:00
|
|
|
path: &str,
|
2023-03-10 09:53:50 +00:00
|
|
|
) -> String {
|
2022-06-30 08:50:09 +00:00
|
|
|
if let Some(last) = last_path_segment(ty_path).args
|
|
|
|
&& let Some(iter_ty) = last.args.iter().find_map(|arg| match arg {
|
|
|
|
GenericArg::Type(ty) => Some(ty),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
{
|
2023-11-02 16:12:25 +00:00
|
|
|
format!(
|
2024-01-14 13:13:04 +00:00
|
|
|
"{path}::<{}>()",
|
2023-11-02 16:12:25 +00:00
|
|
|
snippet_with_context(cx, iter_ty.span, ctxt, "..", applicability).0
|
|
|
|
)
|
2022-06-30 08:50:09 +00:00
|
|
|
} else {
|
2024-01-14 13:13:04 +00:00
|
|
|
format!("{path}()")
|
2022-06-30 08:50:09 +00:00
|
|
|
}
|
|
|
|
}
|