This commit is contained in:
atwam 2023-10-10 13:30:24 +01:00
parent 6fb471d646
commit 2ec8729962
No known key found for this signature in database
2 changed files with 24 additions and 25 deletions

View file

@ -1,6 +1,7 @@
use rustc_data_structures::fx::FxHashMap; 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_then};
use clippy_utils::paths;
use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::ty::is_type_diagnostic_item;
use rustc_ast::ast::LitKind; use rustc_ast::ast::LitKind;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
@ -117,11 +118,10 @@ 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::Read); && let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate); && let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write)
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write); {
then {
span_lint( span_lint(
cx, cx,
NONSENSICAL_OPEN_OPTIONS, NONSENSICAL_OPEN_OPTIONS,
@ -129,37 +129,28 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
"file opened with `truncate` and `read`", "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::Append); && let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate); {
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
then {
span_lint( span_lint(
cx, cx,
NONSENSICAL_OPEN_OPTIONS, NONSENSICAL_OPEN_OPTIONS,
span, span,
"file opened with `append` and `truncate`", "file opened with `append` and `truncate`",
); );
}
} }
if_chain! { if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create)
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create); && let None = options.get(&OpenOption::Truncate)
if let None = options.get(&OpenOption::Truncate); {
then { span_lint_and_help(
span_lint_and_then(
cx, cx,
SUSPICIOUS_OPEN_OPTIONS, SUSPICIOUS_OPEN_OPTIONS,
*create_span, *create_span,
"file opened with `create`, but `truncate` behavior not defined", "file opened with `create`, but `truncate` behavior not defined",
|diag| { Some(span),
diag "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)`"
//.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)`");
},
); );
}
} }
} }

View file

@ -31,7 +31,11 @@ error: file opened with `create`, but `truncate` behavior not defined
LL | OpenOptions::new().create(true).create(false).open("foo.txt"); 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` = note: `-D clippy::suspicious-open-options` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::suspicious_open_options)]` = help: to override `-D warnings` add `#[allow(clippy::suspicious_open_options)]`
@ -59,7 +63,11 @@ error: file opened with `create`, but `truncate` behavior not defined
LL | OpenOptions::new().create(true).open("foo.txt"); 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 error: aborting due to 9 previous errors