Skip to content

Commit 460b162

Browse files
committed
Remove uses of ast_ty_to_ty_cache
1 parent b8be7f3 commit 460b162

File tree

4 files changed

+36
-39
lines changed

4 files changed

+36
-39
lines changed

src/methods.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{fmt, iter};
1010
use syntax::codemap::Span;
1111
use syntax::ptr::P;
1212
use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, match_path, match_trait_method,
13-
match_type, method_chain_args, returns_self, snippet, snippet_opt, span_lint,
13+
match_type, method_chain_args, return_ty, snippet, snippet_opt, span_lint,
1414
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
1515
use utils::{BTREEMAP_ENTRY_PATH, DEFAULT_TRAIT_PATH, HASHMAP_ENTRY_PATH, OPTION_PATH, RESULT_PATH, STRING_PATH,
1616
VEC_PATH};
@@ -431,7 +431,8 @@ impl LateLintPass for MethodsPass {
431431
}
432432
}
433433

434-
if &name.as_str() == &"new" && !returns_self(cx, &sig.decl.output, ty) {
434+
let ret_ty = return_ty(cx.tcx.node_id_to_type(implitem.id));
435+
if &name.as_str() == &"new" && !ret_ty.map_or(false, |ret_ty| ret_ty.walk().any(|t| t == ty)) {
435436
span_lint(cx,
436437
NEW_RET_NO_SELF,
437438
sig.explicit_self.span,

src/new_without_default.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_front::hir;
33
use rustc_front::intravisit::FnKind;
44
use syntax::ast;
55
use syntax::codemap::Span;
6-
use utils::{get_trait_def_id, implements_trait, in_external_macro, returns_self, span_lint, DEFAULT_TRAIT_PATH};
6+
use utils::{get_trait_def_id, implements_trait, in_external_macro, return_ty, span_lint, DEFAULT_TRAIT_PATH};
77

88
/// **What it does:** This lints about type with a `fn new() -> Self` method and no `Default`
99
/// implementation.
@@ -47,13 +47,15 @@ impl LateLintPass for NewWithoutDefault {
4747

4848
if let FnKind::Method(name, _, _) = kind {
4949
if decl.inputs.is_empty() && name.as_str() == "new" {
50-
let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(cx.tcx.map.get_parent(id))).ty;
50+
let self_ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(cx.tcx.map.get_parent(id))).ty;
5151

52-
if returns_self(cx, &decl.output, ty) {
52+
let ret_ty = return_ty(cx.tcx.node_id_to_type(id));
53+
54+
if Some(self_ty) == ret_ty {
5355
if let Some(default_trait_id) = get_trait_def_id(cx, &DEFAULT_TRAIT_PATH) {
54-
if !implements_trait(cx, ty, default_trait_id, Vec::new()) {
56+
if !implements_trait(cx, self_ty, default_trait_id, Vec::new()) {
5557
span_lint(cx, NEW_WITHOUT_DEFAULT, span,
56-
&format!("you should consider adding a `Default` implementation for `{}`", ty));
58+
&format!("you should consider adding a `Default` implementation for `{}`", self_ty));
5759
}
5860
}
5961
}

src/ptr_arg.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc::front::map::NodeItem;
66
use rustc::lint::*;
77
use rustc::middle::ty;
88
use rustc_front::hir::*;
9+
use syntax::ast::NodeId;
910
use utils::{STRING_PATH, VEC_PATH};
1011
use utils::{span_lint, match_type};
1112

@@ -35,7 +36,7 @@ impl LintPass for PtrArg {
3536
impl LateLintPass for PtrArg {
3637
fn check_item(&mut self, cx: &LateContext, item: &Item) {
3738
if let ItemFn(ref decl, _, _, _, _, _) = item.node {
38-
check_fn(cx, decl);
39+
check_fn(cx, decl, item.id);
3940
}
4041
}
4142

@@ -46,34 +47,34 @@ impl LateLintPass for PtrArg {
4647
return; // ignore trait impls
4748
}
4849
}
49-
check_fn(cx, &sig.decl);
50+
check_fn(cx, &sig.decl, item.id);
5051
}
5152
}
5253

5354
fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
5455
if let MethodTraitItem(ref sig, _) = item.node {
55-
check_fn(cx, &sig.decl);
56+
check_fn(cx, &sig.decl, item.id);
5657
}
5758
}
5859
}
5960

60-
fn check_fn(cx: &LateContext, decl: &FnDecl) {
61-
for arg in &decl.inputs {
62-
if let Some(ty) = cx.tcx.ast_ty_to_ty_cache.borrow().get(&arg.ty.id) {
63-
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = ty.sty {
64-
if match_type(cx, ty, &VEC_PATH) {
65-
span_lint(cx,
66-
PTR_ARG,
67-
arg.ty.span,
68-
"writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
69-
with non-Vec-based slices. Consider changing the type to `&[...]`");
70-
} else if match_type(cx, ty, &STRING_PATH) {
71-
span_lint(cx,
72-
PTR_ARG,
73-
arg.ty.span,
74-
"writing `&String` instead of `&str` involves a new object where a slice will do. \
75-
Consider changing the type to `&str`");
76-
}
61+
fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) {
62+
let fn_ty = cx.tcx.node_id_to_type(fn_id).fn_sig().skip_binder();
63+
64+
for (arg, ty) in decl.inputs.iter().zip(&fn_ty.inputs) {
65+
if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = ty.sty {
66+
if match_type(cx, ty, &VEC_PATH) {
67+
span_lint(cx,
68+
PTR_ARG,
69+
arg.ty.span,
70+
"writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
71+
with non-Vec-based slices. Consider changing the type to `&[...]`");
72+
} else if match_type(cx, ty, &STRING_PATH) {
73+
span_lint(cx,
74+
PTR_ARG,
75+
arg.ty.span,
76+
"writing `&String` instead of `&str` involves a new object where a slice will do. \
77+
Consider changing the type to `&str`");
7778
}
7879
}
7980
}

src/utils/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -732,18 +732,11 @@ pub fn unsugar_range(expr: &Expr) -> Option<UnsugaredRange> {
732732
}
733733
}
734734

735-
/// Return whether a method returns `Self`.
736-
pub fn returns_self(cx: &LateContext, ret: &FunctionRetTy, ty: ty::Ty) -> bool {
737-
if let FunctionRetTy::Return(ref ret_ty) = *ret {
738-
let ast_ty_to_ty_cache = cx.tcx.ast_ty_to_ty_cache.borrow();
739-
let ret_ty = ast_ty_to_ty_cache.get(&ret_ty.id);
740-
741-
if let Some(&ret_ty) = ret_ty {
742-
ret_ty.walk().any(|t| t == ty)
743-
} else {
744-
false
745-
}
735+
/// Convenience function to get the return type of a function or `None` if the function diverges.
736+
pub fn return_ty(fun: ty::Ty) -> Option<ty::Ty> {
737+
if let ty::FnConverging(ret_ty) = fun.fn_sig().skip_binder().output {
738+
Some(ret_ty)
746739
} else {
747-
false
740+
None
748741
}
749742
}

0 commit comments

Comments
 (0)