rust-clippy/clippy_lints/src/open_options.rs

201 lines
6.7 KiB
Rust
Raw Normal View History

2021-03-13 23:01:03 +00:00
use crate::utils::{paths, span_lint};
use clippy_utils::ty::match_type;
2020-03-01 03:23:33 +00:00
use rustc_ast::ast::LitKind;
2020-01-06 16:39:50 +00:00
use rustc_hir::{Expr, ExprKind};
2020-01-12 06:08:41 +00:00
use rustc_lint::{LateContext, LateLintPass};
2020-01-11 11:37:08 +00:00
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::{Span, Spanned};
2015-10-07 11:15:14 +00:00
2018-03-28 13:24:26 +00:00
declare_clippy_lint! {
/// **What it does:** Checks for duplicate open options as well as combinations
/// that make no sense.
///
/// **Why is this bad?** In the best case, the code will be harder to read than
/// necessary. I don't know the worst case.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
2019-03-05 22:23:50 +00:00
/// use std::fs::OpenOptions;
///
/// OpenOptions::new().read(true).truncate(true);
/// ```
2015-10-07 11:15:14 +00:00
pub NONSENSICAL_OPEN_OPTIONS,
2018-03-28 13:24:26 +00:00
correctness,
2015-10-07 15:15:44 +00:00
"nonsensical combination of options for opening a file"
2015-10-07 11:15:14 +00:00
}
2019-04-08 20:43:55 +00:00
declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
2015-10-07 11:15:14 +00:00
impl<'tcx> LateLintPass<'tcx> for OpenOptions {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
2020-06-09 21:44:04 +00:00
if let ExprKind::MethodCall(ref path, _, ref arguments, _) = e.kind {
let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
2019-05-17 21:53:54 +00:00
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
2015-10-07 11:15:14 +00:00
let mut options = Vec::new();
get_open_options(cx, &arguments[0], &mut options);
check_open_options(cx, &options, e.span);
}
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
2015-10-07 11:15:14 +00:00
enum Argument {
True,
False,
2016-01-04 04:26:12 +00:00
Unknown,
2015-10-07 11:15:14 +00:00
}
#[derive(Debug)]
enum OpenOption {
2015-10-07 15:15:44 +00:00
Write,
Read,
Truncate,
Create,
2016-01-04 04:26:12 +00:00
Append,
2015-10-07 11:15:14 +00:00
}
fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) {
2020-06-09 21:44:04 +00:00
if let ExprKind::MethodCall(ref path, _, ref arguments, _) = argument.kind {
let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
2016-01-04 04:26:12 +00:00
2015-10-07 11:15:14 +00:00
// Only proceed if this is a call on some object of type std::fs::OpenOptions
2019-05-17 21:53:54 +00:00
if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
2019-09-27 15:16:06 +00:00
let argument_option = match arguments[1].kind {
2018-07-12 07:30:57 +00:00
ExprKind::Lit(ref span) => {
2017-09-05 09:33:04 +00:00
if let Spanned {
node: LitKind::Bool(lit),
..
} = *span
2017-09-05 09:33:04 +00:00
{
if lit { Argument::True } else { Argument::False }
2015-10-07 11:15:14 +00:00
} else {
// 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 11:15:14 +00:00
}
2016-12-20 17:21:30 +00:00
},
2016-01-04 04:26:12 +00:00
_ => Argument::Unknown,
2015-10-07 11:15:14 +00:00
};
2016-01-04 04:26:12 +00:00
2018-06-28 13:46:58 +00:00
match &*path.ident.as_str() {
2015-10-07 11:15:14 +00:00
"create" => {
2015-10-07 15:15:44 +00:00
options.push((OpenOption::Create, argument_option));
2016-12-20 17:21:30 +00:00
},
2015-10-07 11:15:14 +00:00
"append" => {
2015-10-07 15:15:44 +00:00
options.push((OpenOption::Append, argument_option));
2016-12-20 17:21:30 +00:00
},
2015-10-07 11:15:14 +00:00
"truncate" => {
2015-10-07 15:15:44 +00:00
options.push((OpenOption::Truncate, argument_option));
2016-12-20 17:21:30 +00:00
},
2015-10-07 11:15:14 +00:00
"read" => {
2015-10-07 15:15:44 +00:00
options.push((OpenOption::Read, argument_option));
2016-12-20 17:21:30 +00:00
},
2015-10-07 11:15:14 +00:00
"write" => {
2015-10-07 15:15:44 +00:00
options.push((OpenOption::Write, argument_option));
2016-12-20 17:21:30 +00:00
},
2016-04-14 18:14:03 +00:00
_ => (),
2015-10-07 11:15:14 +00:00
}
2016-01-04 04:26:12 +00:00
2015-10-07 11:15:14 +00:00
get_open_options(cx, &arguments[0], options);
}
}
}
fn check_open_options(cx: &LateContext<'_>, options: &[(OpenOption, Argument)], span: Span) {
let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
2016-12-20 17:21:30 +00: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 07:30:56 +00:00
// This code is almost duplicated (oh, the irony), but I haven't found a way to
// unify it.
for option in options {
match *option {
(OpenOption::Create, arg) => {
if create {
2017-08-09 07:30:56 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"the method `create` is called more than once",
2017-08-09 07:30:56 +00:00
);
} else {
create = true
}
2019-08-01 05:09:57 +00:00
create_arg = create_arg || (arg == Argument::True);
2016-12-20 17:21:30 +00:00
},
(OpenOption::Append, arg) => {
if append {
2017-08-09 07:30:56 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"the method `append` is called more than once",
2017-08-09 07:30:56 +00:00
);
} else {
append = true
}
2019-08-01 05:09:57 +00:00
append_arg = append_arg || (arg == Argument::True);
2016-12-20 17:21:30 +00:00
},
(OpenOption::Truncate, arg) => {
if truncate {
2017-08-09 07:30:56 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"the method `truncate` is called more than once",
2017-08-09 07:30:56 +00:00
);
} else {
truncate = true
}
truncate_arg = truncate_arg || (arg == Argument::True);
2016-12-20 17:21:30 +00:00
},
(OpenOption::Read, arg) => {
if read {
2017-08-09 07:30:56 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"the method `read` is called more than once",
2017-08-09 07:30:56 +00:00
);
} else {
read = true
}
2019-08-01 05:09:57 +00:00
read_arg = read_arg || (arg == Argument::True);
2016-12-20 17:21:30 +00:00
},
(OpenOption::Write, arg) => {
if write {
2017-08-09 07:30:56 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"the method `write` is called more than once",
2017-08-09 07:30:56 +00:00
);
} else {
write = true
}
2019-08-01 05:09:57 +00:00
write_arg = write_arg || (arg == Argument::True);
2016-12-20 17:21:30 +00:00
},
}
2015-10-07 11:15:14 +00:00
}
if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
2018-11-27 20:14:15 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"file opened with `truncate` and `read`",
2018-11-27 20:14:15 +00:00
);
2015-10-07 11:15:14 +00:00
}
if append && truncate && append_arg && truncate_arg {
2017-08-09 07:30:56 +00:00
span_lint(
cx,
NONSENSICAL_OPEN_OPTIONS,
span,
2020-01-06 06:30:43 +00:00
"file opened with `append` and `truncate`",
2017-08-09 07:30:56 +00:00
);
2015-10-07 11:15:14 +00:00
}
}