1 use crate::utils::{paths, span_lint};
2 use clippy_utils::ty::match_type;
3 use rustc_ast::ast::LitKind;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::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<'tcx> LateLintPass<'tcx> for OpenOptions {
32 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
33 if let ExprKind::MethodCall(ref path, _, ref arguments, _) = e.kind {
34 let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
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 = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
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),
73 if lit { Argument::True } else { Argument::False }
75 // The function is called with a literal which is not a boolean literal.
76 // This is theoretically possible, but not very likely.
80 _ => Argument::Unknown,
83 match &*path.ident.as_str() {
85 options.push((OpenOption::Create, argument_option));
88 options.push((OpenOption::Append, argument_option));
91 options.push((OpenOption::Truncate, argument_option));
94 options.push((OpenOption::Read, argument_option));
97 options.push((OpenOption::Write, argument_option));
102 get_open_options(cx, &arguments[0], options);
107 fn check_open_options(cx: &LateContext<'_>, options: &[(OpenOption, Argument)], span: Span) {
108 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
109 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
110 (false, false, false, false, false);
111 // This code is almost duplicated (oh, the irony), but I haven't found a way to
114 for option in options {
116 (OpenOption::Create, arg) => {
120 NONSENSICAL_OPEN_OPTIONS,
122 "the method `create` is called more than once",
127 create_arg = create_arg || (arg == Argument::True);
129 (OpenOption::Append, arg) => {
133 NONSENSICAL_OPEN_OPTIONS,
135 "the method `append` is called more than once",
140 append_arg = append_arg || (arg == Argument::True);
142 (OpenOption::Truncate, arg) => {
146 NONSENSICAL_OPEN_OPTIONS,
148 "the method `truncate` is called more than once",
153 truncate_arg = truncate_arg || (arg == Argument::True);
155 (OpenOption::Read, arg) => {
159 NONSENSICAL_OPEN_OPTIONS,
161 "the method `read` is called more than once",
166 read_arg = read_arg || (arg == Argument::True);
168 (OpenOption::Write, arg) => {
172 NONSENSICAL_OPEN_OPTIONS,
174 "the method `write` is called more than once",
179 write_arg = write_arg || (arg == Argument::True);
184 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
187 NONSENSICAL_OPEN_OPTIONS,
189 "file opened with `truncate` and `read`",
192 if append && truncate && append_arg && truncate_arg {
195 NONSENSICAL_OPEN_OPTIONS,
197 "file opened with `append` and `truncate`",