1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::rustc::hir::{Expr, ExprKind};
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::syntax::ast::LitKind;
14 use crate::syntax::source_map::{Span, Spanned};
15 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
17 /// **What it does:** Checks for duplicate open options as well as combinations
18 /// that make no sense.
20 /// **Why is this bad?** In the best case, the code will be harder to read than
21 /// necessary. I don't know the worst case.
23 /// **Known problems:** None.
27 /// OpenOptions::new().read(true).truncate(true)
29 declare_clippy_lint! {
30 pub NONSENSICAL_OPEN_OPTIONS,
32 "nonsensical combination of options for opening a file"
35 #[derive(Copy, Clone)]
36 pub struct NonSensical;
38 impl LintPass for NonSensical {
39 fn get_lints(&self) -> LintArray {
40 lint_array!(NONSENSICAL_OPEN_OPTIONS)
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
45 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
46 if let ExprKind::MethodCall(ref path, _, ref arguments) = e.node {
47 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
48 if path.ident.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
49 let mut options = Vec::new();
50 get_open_options(cx, &arguments[0], &mut options);
51 check_open_options(cx, &options, e.span);
57 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
73 fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
74 if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.node {
75 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
77 // Only proceed if this is a call on some object of type std::fs::OpenOptions
78 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
79 let argument_option = match arguments[1].node {
80 ExprKind::Lit(ref span) => {
82 node: LitKind::Bool(lit),
92 return; // The function is called with a literal
93 // which is not a boolean literal. This is theoretically
94 // possible, but not very likely.
97 _ => Argument::Unknown,
100 match &*path.ident.as_str() {
102 options.push((OpenOption::Create, argument_option));
105 options.push((OpenOption::Append, argument_option));
108 options.push((OpenOption::Truncate, argument_option));
111 options.push((OpenOption::Read, argument_option));
114 options.push((OpenOption::Write, argument_option));
119 get_open_options(cx, &arguments[0], options);
124 fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument)], span: Span) {
125 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
126 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
127 (false, false, false, false, false);
128 // This code is almost duplicated (oh, the irony), but I haven't found a way to
131 for option in options {
133 (OpenOption::Create, arg) => {
137 NONSENSICAL_OPEN_OPTIONS,
139 "the method \"create\" is called more than once",
144 create_arg = create_arg || (arg == Argument::True);;
146 (OpenOption::Append, arg) => {
150 NONSENSICAL_OPEN_OPTIONS,
152 "the method \"append\" is called more than once",
157 append_arg = append_arg || (arg == Argument::True);;
159 (OpenOption::Truncate, arg) => {
163 NONSENSICAL_OPEN_OPTIONS,
165 "the method \"truncate\" is called more than once",
170 truncate_arg = truncate_arg || (arg == Argument::True);
172 (OpenOption::Read, arg) => {
176 NONSENSICAL_OPEN_OPTIONS,
178 "the method \"read\" is called more than once",
183 read_arg = read_arg || (arg == Argument::True);;
185 (OpenOption::Write, arg) => {
189 NONSENSICAL_OPEN_OPTIONS,
191 "the method \"write\" is called more than once",
196 write_arg = write_arg || (arg == Argument::True);;
201 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
204 NONSENSICAL_OPEN_OPTIONS,
206 "file opened with \"truncate\" and \"read\"",
209 if append && truncate && append_arg && truncate_arg {
212 NONSENSICAL_OPEN_OPTIONS,
214 "file opened with \"append\" and \"truncate\"",