Skip to content

Commit

Permalink
PR Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
atwam committed Oct 10, 2023
1 parent 3940e9a commit f42ec8e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
38 changes: 14 additions & 24 deletions clippy_lints/src/methods/open_options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_data_structures::fx::FxHashMap;

use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
use clippy_utils::paths;
use clippy_utils::ty::match_type;
use rustc_ast::ast::LitKind;
Expand Down Expand Up @@ -120,49 +120,39 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
}
}

if_chain! {
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read);
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate);
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
then {
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read)
&& let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
&& let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write)
{
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
"file opened with `truncate` and `read`",
);
}
}

if_chain! {
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append);
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate);
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
then {
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append)
&& let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
{
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
"file opened with `append` and `truncate`",
);
}
}

if_chain! {
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create);
if let None = options.get(&OpenOption::Truncate);
then {
span_lint_and_then(
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create)
&& let None = options.get(&OpenOption::Truncate)
{
span_lint_and_help(
cx,
SUSPICIOUS_OPEN_OPTIONS,
*create_span,
"file opened with `create`, but `truncate` behavior not defined",
|diag| {
diag
//.span_suggestion(create_span.shrink_to_hi(), "add", ".truncate(true)".to_string(), rustc_errors::Applicability::MaybeIncorrect)
.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)`");
},
Some(span),
"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)`"
);
}
}
}
12 changes: 10 additions & 2 deletions tests/ui/open_options.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ error: file opened with `create`, but `truncate` behavior not defined
LL | OpenOptions::new().create(true).create(false).open("foo.txt");
| ^^^^^^^^^^^^
|
= 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)`
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)`
--> $DIR/open_options.rs:14:5
|
LL | OpenOptions::new().create(true).create(false).open("foo.txt");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `-D clippy::suspicious-open-options` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::suspicious_open_options)]`

Expand Down Expand Up @@ -59,7 +63,11 @@ error: file opened with `create`, but `truncate` behavior not defined
LL | OpenOptions::new().create(true).open("foo.txt");
| ^^^^^^^^^^^^
|
= 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)`
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)`
--> $DIR/open_options.rs:22:5
|
LL | OpenOptions::new().create(true).open("foo.txt");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors

0 comments on commit f42ec8e

Please sign in to comment.