fix false positive in suspicious_open_options, make paths work

This commit is contained in:
y21 2024-01-14 00:11:42 +01:00 committed by atwam
parent 515fe65ba8
commit f09cd88199
No known key found for this signature in database
6 changed files with 87 additions and 27 deletions

View file

@ -1,6 +1,18 @@
#![allow(unused_must_use)]
#![warn(clippy::nonsensical_open_options)]
use std::fs::OpenOptions;
#[allow(unused_must_use)]
#[warn(clippy::nonsensical_open_options)]
trait OpenOptionsExt {
fn truncate_write(&mut self, opt: bool) -> &mut Self;
}
impl OpenOptionsExt for OpenOptions {
fn truncate_write(&mut self, opt: bool) -> &mut Self {
self.truncate(opt).write(opt)
}
}
fn main() {
OpenOptions::new().read(true).truncate(true).open("foo.txt");
//~^ ERROR: file opened with `truncate` and `read`
@ -29,6 +41,12 @@ fn main() {
let mut options = std::fs::OpenOptions::new();
options.read(true);
options.read(false);
//#~^ ERROR: the method `read` is called more than once
// Possible future improvement: recognize open options method call chains across statements.
options.open("foo.txt");
let mut options = std::fs::OpenOptions::new();
options.truncate(true);
options.create(true).open("foo.txt");
OpenOptions::new().create(true).truncate_write(true).open("foo.txt");
}

View file

@ -1,5 +1,5 @@
error: file opened with `truncate` and `read`
--> $DIR/open_options.rs:5:5
--> $DIR/open_options.rs:17:5
|
LL | OpenOptions::new().read(true).truncate(true).open("foo.txt");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -8,43 +8,43 @@ LL | OpenOptions::new().read(true).truncate(true).open("foo.txt");
= help: to override `-D warnings` add `#[allow(clippy::nonsensical_open_options)]`
error: file opened with `append` and `truncate`
--> $DIR/open_options.rs:8:5
--> $DIR/open_options.rs:20:5
|
LL | OpenOptions::new().append(true).truncate(true).open("foo.txt");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the method `read` is called more than once
--> $DIR/open_options.rs:11:35
--> $DIR/open_options.rs:23:35
|
LL | OpenOptions::new().read(true).read(false).open("foo.txt");
| ^^^^^^^^^^^
error: the method `create` is called more than once
--> $DIR/open_options.rs:16:10
--> $DIR/open_options.rs:28:10
|
LL | .create(false)
| ^^^^^^^^^^^^^
error: the method `write` is called more than once
--> $DIR/open_options.rs:19:36
--> $DIR/open_options.rs:31:36
|
LL | OpenOptions::new().write(true).write(false).open("foo.txt");
| ^^^^^^^^^^^^
error: the method `append` is called more than once
--> $DIR/open_options.rs:21:37
--> $DIR/open_options.rs:33:37
|
LL | OpenOptions::new().append(true).append(false).open("foo.txt");
| ^^^^^^^^^^^^^
error: the method `truncate` is called more than once
--> $DIR/open_options.rs:23:39
--> $DIR/open_options.rs:35:39
|
LL | OpenOptions::new().truncate(true).truncate(false).open("foo.txt");
| ^^^^^^^^^^^^^^^
error: the method `read` is called more than once
--> $DIR/open_options.rs:26:41
--> $DIR/open_options.rs:38:41
|
LL | std::fs::File::options().read(true).read(false).open("foo.txt");
| ^^^^^^^^^^^

View file

@ -4,7 +4,9 @@ error: file opened with `create`, but `truncate` behavior not defined
LL | OpenOptions::new().create(true).open("foo.txt");
| ^^^^^^^^^^^^- help: add: `.truncate(true)`
|
= help: if you intend to overwrite an existing file entirely, call `.truncate(true)`. if you instead know that you may want to keep some parts of the old file, call `.truncate(false)`. Alternatively, use `.append` to append to the file instead of overwriting it.
= help: if you intend to overwrite an existing file entirely, call `.truncate(true)`
= help: if you instead know that you may want to keep some parts of the old file, call `.truncate(false)`
= help: alternatively, use `.append(true)` to append to the file instead of overwriting it
= note: `-D clippy::suspicious-open-options` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::suspicious_open_options)]`