1 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
2 use rustc_ast::ast::LitKind;
3 use rustc_hir::{Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::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 declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
30 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OpenOptions {
31 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
32 if let ExprKind::MethodCall(ref path, _, ref arguments) = e.kind {
33 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
34 if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
35 let mut options = Vec::new();
36 get_open_options(cx, &arguments[0], &mut options);
37 check_open_options(cx, &options, e.span);
43 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
59 fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) {
60 if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.kind {
61 let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
63 // Only proceed if this is a call on some object of type std::fs::OpenOptions
64 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
65 let argument_option = match arguments[1].kind {
66 ExprKind::Lit(ref span) => {
68 node: LitKind::Bool(lit),
78 return; // The function is called with a literal
79 // which is not a boolean literal. This is theoretically
80 // possible, but not very likely.
83 _ => Argument::Unknown,
86 match &*path.ident.as_str() {
88 options.push((OpenOption::Create, argument_option));
91 options.push((OpenOption::Append, argument_option));
94 options.push((OpenOption::Truncate, argument_option));
97 options.push((OpenOption::Read, argument_option));
100 options.push((OpenOption::Write, argument_option));
105 get_open_options(cx, &arguments[0], options);
110 fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument)], span: Span) {
111 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
112 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
113 (false, false, false, false, false);
114 // This code is almost duplicated (oh, the irony), but I haven't found a way to
117 for option in options {
119 (OpenOption::Create, arg) => {
123 NONSENSICAL_OPEN_OPTIONS,
125 "the method `create` is called more than once",
130 create_arg = create_arg || (arg == Argument::True);
132 (OpenOption::Append, arg) => {
136 NONSENSICAL_OPEN_OPTIONS,
138 "the method `append` is called more than once",
143 append_arg = append_arg || (arg == Argument::True);
145 (OpenOption::Truncate, arg) => {
149 NONSENSICAL_OPEN_OPTIONS,
151 "the method `truncate` is called more than once",
156 truncate_arg = truncate_arg || (arg == Argument::True);
158 (OpenOption::Read, arg) => {
162 NONSENSICAL_OPEN_OPTIONS,
164 "the method `read` is called more than once",
169 read_arg = read_arg || (arg == Argument::True);
171 (OpenOption::Write, arg) => {
175 NONSENSICAL_OPEN_OPTIONS,
177 "the method `write` is called more than once",
182 write_arg = write_arg || (arg == Argument::True);
187 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
190 NONSENSICAL_OPEN_OPTIONS,
192 "file opened with `truncate` and `read`",
195 if append && truncate && append_arg && truncate_arg {
198 NONSENSICAL_OPEN_OPTIONS,
200 "file opened with `append` and `truncate`",