1 use rustc::hir::{Expr, ExprKind};
3 use rustc::{declare_lint, lint_array};
4 use syntax::ast::LitKind;
5 use syntax::codemap::{Span, Spanned};
6 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
8 /// **What it does:** Checks for duplicate open options as well as combinations
9 /// that make no sense.
11 /// **Why is this bad?** In the best case, the code will be harder to read than
12 /// necessary. I don't know the worst case.
14 /// **Known problems:** None.
18 /// OpenOptions::new().read(true).truncate(true)
20 declare_clippy_lint! {
21 pub NONSENSICAL_OPEN_OPTIONS,
23 "nonsensical combination of options for opening a file"
26 #[derive(Copy, Clone)]
27 pub struct NonSensical;
29 impl LintPass for NonSensical {
30 fn get_lints(&self) -> LintArray {
31 lint_array!(NONSENSICAL_OPEN_OPTIONS)
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
36 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
37 if let ExprKind::MethodCall(ref path, _, ref arguments) = e.node {
38 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
39 if path.ident.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
40 let mut options = Vec::new();
41 get_open_options(cx, &arguments[0], &mut options);
42 check_open_options(cx, &options, e.span);
48 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
64 fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
65 if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.node {
66 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
68 // Only proceed if this is a call on some object of type std::fs::OpenOptions
69 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
70 let argument_option = match arguments[1].node {
71 ExprKind::Lit(ref span) => {
73 node: LitKind::Bool(lit),
83 return; // The function is called with a literal
84 // which is not a boolean literal. This is theoretically
85 // possible, but not very likely.
88 _ => Argument::Unknown,
91 match &*path.ident.as_str() {
93 options.push((OpenOption::Create, argument_option));
96 options.push((OpenOption::Append, argument_option));
99 options.push((OpenOption::Truncate, argument_option));
102 options.push((OpenOption::Read, argument_option));
105 options.push((OpenOption::Write, argument_option));
110 get_open_options(cx, &arguments[0], options);
115 fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) {
116 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
117 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
118 (false, false, false, false, false);
119 // This code is almost duplicated (oh, the irony), but I haven't found a way to
122 for option in options {
124 (OpenOption::Create, arg) => {
128 NONSENSICAL_OPEN_OPTIONS,
130 "the method \"create\" is called more than once",
135 create_arg = create_arg || (arg == Argument::True);;
137 (OpenOption::Append, arg) => {
141 NONSENSICAL_OPEN_OPTIONS,
143 "the method \"append\" is called more than once",
148 append_arg = append_arg || (arg == Argument::True);;
150 (OpenOption::Truncate, arg) => {
154 NONSENSICAL_OPEN_OPTIONS,
156 "the method \"truncate\" is called more than once",
161 truncate_arg = truncate_arg || (arg == Argument::True);
163 (OpenOption::Read, arg) => {
167 NONSENSICAL_OPEN_OPTIONS,
169 "the method \"read\" is called more than once",
174 read_arg = read_arg || (arg == Argument::True);;
176 (OpenOption::Write, arg) => {
180 NONSENSICAL_OPEN_OPTIONS,
182 "the method \"write\" is called more than once",
187 write_arg = write_arg || (arg == Argument::True);;
192 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
193 span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "file opened with \"truncate\" and \"read\"");
195 if append && truncate && append_arg && truncate_arg {
198 NONSENSICAL_OPEN_OPTIONS,
200 "file opened with \"append\" and \"truncate\"",