1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::paths;
3 use clippy_utils::ty::match_type;
4 use rustc_ast::ast::LitKind;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::{Span, Spanned};
10 declare_clippy_lint! {
12 /// Checks for duplicate open options as well as combinations
13 /// that make no sense.
15 /// ### Why is this bad?
16 /// In the best case, the code will be harder to read than
17 /// necessary. I don't know the worst case.
21 /// use std::fs::OpenOptions;
23 /// OpenOptions::new().read(true).truncate(true);
25 #[clippy::version = "pre 1.29.0"]
26 pub NONSENSICAL_OPEN_OPTIONS,
28 "nonsensical combination of options for opening a file"
31 declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
33 impl<'tcx> LateLintPass<'tcx> for OpenOptions {
34 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
35 if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &e.kind {
36 let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
37 if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
38 let mut options = Vec::new();
39 get_open_options(cx, self_arg, &mut options);
40 check_open_options(cx, &options, e.span);
46 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
62 fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) {
63 if let ExprKind::MethodCall(path, _, arguments, _) = argument.kind {
64 let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
66 // Only proceed if this is a call on some object of type std::fs::OpenOptions
67 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
68 let argument_option = match arguments[1].kind {
69 ExprKind::Lit(ref span) => {
71 node: LitKind::Bool(lit),
75 if lit { Argument::True } else { Argument::False }
77 // The function is called with a literal which is not a boolean literal.
78 // This is theoretically possible, but not very likely.
82 _ => Argument::Unknown,
85 match &*path.ident.as_str() {
87 options.push((OpenOption::Create, argument_option));
90 options.push((OpenOption::Append, argument_option));
93 options.push((OpenOption::Truncate, argument_option));
96 options.push((OpenOption::Read, argument_option));
99 options.push((OpenOption::Write, argument_option));
104 get_open_options(cx, &arguments[0], options);
109 fn check_open_options(cx: &LateContext<'_>, options: &[(OpenOption, Argument)], span: Span) {
110 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
111 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
112 (false, false, false, false, false);
113 // This code is almost duplicated (oh, the irony), but I haven't found a way to
116 for option in options {
118 (OpenOption::Create, arg) => {
122 NONSENSICAL_OPEN_OPTIONS,
124 "the method `create` is called more than once",
129 create_arg = create_arg || (arg == Argument::True);
131 (OpenOption::Append, arg) => {
135 NONSENSICAL_OPEN_OPTIONS,
137 "the method `append` is called more than once",
142 append_arg = append_arg || (arg == Argument::True);
144 (OpenOption::Truncate, arg) => {
148 NONSENSICAL_OPEN_OPTIONS,
150 "the method `truncate` is called more than once",
155 truncate_arg = truncate_arg || (arg == Argument::True);
157 (OpenOption::Read, arg) => {
161 NONSENSICAL_OPEN_OPTIONS,
163 "the method `read` is called more than once",
168 read_arg = read_arg || (arg == Argument::True);
170 (OpenOption::Write, arg) => {
174 NONSENSICAL_OPEN_OPTIONS,
176 "the method `write` is called more than once",
181 write_arg = write_arg || (arg == Argument::True);
186 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
189 NONSENSICAL_OPEN_OPTIONS,
191 "file opened with `truncate` and `read`",
194 if append && truncate && append_arg && truncate_arg {
197 NONSENSICAL_OPEN_OPTIONS,
199 "file opened with `append` and `truncate`",