Skip to content

new lint: bufreader_stdin#17029

Open
JaafarTanoukhi wants to merge 7 commits into
rust-lang:masterfrom
JaafarTanoukhi:new-lint-bufreader-stdin
Open

new lint: bufreader_stdin#17029
JaafarTanoukhi wants to merge 7 commits into
rust-lang:masterfrom
JaafarTanoukhi:new-lint-bufreader-stdin

Conversation

@JaafarTanoukhi

@JaafarTanoukhi JaafarTanoukhi commented May 17, 2026

Copy link
Copy Markdown

View all comments

changelog: [bufreader_stdin]: new lint

fixes #6755

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 17, 2026
@rustbot

rustbot commented May 17, 2026

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, llogiq, samueltardieu

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the suggestion cannot probably more than MaybeIncorrect, as the surrounding code might expect a BufReader, and not any type implementing BufRead.

The category should probably be upgraded to at least pedantic because of the risk of false positives (due to the type change).

View changes since this review

Comment thread clippy_lints/src/bufreader_stdin.rs Outdated
Comment on lines +52 to +54
&& let TyKind::Path(ref qpath) = ty.kind
&& let Some(did) = cx.qpath_res(qpath, ty.hir_id).opt_def_id()
&& cx.tcx.def_path_str(did).ends_with("std::io::BufReader")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the proper way to check for a given type. Look at the clippy_utils::paths module for that.

Comment thread clippy_lints/src/bufreader_stdin.rs Outdated
"using `BufReader::new` with `Stdin`",
"instead of wrapping `Stdin` in `BufReader`, use the `lock` method directly",
format!("{}.lock()", snippet(cx, arg.span, "..")),
Applicability::MachineApplicable,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the expression doesn't come from a macro but part of it do? For example, BufReader::new(mac!(123)) where mac!(123) expands to std::io::stdin()? Please include examples involving such macros.

Comment thread clippy_lints/src/bufreader_stdin.rs Outdated
format!("{}.lock()", snippet(cx, arg.span, "..")),
Applicability::MachineApplicable,
);
} else if cx.tcx.def_path_str(arg_did).ends_with("std::io::StdinLock") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, use clippy_utils::paths if there is no diagnostic item. We'll add one later.

Comment thread clippy_lints/src/lib.rs Outdated
Box::new(|_| Box::new(unused_peekable::UnusedPeekable)),
Box::new(|_| Box::new(bool_to_int_with_if::BoolToIntWithIf)),
Box::new(|_| Box::new(box_default::BoxDefault)),
Box::new(move |_| Box::new(bufreader_stdin::BufreaderStdin::new())),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax for registering lints has recently changed. Please rebase your PR and add your lint at the end of the lint list.

Comment thread clippy_lints/src/bufreader_stdin.rs Outdated
expr.span,
"using `BufReader::new` with `Stdin`",
"instead of wrapping `Stdin` in `BufReader`, use the `lock` method directly",
format!("{}.lock()", snippet(cx, arg.span, "..")),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to check the applicability here, in case the source cannot be retrieved.

Comment thread clippy_lints/src/bufreader_stdin.rs Outdated
cx,
BUFREADER_STDIN,
expr.span,
"using `BufReader::new` with `StdinLock`",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message could be clearer:

Suggested change
"using `BufReader::new` with `StdinLock`",
"wrapping already buffered `StdinLock` into a `BufReader`",

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 2, 2026
@rustbot

rustbot commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@DanBondarenko

Copy link
Copy Markdown

@JaafarTanoukhi Hi! Are you planning to continue work on this? I'd love to pick this up. :)

@JaafarTanoukhi

Copy link
Copy Markdown
Author

@DanBondarenko I was planning to get back to it sometime this week, but I don't mind if you'd like to take over.

@DanBondarenko

Copy link
Copy Markdown

@JaafarTanoukhi No worries, I'll leave it with you. Happy to help if anything comes up.

@rustbot

rustbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Jul 18, 2026
@rustbot

This comment has been minimized.

@JaafarTanoukhi
JaafarTanoukhi force-pushed the new-lint-bufreader-stdin branch from fe83bad to 346b56a Compare July 18, 2026 18:21
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) has-merge-commits PR has merge commits, merge with caution. labels Jul 18, 2026
@github-actions

Copy link
Copy Markdown

Lintcheck changes for 0a80617

Lint Added Removed Changed
clippy::multiple_crate_versions 0 0 3

This comment will be updated if you push new changes

@JaafarTanoukhi

Copy link
Copy Markdown
Author

@rustbot ready

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 22, 2026
.ty_adt_def()
.map(rustc_middle::ty::AdtDef::did)
{
let snip = snippet(cx, arg.span, "..");

@samueltardieu samueltardieu Jul 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use snippet_opt() instead, that would be cleaner than checking the output.

View changes since the review

Comment on lines +67 to +79
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,
};

@samueltardieu samueltardieu Jul 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to make sure we will lint before using snippet() (or snippet_opt()) which require accessing the source. Here, we might have used snippet() while we are not yet sure we will lint. Also, the suggestion would be best built inside a span_lint_and_then() to ensure that the new string will be allocated only if the lint is actually emitted.

View changes since the review

.typeck_results()
.expr_ty(arg)
.ty_adt_def()
.map(rustc_middle::ty::AdtDef::did)

@samueltardieu samueltardieu Jul 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: rustc_middle::ty is usually imported, not qualified.

View changes since the review

Comment on lines +50 to +57
&& 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)

@samueltardieu samueltardieu Jul 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made shorter using clippy_utils::res traits.

Suggested change
&& 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)
&& ty.basic_res().is_diag_item(cx, sym::IoBufReader)
&& let Some(arg_did) = cx.typeck_results().expr_ty(arg).opt_def_id()

View changes since the review

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

@samueltardieu samueltardieu Jul 24, 2026

Copy link
Copy Markdown
Member

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, not TyKind::Path.

View changes since the review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BufReader Stdin or StdinLock

5 participants