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

Doc's example code for BufferedWriter is wrong and also generate warnings #18197

Closed
jameschurchman opened this issue Oct 20, 2014 · 1 comment · Fixed by #18374
Closed

Doc's example code for BufferedWriter is wrong and also generate warnings #18197

jameschurchman opened this issue Oct 20, 2014 · 1 comment · Fixed by #18374
Assignees

Comments

@jameschurchman
Copy link

Struct std::io::BufferedWriter has the example code :

use std::io::{BufferedWriter, File};

let file = File::open(&Path::new("message.txt"));
let mut writer = BufferedWriter::new(file);

writer.write_str("hello, world");
writer.flush();

But this code opens the file as read only, it should open it for writing or creating a new file, for example :

let file = File::create(&Path::new("message.txt"));

It also generates two warnings :

buff_writer.rs:7:2: 7:35 warning: unused result which must be used, #[warn(unused_must_use)] on by default
buff_writer.rs:7    writer.write_str("hello, world");
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
buff_writer.rs:8:2: 8:17 warning: unused result which must be used, #[warn(unused_must_use)] on by default
buff_writer.rs:8    writer.flush();

by unwrap()ing both these remove the errors, and in the case where File::open is used to successfully crash the sample app when attempting to append to a read only file.

@steveklabnik
Copy link
Member

So the reasons that the warnings don't happen is

/// ```rust
/// # #![allow(unused_must_use)]
/// use std::io::{BufferedWriter, File};
///
/// let file = File::open(&Path::new("message.txt"));
/// let mut writer = BufferedWriter::new(file);
///
/// writer.write_str("hello, world");
/// writer.flush();
/// ```

They were turned off!

steveklabnik added a commit to steveklabnik/rust that referenced this issue Oct 28, 2014
lnicola pushed a commit to lnicola/rust that referenced this issue Oct 8, 2024
…eykril

minor: Stricter requirements for package wide flycheck

Require the existence of a target and `check_workspace` to be false to restart package-wide flycheck. Fixes rust-lang#18194 , rust-lang#18104
lnicola pushed a commit to lnicola/rust that referenced this issue Dec 11, 2024
Our first attempt to make flycheck only check the current crate
if the crate is one of bin/bench/test targets had caused
`check_workspace` to be ignored, which should have been a config
with higher precedence all along. This commit revert rust-lang#18197 and closes rust-lang#18562
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants