Skip to content

Commit

Permalink
Use span_suggestion in borrowed_box lint
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-linder committed Jun 11, 2017
1 parent 663688f commit deef81a
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions clippy_lints/src/types.rs
Expand Up @@ -8,7 +8,7 @@ use std::cmp::Ordering;
use syntax::ast::{IntTy, UintTy, FloatTy};
use syntax::attr::IntType;
use syntax::codemap::Span;
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint,
use utils::{comparisons, higher, in_external_macro, in_macro, match_def_path, snippet, span_help_and_lint, span_lint, span_lint_and_then,
opt_def_id, last_path_segment, type_size};
use utils::paths;

Expand Down Expand Up @@ -177,18 +177,40 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty) {
},
}
},
TyRptr(_, MutTy { ref ty, .. }) => {
TyRptr(ref lt, MutTy { ref ty, ref mutbl }) => {
match ty.node {
TyPath(ref qpath) => {
let def = cx.tables.qpath_def(qpath, ast_ty.id);
if let Some(def_id) = opt_def_id(def) {
if Some(def_id) == cx.tcx.lang_items.owned_box() {
span_help_and_lint(cx,
BORROWED_BOX,
ast_ty.span,
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
"replace `&Box<T>` with simply `&T`");
return; // don't recurse into the type
if_let_chain! {[
let &QPath::Resolved(None, ref path) = qpath,
let [ref bx] = *path.segments,
let PathParameters::AngleBracketedParameters(ref ab_data) = bx.parameters,
let [ref inner] = *ab_data.types
], {
let ltopt = if lt.is_elided() {
"".to_owned()
} else {
format!("{} ", lt.name.as_str())
};
let mutopt = if *mutbl == Mutability::MutMutable {
"mut "
} else {
""
};
span_lint_and_then(cx,
BORROWED_BOX,
ast_ty.span,
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
|db| {
db.span_suggestion(ast_ty.span,
"try",
format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, "..")));
}
);
return; // don't recurse into the type
}};
}
}
check_ty(cx, ty);
Expand Down

0 comments on commit deef81a

Please sign in to comment.