1 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
2 use rustc::declare_lint_pass;
3 use rustc::hir::{Expr, ExprKind};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc_session::declare_tool_lint;
6 use syntax::ast::LitKind;
7 use syntax::source_map::{Span, Spanned};
10 /// **What it does:** Checks for duplicate open options as well as combinations
11 /// that make no sense.
13 /// **Why is this bad?** In the best case, the code will be harder to read than
14 /// necessary. I don't know the worst case.
16 /// **Known problems:** None.
20 /// use std::fs::OpenOptions;
22 /// OpenOptions::new().read(true).truncate(true);
24 pub NONSENSICAL_OPEN_OPTIONS,
26 "nonsensical combination of options for opening a file"
29 declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
31 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OpenOptions {
32 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
33 if let ExprKind::MethodCall(ref path, _, ref arguments) = e.kind {
34 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
35 if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
36 let mut options = Vec::new();
37 get_open_options(cx, &arguments[0], &mut options);
38 check_open_options(cx, &options, e.span);
44 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
60 fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
61 if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.kind {
62 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
64 // Only proceed if this is a call on some object of type std::fs::OpenOptions
65 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
66 let argument_option = match arguments[1].kind {
67 ExprKind::Lit(ref span) => {
69 node: LitKind::Bool(lit),
79 return; // The function is called with a literal
80 // which is not a boolean literal. This is theoretically
81 // possible, but not very likely.
84 _ => Argument::Unknown,
87 match &*path.ident.as_str() {
89 options.push((OpenOption::Create, argument_option));
92 options.push((OpenOption::Append, argument_option));
95 options.push((OpenOption::Truncate, argument_option));
98 options.push((OpenOption::Read, argument_option));
101 options.push((OpenOption::Write, argument_option));
106 get_open_options(cx, &arguments[0], options);
111 fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument)], span: Span) {
112 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
113 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
114 (false, false, false, false, false);
115 // This code is almost duplicated (oh, the irony), but I haven't found a way to
118 for option in options {
120 (OpenOption::Create, arg) => {
124 NONSENSICAL_OPEN_OPTIONS,
126 "the method \"create\" is called more than once",
131 create_arg = create_arg || (arg == Argument::True);
133 (OpenOption::Append, arg) => {
137 NONSENSICAL_OPEN_OPTIONS,
139 "the method \"append\" is called more than once",
144 append_arg = append_arg || (arg == Argument::True);
146 (OpenOption::Truncate, arg) => {
150 NONSENSICAL_OPEN_OPTIONS,
152 "the method \"truncate\" is called more than once",
157 truncate_arg = truncate_arg || (arg == Argument::True);
159 (OpenOption::Read, arg) => {
163 NONSENSICAL_OPEN_OPTIONS,
165 "the method \"read\" is called more than once",
170 read_arg = read_arg || (arg == Argument::True);
172 (OpenOption::Write, arg) => {
176 NONSENSICAL_OPEN_OPTIONS,
178 "the method \"write\" is called more than once",
183 write_arg = write_arg || (arg == Argument::True);
188 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
191 NONSENSICAL_OPEN_OPTIONS,
193 "file opened with \"truncate\" and \"read\"",
196 if append && truncate && append_arg && truncate_arg {
199 NONSENSICAL_OPEN_OPTIONS,
201 "file opened with \"append\" and \"truncate\"",