Skip to content

Commit deef81a

Browse files
author
scott-linder
committed
Use span_suggestion in borrowed_box lint
1 parent 663688f commit deef81a

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
@@ -8,7 +8,7 @@ use std::cmp::Ordering;
88
use syntax::ast::{IntTy, UintTy, FloatTy};
99
use syntax::attr::IntType;
1010
use syntax::codemap::Span;
11-
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint,
11+
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint, span_lint_and_then,
1212
opt_def_id, last_path_segment, type_size};
1313
use utils::paths;
1414

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

0 commit comments

Comments
 (0)