Skip to content

Commit eb5cc8f

Browse files
committed
PR Fixes
1 parent 9335939 commit eb5cc8f

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

clippy_lints/src/methods/open_options.rs

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

33
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
4+
use clippy_utils::paths;
45
use clippy_utils::ty::is_type_diagnostic_item;
56
use rustc_ast::ast::LitKind;
67
use rustc_hir::{Expr, ExprKind};
@@ -117,49 +118,39 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
117118
}
118119
}
119120

120-
if_chain! {
121-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read);
122-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate);
123-
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
124-
then {
121+
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read)
122+
&& let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
123+
&& let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write)
124+
{
125125
span_lint(
126126
cx,
127127
NONSENSICAL_OPEN_OPTIONS,
128128
span,
129129
"file opened with `truncate` and `read`",
130130
);
131131
}
132-
}
133132

134-
if_chain! {
135-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append);
136-
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate);
137-
if let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write);
138-
then {
133+
if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append)
134+
&& let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate)
135+
{
139136
span_lint(
140137
cx,
141138
NONSENSICAL_OPEN_OPTIONS,
142139
span,
143140
"file opened with `append` and `truncate`",
144141
);
145-
}
146142
}
147143

148-
if_chain! {
149-
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create);
150-
if let None = options.get(&OpenOption::Truncate);
151-
then {
152-
span_lint_and_then(
144+
if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create)
145+
&& let None = options.get(&OpenOption::Truncate)
146+
{
147+
span_lint_and_help(
153148
cx,
154149
SUSPICIOUS_OPEN_OPTIONS,
155150
*create_span,
156151
"file opened with `create`, but `truncate` behavior not defined",
157-
|diag| {
158-
diag
159-
//.span_suggestion(create_span.shrink_to_hi(), "add", ".truncate(true)".to_string(), rustc_errors::Applicability::MaybeIncorrect)
160-
.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)`");
161-
},
152+
Some(span),
153+
"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)`"
162154
);
163-
}
164155
}
165156
}

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)