-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: bufreader_stdin
#13682
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
#13682
Changes from all commits
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,73 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::match_def_path; | ||
use clippy_utils::source::snippet; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ExprKind, QPath, TyKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::sym; | ||
|
||
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 increase the memcpy calls. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// let reader = std::io::BufReader::new(std::io::stdin()); | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let stdin = std::io::stdin(); | ||
/// let reader = stdin.lock(); | ||
/// ``` | ||
#[clippy::version = "1.84.0"] | ||
pub BUFREADER_STDIN, | ||
perf, | ||
"using `BufReader::new` with `Stdin` or `StdinLock` is unnecessary and less efficient" | ||
} | ||
|
||
declare_lint_pass!(BufreaderStdinlock => [BUFREADER_STDIN]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for BufreaderStdinlock { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) { | ||
if e.span.from_expansion() { | ||
return; | ||
} | ||
if let ExprKind::Call(func, [arg]) = e.kind | ||
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = func.kind | ||
&& seg.ident.name == sym::new | ||
&& let TyKind::Path(ref qpath) = ty.kind | ||
&& let Some(did) = cx.qpath_res(qpath, ty.hir_id).opt_def_id() | ||
&& match_def_path(cx, did, &["std", "io", "buffered", "bufreader", "BufReader"]) | ||
&& let arg_ty = cx.typeck_results().expr_ty(arg) | ||
&& let Some(arg_did) = arg_ty.ty_adt_def().map(rustc_middle::ty::AdtDef::did) | ||
{ | ||
if match_def_path(cx, arg_did, &["std", "io", "stdio", "Stdin"]) { | ||
span_lint_and_sugg( | ||
cx, | ||
BUFREADER_STDIN, | ||
e.span, | ||
"using `BufReader::new` with `Stdin`", | ||
"instead of wrapping `Stdin` in `BufReader`, use the `lock` method directly", | ||
format!("{}.lock()", snippet(cx, arg.span, "..")), | ||
Applicability::MachineApplicable, | ||
); | ||
} else if match_def_path(cx, arg_did, &["std", "io", "stdio", "StdinLock"]) { | ||
span_lint_and_sugg( | ||
cx, | ||
BUFREADER_STDIN, | ||
e.span, | ||
"using `BufReader::new` with `Stdin`", | ||
"instead of wrapping `StdinLock` in `BufReader`, use it self", | ||
snippet(cx, arg.span, "..").to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} else { | ||
return; | ||
}; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![warn(clippy::bufreader_stdin)] | ||
use std::io::{self, BufReader}; | ||
|
||
fn main() { | ||
let a = io::stdin(); | ||
let reader = a.lock(); | ||
|
||
let b = io::stdin().lock(); | ||
let reader = b; | ||
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. we should be very careful dealing with suggestions where type changes occur, this suggestion won't work if the resulting variable is used somewhere else, for example: use std::io::{self, BufReader};
use std::io::StdinLock;
fn main() {
let b = io::stdin().lock();
let reader = BufReader::new(b);
// let reader = b; // does not work
i_want_buf_reader(reader);
}
fn i_want_buf_reader(reader: BufReader<StdinLock>) {
// do something with the reader
drop(reader);
} this will not compile, and another example would be static READER_ONCE: OnceLock<BufReader<Stdin>> = OnceLock::new();
let a = READER_ONCE.get_or_init(|| BufReader::new(io::stdin())); although these are very unlikely to happen, but you can never be too careful~ So I'd say at least changing the applicability to Also, it would be nice to run 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. Yes, these suggestions aren't MachineApplicable. MaybeIncorrect, unfortunately. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![warn(clippy::BUFREADER_STDIN)] | ||
use std::io::{self, BufReader}; | ||
|
||
fn main() { | ||
let a = io::stdin(); | ||
let reader = BufReader::new(a); | ||
|
||
let b = io::stdin().lock(); | ||
let reader = BufReader::new(b); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
error: unknown lint: `clippy::BUFREADER_STDIN` | ||
--> tests/ui/bufreader_stdin.rs:1:9 | ||
| | ||
LL | #![warn(clippy::BUFREADER_STDIN)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::bufreader_stdin` | ||
| | ||
= note: `-D unknown-lints` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(unknown_lints)]` | ||
Comment on lines
+1
to
+8
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. looks like an accident, make sure to change it to lowercase name |
||
|
||
error: using `BufReader::new` with `Stdin` | ||
--> tests/ui/bufreader_stdin.rs:6: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 = a.lock(); | ||
| ~~~~~~~~ | ||
|
||
error: using `BufReader::new` with `Stdin` | ||
--> tests/ui/bufreader_stdin.rs:9:18 | ||
| | ||
LL | let reader = BufReader::new(b); | ||
| ^^^^^^^^^^^^^^^^^ help: instead of wrapping `StdinLock` in `BufReader`, use it self: `b` | ||
|
||
error: aborting due to 3 previous errors | ||
|
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.
nit: I think
Buf
andReader
should be separated words? no?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.
nah, this is BufReader + Stdin, so I'd keep BufReader as a single atomic unit in the name