|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::match_def_path; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::{ExprKind, QPath, TyKind}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::declare_lint_pass; |
| 8 | +use rustc_span::sym; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for usage of `BufReader::new` with `Stdin` or `StdinLock`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// `Stdin` is already buffered. Re-buffering it only increase the memcpy calls. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```no_run |
| 19 | + /// let reader = std::io::BufReader::new(std::io::stdin()); |
| 20 | + /// ``` |
| 21 | + /// Use instead: |
| 22 | + /// ```no_run |
| 23 | + /// let stdin = std::io::stdin(); |
| 24 | + /// let reader = stdin.lock(); |
| 25 | + /// ``` |
| 26 | + #[clippy::version = "1.84.0"] |
| 27 | + pub BUFREADER_STDIN, |
| 28 | + perf, |
| 29 | + "using `BufReader::new` with `Stdin` or `StdinLock` is unnecessary and less efficient" |
| 30 | +} |
| 31 | + |
| 32 | +declare_lint_pass!(BufreaderStdinlock => [BUFREADER_STDIN]); |
| 33 | + |
| 34 | +impl<'tcx> LateLintPass<'tcx> for BufreaderStdinlock { |
| 35 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) { |
| 36 | + if e.span.from_expansion() { |
| 37 | + return; |
| 38 | + } |
| 39 | + if let ExprKind::Call(func, [arg]) = e.kind |
| 40 | + && let ExprKind::Path(QPath::TypeRelative(ty, seg)) = func.kind |
| 41 | + && seg.ident.name == sym::new |
| 42 | + && let TyKind::Path(ref qpath) = ty.kind |
| 43 | + && let Some(did) = cx.qpath_res(qpath, ty.hir_id).opt_def_id() |
| 44 | + && match_def_path(cx, did, &["std", "io", "buffered", "bufreader", "BufReader"]) |
| 45 | + && let arg_ty = cx.typeck_results().expr_ty(arg) |
| 46 | + && let Some(arg_did) = arg_ty.ty_adt_def().map(|def| def.did()) |
| 47 | + { |
| 48 | + if match_def_path(cx, arg_did, &["std", "io", "stdio", "Stdin"]) { |
| 49 | + span_lint_and_sugg( |
| 50 | + cx, |
| 51 | + &BUFREADER_STDIN, |
| 52 | + e.span, |
| 53 | + "using `BufReader::new` with `Stdin`", |
| 54 | + "instead of wrapping `Stdin` in `BufReader`, use the `lock` method directly", |
| 55 | + format!("{}.lock()", snippet(cx, arg.span, "..")), |
| 56 | + Applicability::MachineApplicable, |
| 57 | + ); |
| 58 | + } else if match_def_path(cx, arg_did, &["std", "io", "stdio", "StdinLock"]) { |
| 59 | + span_lint_and_sugg( |
| 60 | + cx, |
| 61 | + &BUFREADER_STDIN, |
| 62 | + e.span, |
| 63 | + "using `BufReader::new` with `Stdin`", |
| 64 | + "instead of wrapping `StdinLock` in `BufReader`, use it self", |
| 65 | + snippet(cx, arg.span, "..").to_string(), |
| 66 | + Applicability::MachineApplicable, |
| 67 | + ); |
| 68 | + } else { |
| 69 | + return; |
| 70 | + }; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments