]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/vec_resize_to_zero.rs
Auto merge of #103828 - cassaundra:fix-format-args-span2, r=cjgillot
[rust.git] / src / tools / clippy / clippy_lints / src / methods / vec_resize_to_zero.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use if_chain::if_chain;
4 use rustc_ast::LitKind;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind};
7 use rustc_lint::LateContext;
8 use rustc_span::source_map::Spanned;
9 use rustc_span::{sym, Span};
10
11 use super::VEC_RESIZE_TO_ZERO;
12
13 pub(super) fn check<'tcx>(
14     cx: &LateContext<'tcx>,
15     expr: &'tcx Expr<'_>,
16     count_arg: &'tcx Expr<'_>,
17     default_arg: &'tcx Expr<'_>,
18     name_span: Span,
19 ) {
20     if_chain! {
21         if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
22         if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
23         if is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id), sym::Vec);
24         if let ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = count_arg.kind;
25         if let ExprKind::Lit(Spanned { node: LitKind::Int(..), .. }) = default_arg.kind;
26         then {
27             let method_call_span = expr.span.with_lo(name_span.lo());
28             span_lint_and_then(
29                 cx,
30                 VEC_RESIZE_TO_ZERO,
31                 expr.span,
32                 "emptying a vector with `resize`",
33                 |db| {
34                     db.help("the arguments may be inverted...");
35                     db.span_suggestion(
36                         method_call_span,
37                         "...or you can empty the vector with",
38                         "clear()".to_string(),
39                         Applicability::MaybeIncorrect,
40                     );
41                 },
42             );
43         }
44     }
45 }