mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Reduce cyclomatic complexity of types::check_ty
This commit is contained in:
parent
fabb6b6645
commit
2551bd8924
2 changed files with 55 additions and 48 deletions
|
@ -184,16 +184,16 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
|
|||
check_ty(cx, ty, is_local);
|
||||
for ty in p.segments
|
||||
.iter()
|
||||
.filter_map(|ref seg| seg.parameters.as_ref())
|
||||
.flat_map(|ref params| params.types.iter())
|
||||
.filter_map(|seg| seg.parameters.as_ref())
|
||||
.flat_map(|params| params.types.iter())
|
||||
{
|
||||
check_ty(cx, ty, is_local);
|
||||
}
|
||||
},
|
||||
QPath::Resolved(None, ref p) => for ty in p.segments
|
||||
.iter()
|
||||
.filter_map(|ref seg| seg.parameters.as_ref())
|
||||
.flat_map(|ref params| params.types.iter())
|
||||
.filter_map(|seg| seg.parameters.as_ref())
|
||||
.flat_map(|params| params.types.iter())
|
||||
{
|
||||
check_ty(cx, ty, is_local);
|
||||
},
|
||||
|
@ -207,49 +207,7 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
|
|||
},
|
||||
}
|
||||
},
|
||||
TyRptr(ref lt, MutTy { ref ty, ref mutbl }) => {
|
||||
match ty.node {
|
||||
TyPath(ref qpath) => {
|
||||
let hir_id = cx.tcx.hir.node_to_hir_id(ty.id);
|
||||
let def = cx.tables.qpath_def(qpath, hir_id);
|
||||
if_let_chain! {[
|
||||
let Some(def_id) = opt_def_id(def),
|
||||
Some(def_id) == cx.tcx.lang_items().owned_box(),
|
||||
let QPath::Resolved(None, ref path) = *qpath,
|
||||
let [ref bx] = *path.segments,
|
||||
let Some(ref params) = bx.parameters,
|
||||
!params.parenthesized,
|
||||
let [ref inner] = *params.types
|
||||
], {
|
||||
if is_any_trait(inner) {
|
||||
// Ignore `Box<Any>` types, see #1884 for details.
|
||||
return;
|
||||
}
|
||||
|
||||
let ltopt = if lt.is_elided() {
|
||||
"".to_owned()
|
||||
} else {
|
||||
format!("{} ", lt.name.name().as_str())
|
||||
};
|
||||
let mutopt = if *mutbl == Mutability::MutMutable {
|
||||
"mut "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
span_lint_and_sugg(cx,
|
||||
BORROWED_BOX,
|
||||
ast_ty.span,
|
||||
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
|
||||
"try",
|
||||
format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, ".."))
|
||||
);
|
||||
return; // don't recurse into the type
|
||||
}};
|
||||
check_ty(cx, ty, is_local);
|
||||
},
|
||||
_ => check_ty(cx, ty, is_local),
|
||||
}
|
||||
},
|
||||
TyRptr(ref lt, ref mut_ty) => check_ty_rptr(cx, ast_ty, is_local, lt, mut_ty),
|
||||
// recurse
|
||||
TySlice(ref ty) | TyArray(ref ty, _) | TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty, is_local),
|
||||
TyTup(ref tys) => for ty in tys {
|
||||
|
@ -259,6 +217,50 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_ty_rptr(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool, lt: &Lifetime, mut_ty: &MutTy) {
|
||||
match mut_ty.ty.node {
|
||||
TyPath(ref qpath) => {
|
||||
let hir_id = cx.tcx.hir.node_to_hir_id(mut_ty.ty.id);
|
||||
let def = cx.tables.qpath_def(qpath, hir_id);
|
||||
if_let_chain! {[
|
||||
let Some(def_id) = opt_def_id(def),
|
||||
Some(def_id) == cx.tcx.lang_items().owned_box(),
|
||||
let QPath::Resolved(None, ref path) = *qpath,
|
||||
let [ref bx] = *path.segments,
|
||||
let Some(ref params) = bx.parameters,
|
||||
!params.parenthesized,
|
||||
let [ref inner] = *params.types
|
||||
], {
|
||||
if is_any_trait(inner) {
|
||||
// Ignore `Box<Any>` types, see #1884 for details.
|
||||
return;
|
||||
}
|
||||
|
||||
let ltopt = if lt.is_elided() {
|
||||
"".to_owned()
|
||||
} else {
|
||||
format!("{} ", lt.name.name().as_str())
|
||||
};
|
||||
let mutopt = if mut_ty.mutbl == Mutability::MutMutable {
|
||||
"mut "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
span_lint_and_sugg(cx,
|
||||
BORROWED_BOX,
|
||||
ast_ty.span,
|
||||
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
|
||||
"try",
|
||||
format!("&{}{}{}", ltopt, mutopt, &snippet(cx, inner.span, ".."))
|
||||
);
|
||||
return; // don't recurse into the type
|
||||
}};
|
||||
check_ty(cx, &mut_ty.ty, is_local);
|
||||
},
|
||||
_ => check_ty(cx, &mut_ty.ty, is_local),
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if given type is `Any` trait.
|
||||
fn is_any_trait(t: &hir::Ty) -> bool {
|
||||
if_let_chain! {[
|
||||
|
|
|
@ -59,7 +59,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
|
|||
let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
|
||||
], {
|
||||
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters;
|
||||
if parameters.is_none() {
|
||||
let should_check = if let Some(ref params) = *parameters {
|
||||
!params.parenthesized && params.lifetimes.len() == 0
|
||||
} else {
|
||||
true
|
||||
};
|
||||
if should_check {
|
||||
let visitor = &mut UseSelfVisitor {
|
||||
item_path: item_path,
|
||||
cx: cx,
|
||||
|
|
Loading…
Reference in a new issue