2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::{Expr, ExprMethodCall, ExprLit};
|
2016-04-14 16:13:15 +00:00
|
|
|
use rustc::lint::*;
|
2016-02-12 17:35:44 +00:00
|
|
|
use syntax::ast::LitKind;
|
2016-02-24 16:38:57 +00:00
|
|
|
use syntax::codemap::{Span, Spanned};
|
2016-04-14 16:13:15 +00:00
|
|
|
use utils::{match_type, paths, span_lint, walk_ptrs_ty_depth};
|
2015-10-07 11:15:14 +00:00
|
|
|
|
2016-02-05 23:41:54 +00:00
|
|
|
/// **What it does:** This lint checks for duplicate open options as well as combinations that make no sense.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
|
|
|
/// **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:** `OpenOptions::new().read(true).truncate(true)`
|
2015-10-07 11:15:14 +00:00
|
|
|
declare_lint! {
|
|
|
|
pub NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
Warn,
|
2015-10-07 15:15:44 +00:00
|
|
|
"nonsensical combination of options for opening a file"
|
2015-10-07 11:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct NonSensicalOpenOptions;
|
|
|
|
|
|
|
|
impl LintPass for NonSensicalOpenOptions {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NONSENSICAL_OPEN_OPTIONS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass for NonSensicalOpenOptions {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
|
|
|
|
if let ExprMethodCall(ref name, _, ref arguments) = e.node {
|
|
|
|
let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&arguments[0]));
|
2016-04-14 16:13:15 +00:00
|
|
|
if name.node.as_str() == "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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 04:43:56 +00:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2015-10-07 15:15:44 +00:00
|
|
|
fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
|
2015-10-07 11:15:14 +00:00
|
|
|
if let ExprMethodCall(ref name, _, ref arguments) = argument.node {
|
|
|
|
let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&arguments[0]));
|
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
|
2016-04-14 16:13:15 +00:00
|
|
|
if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
|
2016-01-04 04:26:12 +00:00
|
|
|
|
2015-10-07 11:15:14 +00:00
|
|
|
let argument_option = match arguments[1].node {
|
|
|
|
ExprLit(ref span) => {
|
2016-02-12 17:35:44 +00:00
|
|
|
if let Spanned {node: LitKind::Bool(lit), ..} = **span {
|
2016-01-04 04:26:12 +00:00
|
|
|
if lit {
|
|
|
|
Argument::True
|
|
|
|
} else {
|
|
|
|
Argument::False
|
|
|
|
}
|
2015-10-07 11:15:14 +00:00
|
|
|
} else {
|
|
|
|
return; // The function is called with a literal
|
|
|
|
// which is not a boolean literal. This is theoretically
|
|
|
|
// possible, but not very likely.
|
|
|
|
}
|
2015-11-17 04:39:42 +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
|
|
|
|
2015-10-07 11:15:14 +00:00
|
|
|
match &*name.node.as_str() {
|
|
|
|
"create" => {
|
2015-10-07 15:15:44 +00:00
|
|
|
options.push((OpenOption::Create, argument_option));
|
2015-11-17 04:39:42 +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));
|
2015-11-17 04:39:42 +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));
|
2015-11-17 04:39:42 +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));
|
2015-11-17 04:39:42 +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));
|
2015-11-17 04:39:42 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 04:43:56 +00:00
|
|
|
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);
|
|
|
|
let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) = (false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false);
|
2015-10-07 11:15:14 +00:00
|
|
|
// This code is almost duplicated (oh, the irony), but I haven't found a way to unify it.
|
2016-01-04 04:43:56 +00:00
|
|
|
|
|
|
|
for option in options {
|
|
|
|
match *option {
|
|
|
|
(OpenOption::Create, arg) => {
|
|
|
|
if create {
|
|
|
|
span_lint(cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2016-02-05 20:54:29 +00:00
|
|
|
"the method \"create\" is called more than once");
|
2016-01-04 04:43:56 +00:00
|
|
|
} else {
|
|
|
|
create = true
|
|
|
|
}
|
|
|
|
create_arg = create_arg || (arg == Argument::True);;
|
|
|
|
}
|
|
|
|
(OpenOption::Append, arg) => {
|
|
|
|
if append {
|
|
|
|
span_lint(cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2016-02-05 20:54:29 +00:00
|
|
|
"the method \"append\" is called more than once");
|
2016-01-04 04:43:56 +00:00
|
|
|
} else {
|
|
|
|
append = true
|
|
|
|
}
|
|
|
|
append_arg = append_arg || (arg == Argument::True);;
|
|
|
|
}
|
|
|
|
(OpenOption::Truncate, arg) => {
|
|
|
|
if truncate {
|
|
|
|
span_lint(cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2016-02-05 20:54:29 +00:00
|
|
|
"the method \"truncate\" is called more than once");
|
2016-01-04 04:43:56 +00:00
|
|
|
} else {
|
|
|
|
truncate = true
|
|
|
|
}
|
|
|
|
truncate_arg = truncate_arg || (arg == Argument::True);
|
|
|
|
}
|
|
|
|
(OpenOption::Read, arg) => {
|
|
|
|
if read {
|
|
|
|
span_lint(cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2016-02-05 20:54:29 +00:00
|
|
|
"the method \"read\" is called more than once");
|
2016-01-04 04:43:56 +00:00
|
|
|
} else {
|
|
|
|
read = true
|
|
|
|
}
|
|
|
|
read_arg = read_arg || (arg == Argument::True);;
|
|
|
|
}
|
|
|
|
(OpenOption::Write, arg) => {
|
|
|
|
if write {
|
|
|
|
span_lint(cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2016-02-05 20:54:29 +00:00
|
|
|
"the method \"write\" is called more than once");
|
2016-01-04 04:43:56 +00:00
|
|
|
} else {
|
|
|
|
write = true
|
|
|
|
}
|
|
|
|
write_arg = write_arg || (arg == Argument::True);;
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 11:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 04:43:56 +00:00
|
|
|
if read && truncate && read_arg && truncate_arg {
|
2016-02-05 20:54:29 +00:00
|
|
|
span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "file opened with \"truncate\" and \"read\"");
|
2015-10-07 11:15:14 +00:00
|
|
|
}
|
2016-01-04 04:43:56 +00:00
|
|
|
if append && truncate && append_arg && truncate_arg {
|
2016-01-04 04:26:12 +00:00
|
|
|
span_lint(cx,
|
|
|
|
NONSENSICAL_OPEN_OPTIONS,
|
|
|
|
span,
|
2016-02-05 20:54:29 +00:00
|
|
|
"file opened with \"append\" and \"truncate\"");
|
2015-10-07 11:15:14 +00:00
|
|
|
}
|
|
|
|
}
|