Skip to content

Commit f42ec8e

Browse files
committed
PR Fixes
1 parent 3940e9a commit f42ec8e

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

clippy_lints/src/methods/open_options.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fx::FxHashMap;
22

3-
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
3+
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
44
use clippy_utils::paths;
55
use clippy_utils::ty::match_type;
66
use rustc_ast::ast::LitKind;
@@ -120,49 +120,39 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
120120
}
121121
}
122122

123-
if_chain! {
124-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read);
125-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate);
126-
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
127-
then {
123+
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read)
124+
&& let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
125+
&& let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write)
126+
{
128127
span_lint(
129128
cx,
130129
NONSENSICAL_OPEN_OPTIONS,
131130
span,
132131
"file opened with `truncate` and `read`",
133132
);
134133
}
135-
}
136134

137-
if_chain! {
138-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append);
139-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate);
140-
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
141-
then {
135+
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append)
136+
&& let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
137+
{
142138
span_lint(
143139
cx,
144140
NONSENSICAL_OPEN_OPTIONS,
145141
span,
146142
"file opened with `append` and `truncate`",
147143
);
148-
}
149144
}
150145

151-
if_chain! {
152-
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create);
153-
if let None = options.get(&OpenOption::Truncate);
154-
then {
155-
span_lint_and_then(
146+
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create)
147+
&& let None = options.get(&OpenOption::Truncate)
148+
{
149+
span_lint_and_help(
156150
cx,
157151
SUSPICIOUS_OPEN_OPTIONS,
158152
*create_span,
159153
"file opened with `create`, but `truncate` behavior not defined",
160-
|diag| {
161-
diag
162-
//.span_suggestion(create_span.shrink_to_hi(), "add", ".truncate(true)".to_string(), rustc_errors::Applicability::MaybeIncorrect)
163-
.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)`");
164-
},
154+
Some(span),
155+
"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)`"
165156
);
166-
}
167157
}
168158
}

tests/ui/open_options.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ error: file opened with `create`, but `truncate` behavior not defined
3131
LL | OpenOptions::new().create(true).create(false).open("foo.txt");
3232
| ^^^^^^^^^^^^
3333
|
34-
= 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)`
34+
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)`
35+
--> $DIR/open_options.rs:14:5
36+
|
37+
LL | OpenOptions::new().create(true).create(false).open("foo.txt");
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3539
= note: `-D clippy::suspicious-open-options` implied by `-D warnings`
3640
= help: to override `-D warnings` add `#[allow(clippy::suspicious_open_options)]`
3741

@@ -59,7 +63,11 @@ error: file opened with `create`, but `truncate` behavior not defined
5963
LL | OpenOptions::new().create(true).open("foo.txt");
6064
| ^^^^^^^^^^^^
6165
|
62-
= 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)`
66+
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)`
67+
--> $DIR/open_options.rs:22:5
68+
|
69+
LL | OpenOptions::new().create(true).open("foo.txt");
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6371

6472
error: aborting due to 9 previous errors
6573

0 commit comments

Comments
 (0)