|
1 | 1 | use crate::utils::{
|
2 |
| - match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg, |
| 2 | + in_macro, match_def_path, match_qpath, paths, snippet, snippet_with_applicability, span_help_and_lint, |
| 3 | + span_lint_and_sugg, span_lint_and_then, |
3 | 4 | };
|
4 | 5 | use if_chain::if_chain;
|
5 | 6 | use rustc::declare_lint_pass;
|
6 | 7 | use rustc::hir::{BorrowKind, Expr, ExprKind, Mutability, QPath};
|
7 |
| -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 8 | +use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass}; |
8 | 9 | use rustc_errors::Applicability;
|
9 | 10 | use rustc_session::declare_tool_lint;
|
| 11 | +use syntax::source_map::Span; |
10 | 12 |
|
11 | 13 | declare_clippy_lint! {
|
12 | 14 | /// **What it does:** Checks for `mem::replace()` on an `Option` with
|
@@ -67,80 +69,145 @@ declare_clippy_lint! {
|
67 | 69 | "`mem::replace(&mut _, mem::uninitialized())` or `mem::replace(&mut _, mem::zeroed())`"
|
68 | 70 | }
|
69 | 71 |
|
| 72 | +declare_clippy_lint! { |
| 73 | + /// **What it does:** Checks for `std::mem::replace` on a value of type |
| 74 | + /// `T` with `T::default()`. |
| 75 | + /// |
| 76 | + /// **Why is this bad?** `std::mem` module already has the method `take` to |
| 77 | + /// take the current value and replace it with the default value of that type. |
| 78 | + /// |
| 79 | + /// **Known problems:** None. |
| 80 | + /// |
| 81 | + /// **Example:** |
| 82 | + /// ```rust |
| 83 | + /// let mut text = String::from("foo"); |
| 84 | + /// let replaced = std::mem::replace(&mut text, String::default()); |
| 85 | + /// ``` |
| 86 | + /// Is better expressed with: |
| 87 | + /// ```rust |
| 88 | + /// let mut text = String::from("foo"); |
| 89 | + /// let taken = std::mem::take(&mut text); |
| 90 | + /// ``` |
| 91 | + pub MEM_REPLACE_WITH_DEFAULT, |
| 92 | + style, |
| 93 | + "replacing a value of type `T` with `T::default()` instead of using `std::mem::take`" |
| 94 | +} |
| 95 | + |
70 | 96 | declare_lint_pass!(MemReplace =>
|
71 |
| - [MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_WITH_UNINIT]); |
| 97 | + [MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_WITH_UNINIT, MEM_REPLACE_WITH_DEFAULT]); |
| 98 | + |
| 99 | +fn check_replace_option_with_none(cx: &LateContext<'_, '_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) { |
| 100 | + if let ExprKind::Path(ref replacement_qpath) = src.kind { |
| 101 | + // Check that second argument is `Option::None` |
| 102 | + if match_qpath(replacement_qpath, &paths::OPTION_NONE) { |
| 103 | + // Since this is a late pass (already type-checked), |
| 104 | + // and we already know that the second argument is an |
| 105 | + // `Option`, we do not need to check the first |
| 106 | + // argument's type. All that's left is to get |
| 107 | + // replacee's path. |
| 108 | + let replaced_path = match dest.kind { |
| 109 | + ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, ref replaced) => { |
| 110 | + if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.kind { |
| 111 | + replaced_path |
| 112 | + } else { |
| 113 | + return; |
| 114 | + } |
| 115 | + }, |
| 116 | + ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path, |
| 117 | + _ => return, |
| 118 | + }; |
| 119 | + |
| 120 | + let mut applicability = Applicability::MachineApplicable; |
| 121 | + span_lint_and_sugg( |
| 122 | + cx, |
| 123 | + MEM_REPLACE_OPTION_WITH_NONE, |
| 124 | + expr_span, |
| 125 | + "replacing an `Option` with `None`", |
| 126 | + "consider `Option::take()` instead", |
| 127 | + format!( |
| 128 | + "{}.take()", |
| 129 | + snippet_with_applicability(cx, replaced_path.span, "", &mut applicability) |
| 130 | + ), |
| 131 | + applicability, |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr<'_>, expr_span: Span) { |
| 138 | + if let ExprKind::Call(ref repl_func, ref repl_args) = src.kind { |
| 139 | + if_chain! { |
| 140 | + if repl_args.is_empty(); |
| 141 | + if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind; |
| 142 | + if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id(); |
| 143 | + then { |
| 144 | + if match_def_path(cx, repl_def_id, &paths::MEM_UNINITIALIZED) { |
| 145 | + span_help_and_lint( |
| 146 | + cx, |
| 147 | + MEM_REPLACE_WITH_UNINIT, |
| 148 | + expr_span, |
| 149 | + "replacing with `mem::uninitialized()`", |
| 150 | + "consider using the `take_mut` crate instead", |
| 151 | + ); |
| 152 | + } else if match_def_path(cx, repl_def_id, &paths::MEM_ZEROED) && |
| 153 | + !cx.tables.expr_ty(src).is_primitive() { |
| 154 | + span_help_and_lint( |
| 155 | + cx, |
| 156 | + MEM_REPLACE_WITH_UNINIT, |
| 157 | + expr_span, |
| 158 | + "replacing with `mem::zeroed()`", |
| 159 | + "consider using a default value or the `take_mut` crate instead", |
| 160 | + ); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +fn check_replace_with_default(cx: &LateContext<'_, '_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) { |
| 168 | + if let ExprKind::Call(ref repl_func, _) = src.kind { |
| 169 | + if_chain! { |
| 170 | + if !in_external_macro(cx.tcx.sess, expr_span); |
| 171 | + if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind; |
| 172 | + if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id(); |
| 173 | + if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD); |
| 174 | + then { |
| 175 | + span_lint_and_then( |
| 176 | + cx, |
| 177 | + MEM_REPLACE_WITH_DEFAULT, |
| 178 | + expr_span, |
| 179 | + "replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`", |
| 180 | + |db| { |
| 181 | + if !in_macro(expr_span) { |
| 182 | + let suggestion = format!("std::mem::take({})", snippet(cx, dest.span, "")); |
| 183 | + |
| 184 | + db.span_suggestion( |
| 185 | + expr_span, |
| 186 | + "consider using", |
| 187 | + suggestion, |
| 188 | + Applicability::MachineApplicable |
| 189 | + ); |
| 190 | + } |
| 191 | + } |
| 192 | + ); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
72 | 197 |
|
73 | 198 | impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
|
74 | 199 | fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
75 | 200 | if_chain! {
|
76 | 201 | // Check that `expr` is a call to `mem::replace()`
|
77 | 202 | if let ExprKind::Call(ref func, ref func_args) = expr.kind;
|
78 |
| - if func_args.len() == 2; |
79 | 203 | if let ExprKind::Path(ref func_qpath) = func.kind;
|
80 | 204 | if let Some(def_id) = cx.tables.qpath_res(func_qpath, func.hir_id).opt_def_id();
|
81 | 205 | if match_def_path(cx, def_id, &paths::MEM_REPLACE);
|
82 |
| - |
83 |
| - // Check that second argument is `Option::None` |
| 206 | + if let [dest, src] = &**func_args; |
84 | 207 | then {
|
85 |
| - if let ExprKind::Path(ref replacement_qpath) = func_args[1].kind { |
86 |
| - if match_qpath(replacement_qpath, &paths::OPTION_NONE) { |
87 |
| - |
88 |
| - // Since this is a late pass (already type-checked), |
89 |
| - // and we already know that the second argument is an |
90 |
| - // `Option`, we do not need to check the first |
91 |
| - // argument's type. All that's left is to get |
92 |
| - // replacee's path. |
93 |
| - let replaced_path = match func_args[0].kind { |
94 |
| - ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, ref replaced) => { |
95 |
| - if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.kind { |
96 |
| - replaced_path |
97 |
| - } else { |
98 |
| - return |
99 |
| - } |
100 |
| - }, |
101 |
| - ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path, |
102 |
| - _ => return, |
103 |
| - }; |
104 |
| - |
105 |
| - let mut applicability = Applicability::MachineApplicable; |
106 |
| - span_lint_and_sugg( |
107 |
| - cx, |
108 |
| - MEM_REPLACE_OPTION_WITH_NONE, |
109 |
| - expr.span, |
110 |
| - "replacing an `Option` with `None`", |
111 |
| - "consider `Option::take()` instead", |
112 |
| - format!("{}.take()", snippet_with_applicability(cx, replaced_path.span, "", &mut applicability)), |
113 |
| - applicability, |
114 |
| - ); |
115 |
| - } |
116 |
| - } |
117 |
| - if let ExprKind::Call(ref repl_func, ref repl_args) = func_args[1].kind { |
118 |
| - if_chain! { |
119 |
| - if repl_args.is_empty(); |
120 |
| - if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind; |
121 |
| - if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id(); |
122 |
| - then { |
123 |
| - if match_def_path(cx, repl_def_id, &paths::MEM_UNINITIALIZED) { |
124 |
| - span_help_and_lint( |
125 |
| - cx, |
126 |
| - MEM_REPLACE_WITH_UNINIT, |
127 |
| - expr.span, |
128 |
| - "replacing with `mem::uninitialized()`", |
129 |
| - "consider using the `take_mut` crate instead", |
130 |
| - ); |
131 |
| - } else if match_def_path(cx, repl_def_id, &paths::MEM_ZEROED) && |
132 |
| - !cx.tables.expr_ty(&func_args[1]).is_primitive() { |
133 |
| - span_help_and_lint( |
134 |
| - cx, |
135 |
| - MEM_REPLACE_WITH_UNINIT, |
136 |
| - expr.span, |
137 |
| - "replacing with `mem::zeroed()`", |
138 |
| - "consider using a default value or the `take_mut` crate instead", |
139 |
| - ); |
140 |
| - } |
141 |
| - } |
142 |
| - } |
143 |
| - } |
| 208 | + check_replace_option_with_none(cx, src, dest, expr.span); |
| 209 | + check_replace_with_uninit(cx, src, expr.span); |
| 210 | + check_replace_with_default(cx, src, dest, expr.span); |
144 | 211 | }
|
145 | 212 | }
|
146 | 213 | }
|
|
0 commit comments