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;
7 use rustc_span::source_map::{Span, Spanned};
9 use super::NONSENSICAL_OPEN_OPTIONS;
11 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
12 if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
13 && let Some(impl_id) = cx.tcx.impl_of_method(method_id)
14 && match_type(cx, cx.tcx.type_of(impl_id), &paths::OPEN_OPTIONS)
16 let mut options = Vec::new();
17 get_open_options(cx, recv, &mut options);
18 check_open_options(cx, &options, e.span);
22 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
38 fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) {
39 if let ExprKind::MethodCall(path, receiver, arguments, _) = argument.kind {
40 let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();
42 // Only proceed if this is a call on some object of type std::fs::OpenOptions
43 if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && !arguments.is_empty() {
44 let argument_option = match arguments[0].kind {
45 ExprKind::Lit(ref span) => {
47 node: LitKind::Bool(lit),
51 if lit { Argument::True } else { Argument::False }
53 // The function is called with a literal which is not a boolean literal.
54 // This is theoretically possible, but not very likely.
58 _ => Argument::Unknown,
61 match path.ident.as_str() {
63 options.push((OpenOption::Create, argument_option));
66 options.push((OpenOption::Append, argument_option));
69 options.push((OpenOption::Truncate, argument_option));
72 options.push((OpenOption::Read, argument_option));
75 options.push((OpenOption::Write, argument_option));
80 get_open_options(cx, receiver, options);
85 fn check_open_options(cx: &LateContext<'_>, options: &[(OpenOption, Argument)], span: Span) {
86 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
87 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
88 (false, false, false, false, false);
89 // This code is almost duplicated (oh, the irony), but I haven't found a way to
92 for option in options {
94 (OpenOption::Create, arg) => {
98 NONSENSICAL_OPEN_OPTIONS,
100 "the method `create` is called more than once",
105 create_arg = create_arg || (arg == Argument::True);
107 (OpenOption::Append, arg) => {
111 NONSENSICAL_OPEN_OPTIONS,
113 "the method `append` is called more than once",
118 append_arg = append_arg || (arg == Argument::True);
120 (OpenOption::Truncate, arg) => {
124 NONSENSICAL_OPEN_OPTIONS,
126 "the method `truncate` is called more than once",
131 truncate_arg = truncate_arg || (arg == Argument::True);
133 (OpenOption::Read, arg) => {
137 NONSENSICAL_OPEN_OPTIONS,
139 "the method `read` is called more than once",
144 read_arg = read_arg || (arg == Argument::True);
146 (OpenOption::Write, arg) => {
150 NONSENSICAL_OPEN_OPTIONS,
152 "the method `write` is called more than once",
157 write_arg = write_arg || (arg == Argument::True);
162 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
165 NONSENSICAL_OPEN_OPTIONS,
167 "file opened with `truncate` and `read`",
170 if append && truncate && append_arg && truncate_arg {
173 NONSENSICAL_OPEN_OPTIONS,
175 "file opened with `append` and `truncate`",