|
| 1 | +use clippy_utils::consts::{ConstEvalCtxt, mir_to_const}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::higher; |
| 4 | +use clippy_utils::source::snippet; |
| 5 | +use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; |
| 6 | +use rustc_ast::ast::RangeLimits; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Expr, ExprKind, LangItem, Path, QPath}; |
| 9 | +use rustc_lint::LateContext; |
| 10 | +use rustc_middle::mir::Const; |
| 11 | +use rustc_middle::ty::{Adt, Ty, TypeckResults}; |
| 12 | +use rustc_span::Span; |
| 13 | +use rustc_span::symbol::sym; |
| 14 | + |
| 15 | +use super::TRUNCATE_WITH_DRAIN; |
| 16 | + |
| 17 | +// Add `String` here when it is added to diagnostic items |
| 18 | +const ACCEPTABLE_TYPES_WITH_ARG: [rustc_span::Symbol; 2] = [sym::Vec, sym::VecDeque]; |
| 19 | + |
| 20 | +pub fn is_range_open_ended<'a>( |
| 21 | + cx: &LateContext<'a>, |
| 22 | + range: higher::Range<'_>, |
| 23 | + ty: Ty<'a>, |
| 24 | + container_path: Option<&Path<'_>>, |
| 25 | +) -> bool { |
| 26 | + let higher::Range { start, end, limits } = range; |
| 27 | + let start_is_none_or_min = start.map_or(true, |start| { |
| 28 | + if let Adt(_, subst) = ty.kind() |
| 29 | + && let bnd_ty = subst.type_at(0) |
| 30 | + && let Some(min_val) = bnd_ty.numeric_min_val(cx.tcx) |
| 31 | + && let Some(min_const) = mir_to_const(cx.tcx, Const::from_ty_const(min_val, bnd_ty, cx.tcx)) |
| 32 | + && let Some(start_const) = ConstEvalCtxt::new(cx).eval(start) |
| 33 | + { |
| 34 | + start_const == min_const |
| 35 | + } else { |
| 36 | + false |
| 37 | + } |
| 38 | + }); |
| 39 | + let end_is_none_or_max = end.map_or(true, |end| match limits { |
| 40 | + RangeLimits::Closed => { |
| 41 | + if let Adt(_, subst) = ty.kind() |
| 42 | + && let bnd_ty = subst.type_at(0) |
| 43 | + && let Some(max_val) = bnd_ty.numeric_max_val(cx.tcx) |
| 44 | + && let Some(max_const) = mir_to_const(cx.tcx, Const::from_ty_const(max_val, bnd_ty, cx.tcx)) |
| 45 | + && let Some(end_const) = ConstEvalCtxt::new(cx).eval(end) |
| 46 | + { |
| 47 | + end_const == max_const |
| 48 | + } else { |
| 49 | + false |
| 50 | + } |
| 51 | + }, |
| 52 | + RangeLimits::HalfOpen => { |
| 53 | + if let Some(container_path) = container_path |
| 54 | + && let ExprKind::MethodCall(name, self_arg, [], _) = end.kind |
| 55 | + && name.ident.name == sym::len |
| 56 | + && let ExprKind::Path(QPath::Resolved(None, path)) = self_arg.kind |
| 57 | + { |
| 58 | + container_path.res == path.res |
| 59 | + } else { |
| 60 | + false |
| 61 | + } |
| 62 | + }, |
| 63 | + }); |
| 64 | + !start_is_none_or_min && end_is_none_or_max |
| 65 | +} |
| 66 | + |
| 67 | +fn match_acceptable_type( |
| 68 | + cx: &LateContext<'_>, |
| 69 | + expr: &Expr<'_>, |
| 70 | + typeck_results: &TypeckResults<'_>, |
| 71 | + types: &[rustc_span::Symbol], |
| 72 | +) -> bool { |
| 73 | + let expr_ty = typeck_results.expr_ty(expr).peel_refs(); |
| 74 | + types.iter().any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty)) |
| 75 | + // String type is a lang item but not a diagnostic item for now so we need a separate check |
| 76 | + || is_type_lang_item(cx, expr_ty, LangItem::String) |
| 77 | +} |
| 78 | + |
| 79 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, arg: Option<&Expr<'_>>) { |
| 80 | + if let Some(arg) = arg { |
| 81 | + let typeck_results = cx.typeck_results(); |
| 82 | + if match_acceptable_type(cx, recv, typeck_results, &ACCEPTABLE_TYPES_WITH_ARG) |
| 83 | + && let ExprKind::Path(QPath::Resolved(None, container_path)) = recv.kind |
| 84 | + && let Some(range) = higher::Range::hir(arg) |
| 85 | + && let higher::Range { start: Some(start), .. } = range |
| 86 | + && is_range_open_ended(cx, range, typeck_results.expr_ty(arg), Some(container_path)) |
| 87 | + && let Some(adt) = typeck_results.expr_ty(recv).ty_adt_def() |
| 88 | + // Use `opt_item_name` while `String` is not a diagnostic item |
| 89 | + && let Some(ty_name) = cx.tcx.opt_item_name(adt.did()) |
| 90 | + { |
| 91 | + span_lint_and_sugg( |
| 92 | + cx, |
| 93 | + TRUNCATE_WITH_DRAIN, |
| 94 | + span.with_hi(expr.span.hi()), |
| 95 | + format!("`drain` used to truncate a `{ty_name}`"), |
| 96 | + "try", |
| 97 | + format!("truncate({})", snippet(cx, start.span, "0")), |
| 98 | + Applicability::MachineApplicable, |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments