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};
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)
34 fn name(&self) -> &'static str {
39 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
40 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
41 if let ExprKind::MethodCall(ref path, _, ref arguments) = e.node {
42 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
43 if path.ident.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
44 let mut options = Vec::new();
45 get_open_options(cx, &arguments[0], &mut options);
46 check_open_options(cx, &options, e.span);
52 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
68 fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
69 if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.node {
70 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
72 // Only proceed if this is a call on some object of type std::fs::OpenOptions
73 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
74 let argument_option = match arguments[1].node {
75 ExprKind::Lit(ref span) => {
77 node: LitKind::Bool(lit),
87 return; // The function is called with a literal
88 // which is not a boolean literal. This is theoretically
89 // possible, but not very likely.
92 _ => Argument::Unknown,
95 match &*path.ident.as_str() {
97 options.push((OpenOption::Create, argument_option));
100 options.push((OpenOption::Append, argument_option));
103 options.push((OpenOption::Truncate, argument_option));
106 options.push((OpenOption::Read, argument_option));
109 options.push((OpenOption::Write, argument_option));
114 get_open_options(cx, &arguments[0], options);
119 fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument)], span: Span) {
120 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
121 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
122 (false, false, false, false, false);
123 // This code is almost duplicated (oh, the irony), but I haven't found a way to
126 for option in options {
128 (OpenOption::Create, arg) => {
132 NONSENSICAL_OPEN_OPTIONS,
134 "the method \"create\" is called more than once",
139 create_arg = create_arg || (arg == Argument::True);;
141 (OpenOption::Append, arg) => {
145 NONSENSICAL_OPEN_OPTIONS,
147 "the method \"append\" is called more than once",
152 append_arg = append_arg || (arg == Argument::True);;
154 (OpenOption::Truncate, arg) => {
158 NONSENSICAL_OPEN_OPTIONS,
160 "the method \"truncate\" is called more than once",
165 truncate_arg = truncate_arg || (arg == Argument::True);
167 (OpenOption::Read, arg) => {
171 NONSENSICAL_OPEN_OPTIONS,
173 "the method \"read\" is called more than once",
178 read_arg = read_arg || (arg == Argument::True);;
180 (OpenOption::Write, arg) => {
184 NONSENSICAL_OPEN_OPTIONS,
186 "the method \"write\" is called more than once",
191 write_arg = write_arg || (arg == Argument::True);;
196 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
199 NONSENSICAL_OPEN_OPTIONS,
201 "file opened with \"truncate\" and \"read\"",
204 if append && truncate && append_arg && truncate_arg {
207 NONSENSICAL_OPEN_OPTIONS,
209 "file opened with \"append\" and \"truncate\"",