|
1 | | -use clippy_utils::diagnostics::span_lint; |
2 | | -use clippy_utils::ty::is_type_diagnostic_item; |
| 1 | +use rustc_data_structures::fx::FxHashMap; |
| 2 | + |
| 3 | +use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; |
| 4 | +use clippy_utils::ty::{is_type_diagnostic_item, match_type}; |
| 5 | +use clippy_utils::{match_any_def_paths, paths}; |
3 | 6 | use rustc_ast::ast::LitKind; |
4 | 7 | use rustc_hir::{Expr, ExprKind}; |
5 | 8 | use rustc_lint::LateContext; |
| 9 | +use rustc_middle::ty::Ty; |
6 | 10 | use rustc_span::source_map::Spanned; |
7 | 11 | use rustc_span::{sym, Span}; |
8 | 12 |
|
9 | | -use super::NONSENSICAL_OPEN_OPTIONS; |
| 13 | +use super::{NONSENSICAL_OPEN_OPTIONS, SUSPICIOUS_OPEN_OPTIONS}; |
| 14 | + |
| 15 | +fn is_open_options(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { |
| 16 | + is_type_diagnostic_item(cx, ty, sym::FsOpenOptions) || match_type(cx, ty, &paths::TOKIO_IO_OPEN_OPTIONS) |
| 17 | +} |
10 | 18 |
|
11 | 19 | pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) { |
12 | 20 | if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) |
13 | 21 | && let Some(impl_id) = cx.tcx.impl_of_method(method_id) |
14 | | - && is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::FsOpenOptions) |
| 22 | + && is_open_options(cx, cx.tcx.type_of(impl_id).instantiate_identity()) |
15 | 23 | { |
16 | 24 | let mut options = Vec::new(); |
17 | | - get_open_options(cx, recv, &mut options); |
18 | | - check_open_options(cx, &options, e.span); |
| 25 | + if get_open_options(cx, recv, &mut options) { |
| 26 | + check_open_options(cx, &options, e.span); |
| 27 | + } |
19 | 28 | } |
20 | 29 | } |
21 | 30 |
|
22 | | -#[derive(Debug, PartialEq, Eq, Clone, Copy)] |
| 31 | +#[derive(Eq, PartialEq, Clone, Debug)] |
23 | 32 | enum Argument { |
24 | | - True, |
25 | | - False, |
| 33 | + Set(bool), |
26 | 34 | Unknown, |
27 | 35 | } |
28 | 36 |
|
29 | | -#[derive(Debug)] |
| 37 | +#[derive(Debug, Eq, PartialEq, Hash, Clone)] |
30 | 38 | enum OpenOption { |
31 | | - Write, |
| 39 | + Append, |
| 40 | + Create, |
| 41 | + CreateNew, |
32 | 42 | Read, |
33 | 43 | Truncate, |
34 | | - Create, |
35 | | - Append, |
| 44 | + Write, |
| 45 | +} |
| 46 | +impl std::fmt::Display for OpenOption { |
| 47 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 48 | + match self { |
| 49 | + OpenOption::Append => write!(f, "append"), |
| 50 | + OpenOption::Create => write!(f, "create"), |
| 51 | + OpenOption::CreateNew => write!(f, "create_new"), |
| 52 | + OpenOption::Read => write!(f, "read"), |
| 53 | + OpenOption::Truncate => write!(f, "truncate"), |
| 54 | + OpenOption::Write => write!(f, "write"), |
| 55 | + } |
| 56 | + } |
36 | 57 | } |
37 | 58 |
|
38 | | -fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) { |
39 | | - if let ExprKind::MethodCall(path, receiver, arguments, _) = argument.kind { |
| 59 | +/// Collects information about a method call chain on `OpenOptions`. |
| 60 | +/// Returns false if an unexpected expression kind was found "on the way", |
| 61 | +/// and linting should then be avoided. |
| 62 | +fn get_open_options( |
| 63 | + cx: &LateContext<'_>, |
| 64 | + argument: &Expr<'_>, |
| 65 | + options: &mut Vec<(OpenOption, Argument, Span)>, |
| 66 | +) -> bool { |
| 67 | + if let ExprKind::MethodCall(path, receiver, arguments, span) = argument.kind { |
40 | 68 | let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs(); |
41 | 69 |
|
42 | 70 | // Only proceed if this is a call on some object of type std::fs::OpenOptions |
43 | | - if is_type_diagnostic_item(cx, obj_ty, sym::FsOpenOptions) && !arguments.is_empty() { |
| 71 | + if !arguments.is_empty() && is_open_options(cx, obj_ty) { |
44 | 72 | let argument_option = match arguments[0].kind { |
45 | 73 | ExprKind::Lit(span) => { |
46 | 74 | if let Spanned { |
47 | 75 | node: LitKind::Bool(lit), |
48 | 76 | .. |
49 | 77 | } = span |
50 | 78 | { |
51 | | - if *lit { Argument::True } else { Argument::False } |
| 79 | + Argument::Set(*lit) |
52 | 80 | } else { |
53 | 81 | // The function is called with a literal which is not a boolean literal. |
54 | 82 | // This is theoretically possible, but not very likely. |
55 | | - return; |
| 83 | + // We'll ignore it for now |
| 84 | + return get_open_options(cx, receiver, options); |
56 | 85 | } |
57 | 86 | }, |
58 | 87 | _ => Argument::Unknown, |
59 | 88 | }; |
60 | 89 |
|
61 | 90 | match path.ident.as_str() { |
62 | 91 | "create" => { |
63 | | - options.push((OpenOption::Create, argument_option)); |
| 92 | + options.push((OpenOption::Create, argument_option, span)); |
| 93 | + }, |
| 94 | + "create_new" => { |
| 95 | + options.push((OpenOption::CreateNew, argument_option, span)); |
64 | 96 | }, |
65 | 97 | "append" => { |
66 | | - options.push((OpenOption::Append, argument_option)); |
| 98 | + options.push((OpenOption::Append, argument_option, span)); |
67 | 99 | }, |
68 | 100 | "truncate" => { |
69 | | - options.push((OpenOption::Truncate, argument_option)); |
| 101 | + options.push((OpenOption::Truncate, argument_option, span)); |
70 | 102 | }, |
71 | 103 | "read" => { |
72 | | - options.push((OpenOption::Read, argument_option)); |
| 104 | + options.push((OpenOption::Read, argument_option, span)); |
73 | 105 | }, |
74 | 106 | "write" => { |
75 | | - options.push((OpenOption::Write, argument_option)); |
| 107 | + options.push((OpenOption::Write, argument_option, span)); |
| 108 | + }, |
| 109 | + _ => { |
| 110 | + // Avoid linting altogether if this method is from a trait. |
| 111 | + // This might be a user defined extension trait with a method like `truncate_write` |
| 112 | + // which would be a false positive |
| 113 | + if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(argument.hir_id) |
| 114 | + && cx.tcx.trait_of_item(method_def_id).is_some() |
| 115 | + { |
| 116 | + return false; |
| 117 | + } |
76 | 118 | }, |
77 | | - _ => (), |
78 | 119 | } |
79 | 120 |
|
80 | | - get_open_options(cx, receiver, options); |
| 121 | + get_open_options(cx, receiver, options) |
| 122 | + } else { |
| 123 | + false |
81 | 124 | } |
| 125 | + } else if let ExprKind::Call(callee, _) = argument.kind |
| 126 | + && let ExprKind::Path(path) = callee.kind |
| 127 | + && let Some(did) = cx.qpath_res(&path, callee.hir_id).opt_def_id() |
| 128 | + { |
| 129 | + match_any_def_paths( |
| 130 | + cx, |
| 131 | + did, |
| 132 | + &[ |
| 133 | + &paths::TOKIO_IO_OPEN_OPTIONS_NEW, |
| 134 | + &paths::OPEN_OPTIONS_NEW, |
| 135 | + &paths::FILE_OPTIONS, |
| 136 | + &paths::TOKIO_FILE_OPTIONS, |
| 137 | + ], |
| 138 | + ) |
| 139 | + .is_some() |
| 140 | + } else { |
| 141 | + false |
82 | 142 | } |
83 | 143 | } |
84 | 144 |
|
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 |
90 | | - // unify it. |
91 | | - |
92 | | - for option in options { |
93 | | - match *option { |
94 | | - (OpenOption::Create, arg) => { |
95 | | - if create { |
96 | | - span_lint( |
97 | | - cx, |
98 | | - NONSENSICAL_OPEN_OPTIONS, |
99 | | - span, |
100 | | - "the method `create` is called more than once", |
101 | | - ); |
102 | | - } else { |
103 | | - create = true; |
104 | | - } |
105 | | - create_arg = create_arg || (arg == Argument::True); |
106 | | - }, |
107 | | - (OpenOption::Append, arg) => { |
108 | | - if append { |
109 | | - span_lint( |
110 | | - cx, |
111 | | - NONSENSICAL_OPEN_OPTIONS, |
112 | | - span, |
113 | | - "the method `append` is called more than once", |
114 | | - ); |
115 | | - } else { |
116 | | - append = true; |
117 | | - } |
118 | | - append_arg = append_arg || (arg == Argument::True); |
119 | | - }, |
120 | | - (OpenOption::Truncate, arg) => { |
121 | | - if truncate { |
122 | | - span_lint( |
123 | | - cx, |
124 | | - NONSENSICAL_OPEN_OPTIONS, |
125 | | - span, |
126 | | - "the method `truncate` is called more than once", |
127 | | - ); |
128 | | - } else { |
129 | | - truncate = true; |
130 | | - } |
131 | | - truncate_arg = truncate_arg || (arg == Argument::True); |
132 | | - }, |
133 | | - (OpenOption::Read, arg) => { |
134 | | - if read { |
135 | | - span_lint( |
136 | | - cx, |
137 | | - NONSENSICAL_OPEN_OPTIONS, |
138 | | - span, |
139 | | - "the method `read` is called more than once", |
140 | | - ); |
141 | | - } else { |
142 | | - read = true; |
143 | | - } |
144 | | - read_arg = read_arg || (arg == Argument::True); |
145 | | - }, |
146 | | - (OpenOption::Write, arg) => { |
147 | | - if write { |
148 | | - span_lint( |
149 | | - cx, |
150 | | - NONSENSICAL_OPEN_OPTIONS, |
151 | | - span, |
152 | | - "the method `write` is called more than once", |
153 | | - ); |
154 | | - } else { |
155 | | - write = true; |
156 | | - } |
157 | | - write_arg = write_arg || (arg == Argument::True); |
158 | | - }, |
| 145 | +fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, Span)], span: Span) { |
| 146 | + // The args passed to these methods, if they have been called |
| 147 | + let mut options = FxHashMap::default(); |
| 148 | + for (option, arg, sp) in settings { |
| 149 | + if let Some((_, prev_span)) = options.insert(option.clone(), (arg.clone(), *sp)) { |
| 150 | + span_lint( |
| 151 | + cx, |
| 152 | + NONSENSICAL_OPEN_OPTIONS, |
| 153 | + prev_span, |
| 154 | + &format!("the method `{}` is called more than once", &option), |
| 155 | + ); |
159 | 156 | } |
160 | 157 | } |
161 | 158 |
|
162 | | - if read && truncate && read_arg && truncate_arg && !(write && write_arg) { |
| 159 | + if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Read) |
| 160 | + && let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate) |
| 161 | + && let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Write) |
| 162 | + { |
163 | 163 | span_lint( |
164 | 164 | cx, |
165 | 165 | NONSENSICAL_OPEN_OPTIONS, |
166 | 166 | span, |
167 | 167 | "file opened with `truncate` and `read`", |
168 | 168 | ); |
169 | 169 | } |
170 | | - if append && truncate && append_arg && truncate_arg { |
| 170 | + |
| 171 | + if let Some((Argument::Set(true), _)) = options.get(&OpenOption::Append) |
| 172 | + && let Some((Argument::Set(true), _)) = options.get(&OpenOption::Truncate) |
| 173 | + { |
171 | 174 | span_lint( |
172 | 175 | cx, |
173 | 176 | NONSENSICAL_OPEN_OPTIONS, |
174 | 177 | span, |
175 | 178 | "file opened with `append` and `truncate`", |
176 | 179 | ); |
177 | 180 | } |
| 181 | + |
| 182 | + if let Some((Argument::Set(true), create_span)) = options.get(&OpenOption::Create) |
| 183 | + && let None = options.get(&OpenOption::Truncate) |
| 184 | + && let None | Some((Argument::Set(false), _)) = options.get(&OpenOption::Append) |
| 185 | + { |
| 186 | + span_lint_and_then( |
| 187 | + cx, |
| 188 | + SUSPICIOUS_OPEN_OPTIONS, |
| 189 | + *create_span, |
| 190 | + "file opened with `create`, but `truncate` behavior not defined", |
| 191 | + |diag| { |
| 192 | + diag.span_suggestion( |
| 193 | + create_span.shrink_to_hi(), |
| 194 | + "add", |
| 195 | + ".truncate(true)".to_string(), |
| 196 | + rustc_errors::Applicability::MaybeIncorrect, |
| 197 | + ) |
| 198 | + .help("if you intend to overwrite an existing file entirely, call `.truncate(true)`") |
| 199 | + .help( |
| 200 | + "if you instead know that you may want to keep some parts of the old file, call `.truncate(false)`", |
| 201 | + ) |
| 202 | + .help("alternatively, use `.append(true)` to append to the file instead of overwriting it"); |
| 203 | + }, |
| 204 | + ); |
| 205 | + } |
178 | 206 | } |
0 commit comments