Skip to content
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

std::io::Empty that is never read from #11400

Open
Centri3 opened this issue Aug 24, 2023 · 1 comment
Open

std::io::Empty that is never read from #11400

Centri3 opened this issue Aug 24, 2023 · 1 comment
Labels
A-lint Area: New lints

Comments

@Centri3
Copy link
Member

Centri3 commented Aug 24, 2023

What it does

If Empty is constructed yet its implementations of Read/BufRead are never required, we should suggest Sink instead which conveys this purpose better.

We also need to make sure some access does not require a trait outside of std that Sink doesn't implement.

Advantage

  • This restricts it from being read from in the future, if that's undesirable.

Drawbacks

  • This restricts it from being read from in the future, if that's desirable.

Example

use std::io::{prelude::*, empty};

fn b<T: Read>(t: &T) {}

fn a() {
    let mut empty_unused = empty();
    empty_unused.write(&[0]);

    let mut empty_used1 = empty();
    empty_used1.write(&[0]);
    empty_used1.read(&mut []);
    
    let mut empty_used2 = empty();
    b(&empty_used2);
}

Could be written as:

use std::io::{prelude::*, empty, sink};

fn b<T: Read>(t: &T) {}

fn a() {
    let mut empty_unused = sink();
    empty_unused.write(&[0]);

    let mut empty_used1 = empty();
    empty_used1.write(&[0]);
    empty_used1.read(&mut []);
    
    let mut empty_used2 = empty();
    b(&empty_used2);
}
@Centri3 Centri3 added the A-lint Area: New lints label Aug 24, 2023
@Centri3
Copy link
Member Author

Centri3 commented Aug 24, 2023

Write for Empty was only added (merged) last month and is still in nightly: rust-lang/rust#98154, as such we might want to wait until it's in stable to possibly implement this (just so we know it's here to stay and don't do unnecessary work).

There was also a PR to soft-deprecate Sink after this functionality was added but it doesn't seem it will ever be: rust-lang/rust#114384 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant