1 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
2 use rustc::hir::{Expr, ExprKind};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5 use syntax::ast::LitKind;
6 use syntax::source_map::{Span, Spanned};
9 /// **What it does:** Checks for duplicate open options as well as combinations
10 /// that make no sense.
12 /// **Why is this bad?** In the best case, the code will be harder to read than
13 /// necessary. I don't know the worst case.
15 /// **Known problems:** None.
19 /// use std::fs::OpenOptions;
21 /// OpenOptions::new().read(true).truncate(true);
23 pub NONSENSICAL_OPEN_OPTIONS,
25 "nonsensical combination of options for opening a file"
28 #[derive(Copy, Clone)]
29 pub struct NonSensical;
31 impl LintPass for NonSensical {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(NONSENSICAL_OPEN_OPTIONS)
36 fn name(&self) -> &'static str {
41 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
42 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
43 if let ExprKind::MethodCall(ref path, _, ref arguments) = e.node {
44 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
45 if path.ident.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
46 let mut options = Vec::new();
47 get_open_options(cx, &arguments[0], &mut options);
48 check_open_options(cx, &options, e.span);
54 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
70 fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
71 if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.node {
72 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
74 // Only proceed if this is a call on some object of type std::fs::OpenOptions
75 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
76 let argument_option = match arguments[1].node {
77 ExprKind::Lit(ref span) => {
79 node: LitKind::Bool(lit),
89 return; // The function is called with a literal
90 // which is not a boolean literal. This is theoretically
91 // possible, but not very likely.
94 _ => Argument::Unknown,
97 match &*path.ident.as_str() {
99 options.push((OpenOption::Create, argument_option));
102 options.push((OpenOption::Append, argument_option));
105 options.push((OpenOption::Truncate, argument_option));
108 options.push((OpenOption::Read, argument_option));
111 options.push((OpenOption::Write, argument_option));
116 get_open_options(cx, &arguments[0], options);
121 fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument)], span: Span) {
122 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
123 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
124 (false, false, false, false, false);
125 // This code is almost duplicated (oh, the irony), but I haven't found a way to
128 for option in options {
130 (OpenOption::Create, arg) => {
134 NONSENSICAL_OPEN_OPTIONS,
136 "the method \"create\" is called more than once",
141 create_arg = create_arg || (arg == Argument::True);;
143 (OpenOption::Append, arg) => {
147 NONSENSICAL_OPEN_OPTIONS,
149 "the method \"append\" is called more than once",
154 append_arg = append_arg || (arg == Argument::True);;
156 (OpenOption::Truncate, arg) => {
160 NONSENSICAL_OPEN_OPTIONS,
162 "the method \"truncate\" is called more than once",
167 truncate_arg = truncate_arg || (arg == Argument::True);
169 (OpenOption::Read, arg) => {
173 NONSENSICAL_OPEN_OPTIONS,
175 "the method \"read\" is called more than once",
180 read_arg = read_arg || (arg == Argument::True);;
182 (OpenOption::Write, arg) => {
186 NONSENSICAL_OPEN_OPTIONS,
188 "the method \"write\" is called more than once",
193 write_arg = write_arg || (arg == Argument::True);;
198 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
201 NONSENSICAL_OPEN_OPTIONS,
203 "file opened with \"truncate\" and \"read\"",
206 if append && truncate && append_arg && truncate_arg {
209 NONSENSICAL_OPEN_OPTIONS,
211 "file opened with \"append\" and \"truncate\"",