Skip to content

Commit 70194d5

Browse files
author
scott-linder
committed
Use span_suggestion in borrowed_box lint
1 parent 953c435 commit 70194d5

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

clippy_lints/src/types.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::cmp::Ordering;
77
use syntax::ast::{IntTy, UintTy, FloatTy};
88
use syntax::attr::IntType;
99
use syntax::codemap::Span;
10-
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint,
10+
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint, span_lint_and_then,
1111
opt_def_id, last_path_segment, type_size};
1212
use utils::paths;
1313

@@ -176,18 +176,40 @@ fn check_ty(cx: &LateContext, ast_ty: &Ty) {
176176
},
177177
}
178178
},
179-
TyRptr(_, MutTy { ref ty, .. }) => {
179+
TyRptr(ref lt, MutTy { ref ty, ref mutbl }) => {
180180
match ty.node {
181181
TyPath(ref qpath) => {
182182
let def = cx.tables.qpath_def(qpath, ast_ty.id);
183183
if let Some(def_id) = opt_def_id(def) {
184184
if Some(def_id) == cx.tcx.lang_items.owned_box() {
185-
span_help_and_lint(cx,
186-
BORROWED_BOX,
187-
ast_ty.span,
188-
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
189-
"replace `&Box<T>` with simply `&T`");
190-
return; // don't recurse into the type
185+
if_let_chain! {[
186+
let &QPath::Resolved(None, ref path) = qpath,
187+
let [ref bx] = *path.segments,
188+
let PathParameters::AngleBracketedParameters(ref ab_data) = bx.parameters,
189+
let [ref inner] = *ab_data.types
190+
], {
191+
let ltopt = if lt.is_elided() {
192+
"".to_owned()
193+
} else {
194+
format!("{} ", lt.name.as_str())
195+
};
196+
let mutopt = if *mutbl == Mutability::MutMutable {
197+
"mut "
198+
} else {
199+
""
200+
};
201+
span_lint_and_then(cx,
202+
BORROWED_BOX,
203+
ast_ty.span,
204+
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
205+
|db| {
206+
db.span_suggestion(ast_ty.span,
207+
"try",
208+
format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, "..")));
209+
}
210+
);
211+
return; // don't recurse into the type
212+
}};
191213
}
192214
}
193215
check_ty(cx, ty);

0 commit comments

Comments
 (0)