2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
|
|
|
use clippy_utils::paths;
|
|
|
|
use clippy_utils::ty::match_type;
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast::LitKind;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2022-08-31 08:24:45 -05:00
|
|
|
use rustc_lint::LateContext;
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::{Span, Spanned};
|
2015-10-07 06:15:14 -05:00
|
|
|
|
2022-08-31 08:24:45 -05:00
|
|
|
use super::NONSENSICAL_OPEN_OPTIONS;
|
2015-10-07 06:15:14 -05:00
|
|
|
|
2022-08-31 08:24:45 -05:00
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
|
|
|
|
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
|
|
|
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
|
|
|
|
&& match_type(cx, cx.tcx.type_of(impl_id), &paths::OPEN_OPTIONS)
|
|
|
|
{
|
|
|
|
let mut options = Vec::new();
|
|
|
|
get_open_options(cx, recv, &mut options);
|
|
|
|
check_open_options(cx, &options, e.span);
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 22:43:56 -06:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2015-10-07 06:15:14 -05:00
|
|
|
enum Argument {
|
|
|
|
True,
|
|
|
|
False,
|
2016-01-03 22:26:12 -06:00
|
|
|
Unknown,
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum OpenOption {
|
2015-10-07 10:15:44 -05:00
|
|
|
Write,
|
|
|
|
Read,
|
|
|
|
Truncate,
|
|
|
|
Create,
|
2016-01-03 22:26:12 -06:00
|
|
|
Append,
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) {
|
2022-09-01 04:43:35 -05:00
|
|
|
if let ExprKind::MethodCall(path, receiver, arguments, _) = argument.kind {
|
|
|
|
let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();
|
2016-01-03 22:26:12 -06:00
|
|
|
|
2015-10-07 06:15:14 -05:00
|
|
|
// Only proceed if this is a call on some object of type std::fs::OpenOptions
|
2022-09-08 14:27:09 -05:00
|
|
|
if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && !arguments.is_empty() {
|
2022-09-01 04:43:35 -05:00
|
|
|
let argument_option = match arguments[0].kind {
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Lit(ref span) => {
|
2017-09-05 04:33:04 -05:00
|
|
|
if let Spanned {
|
|
|
|
node: LitKind::Bool(lit),
|
|
|
|
..
|
2019-01-19 02:27:45 -06:00
|
|
|
} = *span
|
2017-09-05 04:33:04 -05:00
|
|
|
{
|
2021-03-12 08:30:50 -06:00
|
|
|
if lit { Argument::True } else { Argument::False }
|
2015-10-07 06:15:14 -05:00
|
|
|
} else {
|
2021-03-12 08:30:50 -06:00
|
|
|
// The function is called with a literal which is not a boolean literal.
|
|
|
|
// This is theoretically possible, but not very likely.
|
|
|
|
return;
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:26:12 -06:00
|
|
|
_ => Argument::Unknown,
|
2015-10-07 06:15:14 -05:00
|
|
|
};
|
2016-01-03 22:26:12 -06:00
|
|
|
|
2021-12-14 23:13:11 -06:00
|
|
|
match path.ident.as_str() {
|
2015-10-07 06:15:14 -05:00
|
|
|
"create" => {
|
2015-10-07 10:15:44 -05:00
|
|
|
options.push((OpenOption::Create, argument_option));
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-10-07 06:15:14 -05:00
|
|
|
"append" => {
|
2015-10-07 10:15:44 -05:00
|
|
|
options.push((OpenOption::Append, argument_option));
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-10-07 06:15:14 -05:00
|
|
|
"truncate" => {
|
2015-10-07 10:15:44 -05:00
|
|
|
options.push((OpenOption::Truncate, argument_option));
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-10-07 06:15:14 -05:00
|
|
|
"read" => {
|
2015-10-07 10:15:44 -05:00
|
|
|
options.push((OpenOption::Read, argument_option));
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-10-07 06:15:14 -05:00
|
|
|
"write" => {
|
2015-10-07 10:15:44 -05:00
|
|
|
options.push((OpenOption::Write, argument_option));
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-04-14 13:14:03 -05:00
|
|
|
_ => (),
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
2016-01-03 22:26:12 -06:00
|
|
|
|
2022-09-01 04:43:35 -05:00
|
|
|
get_open_options(cx, receiver, options);
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_open_options(cx: &LateContext<'_>, options: &[(OpenOption, Argument)], span: Span) {
|
2016-01-03 22:43:56 -06:00
|
|
|
let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
|
2016-12-20 11:21:30 -06:00
|
|
|
let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
|
|
|
|
(false, false, false, false, false);
|
2017-08-09 02:30:56 -05:00
|
|
|
// This code is almost duplicated (oh, the irony), but I haven't found a way to
|
|
|
|
// unify it.
|
2016-01-03 22:43:56 -06:00
|
|
|
|
|
|
|
for option in options {
|
|
|
|
match *option {
|
|
|
|
(OpenOption::Create, arg) => {
|
|
|
|
if create {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"the method `create` is called more than once",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
2021-06-03 01:41:37 -05:00
|
|
|
create = true;
|
2016-01-03 22:43:56 -06:00
|
|
|
}
|
2019-08-01 00:09:57 -05:00
|
|
|
create_arg = create_arg || (arg == Argument::True);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:43:56 -06:00
|
|
|
(OpenOption::Append, arg) => {
|
|
|
|
if append {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"the method `append` is called more than once",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
2021-06-03 01:41:37 -05:00
|
|
|
append = true;
|
2016-01-03 22:43:56 -06:00
|
|
|
}
|
2019-08-01 00:09:57 -05:00
|
|
|
append_arg = append_arg || (arg == Argument::True);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:43:56 -06:00
|
|
|
(OpenOption::Truncate, arg) => {
|
|
|
|
if truncate {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"the method `truncate` is called more than once",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
2021-06-03 01:41:37 -05:00
|
|
|
truncate = true;
|
2016-01-03 22:43:56 -06:00
|
|
|
}
|
|
|
|
truncate_arg = truncate_arg || (arg == Argument::True);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:43:56 -06:00
|
|
|
(OpenOption::Read, arg) => {
|
|
|
|
if read {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"the method `read` is called more than once",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
2021-06-03 01:41:37 -05:00
|
|
|
read = true;
|
2016-01-03 22:43:56 -06:00
|
|
|
}
|
2019-08-01 00:09:57 -05:00
|
|
|
read_arg = read_arg || (arg == Argument::True);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:43:56 -06:00
|
|
|
(OpenOption::Write, arg) => {
|
|
|
|
if write {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"the method `write` is called more than once",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
2021-06-03 01:41:37 -05:00
|
|
|
write = true;
|
2016-01-03 22:43:56 -06:00
|
|
|
}
|
2019-08-01 00:09:57 -05:00
|
|
|
write_arg = write_arg || (arg == Argument::True);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:43:56 -06:00
|
|
|
}
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
|
2016-08-02 15:57:32 -05:00
|
|
|
if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
|
2018-11-27 14:14:15 -06:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"file opened with `truncate` and `read`",
|
2018-11-27 14:14:15 -06:00
|
|
|
);
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
2016-01-03 22:43:56 -06:00
|
|
|
if append && truncate && append_arg && truncate_arg {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2020-01-06 00:30:43 -06:00
|
|
|
"file opened with `append` and `truncate`",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
}
|