1 use crate::utils::{match_type, paths, span_lint};
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<'tcx> LateLintPass<'tcx> for OpenOptions {
31 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
32 if let ExprKind::MethodCall(ref path, _, ref arguments, _) = e.kind {
33 let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
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 = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
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),
72 if lit { Argument::True } else { Argument::False }
74 // The function is called with a literal which is not a boolean literal.
75 // This is theoretically possible, but not very likely.
79 _ => Argument::Unknown,
82 match &*path.ident.as_str() {
84 options.push((OpenOption::Create, argument_option));
87 options.push((OpenOption::Append, argument_option));
90 options.push((OpenOption::Truncate, argument_option));
93 options.push((OpenOption::Read, argument_option));
96 options.push((OpenOption::Write, argument_option));
101 get_open_options(cx, &arguments[0], options);
106 fn check_open_options(cx: &LateContext<'_>, options: &[(OpenOption, Argument)], span: Span) {
107 let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
108 let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
109 (false, false, false, false, false);
110 // This code is almost duplicated (oh, the irony), but I haven't found a way to
113 for option in options {
115 (OpenOption::Create, arg) => {
119 NONSENSICAL_OPEN_OPTIONS,
121 "the method `create` is called more than once",
126 create_arg = create_arg || (arg == Argument::True);
128 (OpenOption::Append, arg) => {
132 NONSENSICAL_OPEN_OPTIONS,
134 "the method `append` is called more than once",
139 append_arg = append_arg || (arg == Argument::True);
141 (OpenOption::Truncate, arg) => {
145 NONSENSICAL_OPEN_OPTIONS,
147 "the method `truncate` is called more than once",
152 truncate_arg = truncate_arg || (arg == Argument::True);
154 (OpenOption::Read, arg) => {
158 NONSENSICAL_OPEN_OPTIONS,
160 "the method `read` is called more than once",
165 read_arg = read_arg || (arg == Argument::True);
167 (OpenOption::Write, arg) => {
171 NONSENSICAL_OPEN_OPTIONS,
173 "the method `write` is called more than once",
178 write_arg = write_arg || (arg == Argument::True);
183 if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
186 NONSENSICAL_OPEN_OPTIONS,
188 "file opened with `truncate` and `read`",
191 if append && truncate && append_arg && truncate_arg {
194 NONSENSICAL_OPEN_OPTIONS,
196 "file opened with `append` and `truncate`",