-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
new lint: bufreader_stdin
#17029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
new lint: bufreader_stdin
#17029
Changes from all commits
0b48a45
6ff18aa
fc21fd7
75d8e79
346b56a
e0e5252
0a80617
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||
| use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; | ||||||||||||||||||||||
| use clippy_utils::source::snippet; | ||||||||||||||||||||||
| use clippy_utils::sym; | ||||||||||||||||||||||
| use rustc_errors::Applicability; | ||||||||||||||||||||||
| use rustc_hir::{Expr, ExprKind, QPath, TyKind}; | ||||||||||||||||||||||
| use rustc_lint::{LateContext, LateLintPass}; | ||||||||||||||||||||||
| use rustc_session::impl_lint_pass; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| declare_clippy_lint! { | ||||||||||||||||||||||
| /// ### What it does | ||||||||||||||||||||||
| /// Checks for usage of `BufReader::new` with `Stdin` or `StdinLock`. | ||||||||||||||||||||||
| /// | ||||||||||||||||||||||
| /// ### Why is this bad? | ||||||||||||||||||||||
| /// `Stdin` is already buffered. Re-buffering it only increases the memcpy calls. | ||||||||||||||||||||||
| /// | ||||||||||||||||||||||
| /// ### Example | ||||||||||||||||||||||
| /// | ||||||||||||||||||||||
| /// ```ignore | ||||||||||||||||||||||
| /// let reader = std::io::BufReader::new(std::io::stdin()); | ||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||
| /// | ||||||||||||||||||||||
| /// Use instead: | ||||||||||||||||||||||
| /// | ||||||||||||||||||||||
| /// ```ignore | ||||||||||||||||||||||
| /// let stdin = std::io::stdin(); | ||||||||||||||||||||||
| /// let reader = stdin.lock(); | ||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #[clippy::version = "1.97.0"] | ||||||||||||||||||||||
| pub BUFREADER_STDIN, | ||||||||||||||||||||||
| pedantic, | ||||||||||||||||||||||
| "using `BufReader::new` with `Stdin` or `StdinLock` is unnecessary and less efficient" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| impl_lint_pass!(BufreaderStdin => [BUFREADER_STDIN]); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| pub struct BufreaderStdin {} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| impl BufreaderStdin { | ||||||||||||||||||||||
| pub fn new() -> Self { | ||||||||||||||||||||||
| Self {} | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| impl<'tcx> LateLintPass<'tcx> for BufreaderStdin { | ||||||||||||||||||||||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||||||||||||||||||||||
| if let ExprKind::Call(func, [arg]) = expr.kind | ||||||||||||||||||||||
| && let ExprKind::Path(QPath::TypeRelative(ty, segment)) = func.kind | ||||||||||||||||||||||
| && segment.ident.name == sym::new | ||||||||||||||||||||||
| && let TyKind::Path(ref qpath) = ty.kind | ||||||||||||||||||||||
| && let Some(did) = cx.qpath_res(qpath, ty.hir_id).opt_def_id() | ||||||||||||||||||||||
| && cx.tcx.is_diagnostic_item(sym::IoBufReader, did) | ||||||||||||||||||||||
| && let Some(arg_did) = cx | ||||||||||||||||||||||
| .typeck_results() | ||||||||||||||||||||||
| .expr_ty(arg) | ||||||||||||||||||||||
| .ty_adt_def() | ||||||||||||||||||||||
| .map(rustc_middle::ty::AdtDef::did) | ||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note:
Comment on lines
+50
to
+57
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be made shorter using
Suggested change
|
||||||||||||||||||||||
| { | ||||||||||||||||||||||
| let snip = snippet(cx, arg.span, ".."); | ||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||||||||||||||||||||||
| let applicability = if snip == ".." { | ||||||||||||||||||||||
| Applicability::HasPlaceholders | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| Applicability::MaybeIncorrect | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let arg_did_name = cx.tcx.get_diagnostic_name(arg_did); | ||||||||||||||||||||||
| let (msg, help, sugg) = match arg_did_name { | ||||||||||||||||||||||
| Some(sym::Stdin) => ( | ||||||||||||||||||||||
| "wrapping already buffered `Stdin` into a `BufReader`", | ||||||||||||||||||||||
| "instead of wrapping `Stdin` in `BufReader`, use the `lock` method directly", | ||||||||||||||||||||||
| format!("{snip}.lock()"), | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| Some(sym::StdinLock) => ( | ||||||||||||||||||||||
| "wrapping already buffered `StdinLock` into a `BufReader`", | ||||||||||||||||||||||
| "instead of wrapping `StdinLock` in `BufReader`, use it directly", | ||||||||||||||||||||||
| snip.to_string(), | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| _ => return, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
Comment on lines
+67
to
+79
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to make sure we will lint before using |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if arg.span.from_expansion() { | ||||||||||||||||||||||
| span_lint(cx, BUFREADER_STDIN, expr.span, msg); | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| span_lint_and_sugg(cx, BUFREADER_STDIN, expr.span, msg, help, sugg, applicability); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #![warn(clippy::bufreader_stdin)] | ||
|
|
||
| use std::io::{self, BufReader}; | ||
|
|
||
| macro_rules! stdin_macro { | ||
| () => { | ||
| io::stdin() | ||
| }; | ||
| } | ||
|
|
||
| fn main() { | ||
| let a = io::stdin(); | ||
| let reader = a.lock(); | ||
| //~^ bufreader_stdin | ||
|
|
||
| let b = io::stdin().lock(); | ||
| let reader = b; | ||
| //~^ bufreader_stdin | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #![warn(clippy::bufreader_stdin)] | ||
|
|
||
| use std::io::{self, BufReader}; | ||
|
|
||
| macro_rules! stdin_macro { | ||
| () => { | ||
| io::stdin() | ||
| }; | ||
| } | ||
|
|
||
| fn main() { | ||
| let a = io::stdin(); | ||
| let reader = BufReader::new(a); | ||
| //~^ bufreader_stdin | ||
|
|
||
| let b = io::stdin().lock(); | ||
| let reader = BufReader::new(b); | ||
| //~^ bufreader_stdin | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| error: wrapping already buffered `Stdin` into a `BufReader` | ||
| --> tests/ui/bufreader_stdin.rs:13:18 | ||
| | | ||
| LL | let reader = BufReader::new(a); | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: `-D clippy::bufreader-stdin` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::bufreader_stdin)]` | ||
| help: instead of wrapping `Stdin` in `BufReader`, use the `lock` method directly | ||
| | | ||
| LL - let reader = BufReader::new(a); | ||
| LL + let reader = a.lock(); | ||
| | | ||
|
|
||
| error: wrapping already buffered `StdinLock` into a `BufReader` | ||
| --> tests/ui/bufreader_stdin.rs:17:18 | ||
| | | ||
| LL | let reader = BufReader::new(b); | ||
| | ^^^^^^^^^^^^^^^^^ help: instead of wrapping `StdinLock` in `BufReader`, use it directly: `b` | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: we usually write this as
ty::Path, notTyKind::Path.View changes since the review