|
1 | 1 | use crate::utils::{ |
2 | | - in_macro, 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; |
@@ -166,24 +167,28 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr, expr_span: Sp |
166 | 167 | fn check_replace_with_default(cx: &LateContext<'_, '_>, src: &Expr, dest: &Expr, expr_span: Span) { |
167 | 168 | if let ExprKind::Call(ref repl_func, _) = src.kind { |
168 | 169 | if_chain! { |
169 | | - if !in_macro(expr_span) && !in_external_macro(cx.tcx.sess, expr_span); |
| 170 | + if !in_external_macro(cx.tcx.sess, expr_span); |
170 | 171 | if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind; |
171 | 172 | if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id(); |
172 | 173 | if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD); |
173 | 174 | then { |
174 | | - let mut applicability = Applicability::MachineApplicable; |
175 | | - |
176 | | - span_lint_and_sugg( |
| 175 | + span_lint_and_then( |
177 | 176 | cx, |
178 | 177 | MEM_REPLACE_WITH_DEFAULT, |
179 | 178 | expr_span, |
180 | 179 | "replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`", |
181 | | - "consider using", |
182 | | - format!( |
183 | | - "std::mem::take({})", |
184 | | - snippet_with_applicability(cx, dest.span, "", &mut applicability) |
185 | | - ), |
186 | | - applicability, |
| 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 | + } |
187 | 192 | ); |
188 | 193 | } |
189 | 194 | } |
|
0 commit comments