]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/open_options.rs
Rustup
[rust.git] / clippy_lints / src / open_options.rs
1 use rustc::hir::{Expr, ExprLit, ExprMethodCall};
2 use rustc::lint::*;
3 use syntax::ast::LitKind;
4 use syntax::codemap::{Span, Spanned};
5 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
6
7 /// **What it does:** Checks for duplicate open options as well as combinations
8 /// that make no sense.
9 ///
10 /// **Why is this bad?** In the best case, the code will be harder to read than
11 /// necessary. I don't know the worst case.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// OpenOptions::new().read(true).truncate(true)
18 /// ```
19 declare_clippy_lint! {
20     pub NONSENSICAL_OPEN_OPTIONS,
21     correctness,
22     "nonsensical combination of options for opening a file"
23 }
24
25 #[derive(Copy, Clone)]
26 pub struct NonSensical;
27
28 impl LintPass for NonSensical {
29     fn get_lints(&self) -> LintArray {
30         lint_array!(NONSENSICAL_OPEN_OPTIONS)
31     }
32 }
33
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
35     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36         if let ExprMethodCall(ref path, _, ref arguments) = e.node {
37             let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
38             if path.ident.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
39                 let mut options = Vec::new();
40                 get_open_options(cx, &arguments[0], &mut options);
41                 check_open_options(cx, &options, e.span);
42             }
43         }
44     }
45 }
46
47 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
48 enum Argument {
49     True,
50     False,
51     Unknown,
52 }
53
54 #[derive(Debug)]
55 enum OpenOption {
56     Write,
57     Read,
58     Truncate,
59     Create,
60     Append,
61 }
62
63 fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
64     if let ExprMethodCall(ref path, _, ref arguments) = argument.node {
65         let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
66
67         // Only proceed if this is a call on some object of type std::fs::OpenOptions
68         if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
69             let argument_option = match arguments[1].node {
70                 ExprLit(ref span) => {
71                     if let Spanned {
72                         node: LitKind::Bool(lit),
73                         ..
74                     } = **span
75                     {
76                         if lit {
77                             Argument::True
78                         } else {
79                             Argument::False
80                         }
81                     } else {
82                         return; // The function is called with a literal
83                                 // which is not a boolean literal. This is theoretically
84                                 // possible, but not very likely.
85                     }
86                 },
87                 _ => Argument::Unknown,
88             };
89
90             match &*path.ident.as_str() {
91                 "create" => {
92                     options.push((OpenOption::Create, argument_option));
93                 },
94                 "append" => {
95                     options.push((OpenOption::Append, argument_option));
96                 },
97                 "truncate" => {
98                     options.push((OpenOption::Truncate, argument_option));
99                 },
100                 "read" => {
101                     options.push((OpenOption::Read, argument_option));
102                 },
103                 "write" => {
104                     options.push((OpenOption::Write, argument_option));
105                 },
106                 _ => (),
107             }
108
109             get_open_options(cx, &arguments[0], options);
110         }
111     }
112 }
113
114 fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) {
115     let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
116     let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
117         (false, false, false, false, false);
118     // This code is almost duplicated (oh, the irony), but I haven't found a way to
119     // unify it.
120
121     for option in options {
122         match *option {
123             (OpenOption::Create, arg) => {
124                 if create {
125                     span_lint(
126                         cx,
127                         NONSENSICAL_OPEN_OPTIONS,
128                         span,
129                         "the method \"create\" is called more than once",
130                     );
131                 } else {
132                     create = true
133                 }
134                 create_arg = create_arg || (arg == Argument::True);;
135             },
136             (OpenOption::Append, arg) => {
137                 if append {
138                     span_lint(
139                         cx,
140                         NONSENSICAL_OPEN_OPTIONS,
141                         span,
142                         "the method \"append\" is called more than once",
143                     );
144                 } else {
145                     append = true
146                 }
147                 append_arg = append_arg || (arg == Argument::True);;
148             },
149             (OpenOption::Truncate, arg) => {
150                 if truncate {
151                     span_lint(
152                         cx,
153                         NONSENSICAL_OPEN_OPTIONS,
154                         span,
155                         "the method \"truncate\" is called more than once",
156                     );
157                 } else {
158                     truncate = true
159                 }
160                 truncate_arg = truncate_arg || (arg == Argument::True);
161             },
162             (OpenOption::Read, arg) => {
163                 if read {
164                     span_lint(
165                         cx,
166                         NONSENSICAL_OPEN_OPTIONS,
167                         span,
168                         "the method \"read\" is called more than once",
169                     );
170                 } else {
171                     read = true
172                 }
173                 read_arg = read_arg || (arg == Argument::True);;
174             },
175             (OpenOption::Write, arg) => {
176                 if write {
177                     span_lint(
178                         cx,
179                         NONSENSICAL_OPEN_OPTIONS,
180                         span,
181                         "the method \"write\" is called more than once",
182                     );
183                 } else {
184                     write = true
185                 }
186                 write_arg = write_arg || (arg == Argument::True);;
187             },
188         }
189     }
190
191     if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
192         span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "file opened with \"truncate\" and \"read\"");
193     }
194     if append && truncate && append_arg && truncate_arg {
195         span_lint(
196             cx,
197             NONSENSICAL_OPEN_OPTIONS,
198             span,
199             "file opened with \"append\" and \"truncate\"",
200         );
201     }
202 }