ptr_arg: fix panic when pattern type is not in tcx

This commit is contained in:
Georg Brandl 2015-08-22 08:57:05 +02:00
parent e1a4248e68
commit e92bf84a53
1 changed files with 12 additions and 14 deletions

View File

@ -45,20 +45,18 @@ impl LintPass for PtrArg {
fn check_fn(cx: &Context, decl: &FnDecl) {
for arg in &decl.inputs {
if arg.ty.node == TyInfer { // "self" arguments
continue;
}
let ref sty = cx.tcx.pat_ty(&*arg.pat).sty;
if let &ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = sty {
if match_type(cx, ty, &VEC_PATH) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&Vec<_>` instead of `&[_]` involves one more reference \
and cannot be used with non-Vec-based slices. Consider changing \
the type to `&[...]`");
} else if match_type(cx, ty, &STRING_PATH) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&String` instead of `&str` involves a new object \
where a slice will do. Consider changing the type to `&str`");
if let Some(pat_ty) = cx.tcx.pat_ty_opt(&*arg.pat) {
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = pat_ty.sty {
if match_type(cx, ty, &VEC_PATH) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&Vec<_>` instead of `&[_]` involves one more reference \
and cannot be used with non-Vec-based slices. Consider changing \
the type to `&[...]`");
} else if match_type(cx, ty, &STRING_PATH) {
span_lint(cx, PTR_ARG, arg.ty.span,
"writing `&String` instead of `&str` involves a new object \
where a slice will do. Consider changing the type to `&str`");
}
}
}
}