2019-05-13 18:34:08 -05:00
|
|
|
use crate::utils::sym;
|
2019-05-14 03:06:21 -05:00
|
|
|
use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir::{Expr, ExprKind};
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-04-08 15:43:55 -05:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2018-12-29 09:04:45 -06:00
|
|
|
use syntax::ast::LitKind;
|
|
|
|
use syntax::source_map::{Span, Spanned};
|
2015-10-07 06:15:14 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **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 16:23:50 -06:00
|
|
|
/// use std::fs::OpenOptions;
|
|
|
|
///
|
|
|
|
/// OpenOptions::new().read(true).truncate(true);
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2015-10-07 06:15:14 -05:00
|
|
|
pub NONSENSICAL_OPEN_OPTIONS,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2015-10-07 10:15:44 -05:00
|
|
|
"nonsensical combination of options for opening a file"
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
|
2015-10-07 06:15:14 -05:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OpenOptions {
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref arguments) = e.node {
|
2018-03-21 13:10:10 -05:00
|
|
|
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
|
2019-05-13 18:34:08 -05:00
|
|
|
if path.ident.name == *sym::open && match_type(cx, obj_ty, &*paths::OPEN_OPTIONS) {
|
2015-10-07 06:15:14 -05:00
|
|
|
let mut options = Vec::new();
|
|
|
|
get_open_options(cx, &arguments[0], &mut options);
|
|
|
|
check_open_options(cx, &options, e.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-23 06:01:12 -05:00
|
|
|
fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.node {
|
2018-03-21 13:10:10 -05:00
|
|
|
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
|
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
|
2019-05-13 18:34:08 -05:00
|
|
|
if match_type(cx, obj_ty, &*paths::OPEN_OPTIONS) && arguments.len() >= 2 {
|
2015-10-07 06:15:14 -05:00
|
|
|
let argument_option = match arguments[1].node {
|
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
|
|
|
{
|
|
|
|
if lit {
|
|
|
|
Argument::True
|
|
|
|
} else {
|
|
|
|
Argument::False
|
|
|
|
}
|
2015-10-07 06:15:14 -05:00
|
|
|
} else {
|
|
|
|
return; // The function is called with a literal
|
2017-11-04 14:55:56 -05:00
|
|
|
// which is not a boolean literal. This is theoretically
|
|
|
|
// possible, but not very likely.
|
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
|
|
|
|
2018-06-28 08:46:58 -05: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
|
|
|
|
2015-10-07 06:15:14 -05:00
|
|
|
get_open_options(cx, &arguments[0], options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 06:01:12 -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,
|
|
|
|
"the method \"create\" is called more than once",
|
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
|
|
|
create = true
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
"the method \"append\" is called more than once",
|
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
|
|
|
append = true
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
"the method \"truncate\" is called more than once",
|
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
|
|
|
truncate = true
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
"the method \"read\" is called more than once",
|
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
|
|
|
read = true
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
"the method \"write\" is called more than once",
|
|
|
|
);
|
2016-01-03 22:43:56 -06:00
|
|
|
} else {
|
|
|
|
write = true
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
"file opened with \"truncate\" and \"read\"",
|
|
|
|
);
|
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,
|
|
|
|
"file opened with \"append\" and \"truncate\"",
|
|
|
|
);
|
2015-10-07 06:15:14 -05:00
|
|
|
}
|
|
|
|
}
|