]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs
Rollup merge of #105623 - compiler-errors:generator-type-size-fix, r=Nilstrieb
[rust.git] / src / tools / clippy / clippy_lints / src / methods / seek_to_start_instead_of_rewind.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::ty::implements_trait;
3 use clippy_utils::{get_trait_def_id, match_def_path, paths};
4 use rustc_ast::ast::{LitIntType, LitKind};
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind};
7 use rustc_lint::LateContext;
8 use rustc_span::Span;
9
10 use super::SEEK_TO_START_INSTEAD_OF_REWIND;
11
12 pub(super) fn check<'tcx>(
13     cx: &LateContext<'tcx>,
14     expr: &'tcx Expr<'_>,
15     recv: &'tcx Expr<'_>,
16     arg: &'tcx Expr<'_>,
17     name_span: Span,
18 ) {
19     // Get receiver type
20     let ty = cx.typeck_results().expr_ty(recv).peel_refs();
21
22     if let Some(seek_trait_id) = get_trait_def_id(cx, &paths::STD_IO_SEEK) &&
23         implements_trait(cx, ty, seek_trait_id, &[]) &&
24         let ExprKind::Call(func, args1) = arg.kind &&
25         let ExprKind::Path(ref path) = func.kind &&
26         let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id() &&
27         match_def_path(cx, def_id, &paths::STD_IO_SEEKFROM_START) &&
28         args1.len() == 1 &&
29         let ExprKind::Lit(ref lit) = args1[0].kind &&
30         let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node
31     {
32         let method_call_span = expr.span.with_lo(name_span.lo());
33         span_lint_and_then(
34             cx,
35             SEEK_TO_START_INSTEAD_OF_REWIND,
36             method_call_span,
37             "used `seek` to go to the start of the stream",
38             |diag| {
39                 let app = Applicability::MachineApplicable;
40
41                 diag.span_suggestion(method_call_span, "replace with", "rewind()", app);
42             },
43         );
44     }
45 }