Skip to content

Commit 74ebe6e

Browse files
author
scott-linder
committed
Add check_local to TypePass for BORROWED_BOX
Adds a boolean flag to indicate whether the current type in `check_ty` is in a local declaration, as only the borrowed box lint should consider these types.
1 parent c29f5ea commit 74ebe6e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

clippy_lints/src/types.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -100,35 +100,41 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypePass {
100100
}
101101

102102
fn check_struct_field(&mut self, cx: &LateContext, field: &StructField) {
103-
check_ty(cx, &field.ty);
103+
check_ty(cx, &field.ty, false);
104104
}
105105

106106
fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
107107
match item.node {
108108
TraitItemKind::Const(ref ty, _) |
109-
TraitItemKind::Type(_, Some(ref ty)) => check_ty(cx, ty),
109+
TraitItemKind::Type(_, Some(ref ty)) => check_ty(cx, ty, false),
110110
TraitItemKind::Method(ref sig, _) => check_fn_decl(cx, &sig.decl),
111111
_ => (),
112112
}
113113
}
114+
115+
fn check_local(&mut self, cx: &LateContext, local: &Local) {
116+
if let Some(ref ty) = local.ty {
117+
check_ty(cx, ty, true);
118+
}
119+
}
114120
}
115121

116122
fn check_fn_decl(cx: &LateContext, decl: &FnDecl) {
117123
for input in &decl.inputs {
118-
check_ty(cx, input);
124+
check_ty(cx, input, false);
119125
}
120126

121127
if let FunctionRetTy::Return(ref ty) = decl.output {
122-
check_ty(cx, ty);
128+
check_ty(cx, ty, false);
123129
}
124130
}
125131

126-
fn check_ty(cx: &LateContext, ast_ty: &hir::Ty) {
132+
fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) {
127133
if in_macro(ast_ty.span) {
128134
return;
129135
}
130136
match ast_ty.node {
131-
TyPath(ref qpath) => {
137+
TyPath(ref qpath) if !is_local => {
132138
let def = cx.tables.qpath_def(qpath, ast_ty.id);
133139
if let Some(def_id) = opt_def_id(def) {
134140
if Some(def_id) == cx.tcx.lang_items.owned_box() {
@@ -159,20 +165,20 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty) {
159165
}
160166
match *qpath {
161167
QPath::Resolved(Some(ref ty), ref p) => {
162-
check_ty(cx, ty);
168+
check_ty(cx, ty, is_local);
163169
for ty in p.segments.iter().flat_map(|seg| seg.parameters.types()) {
164-
check_ty(cx, ty);
170+
check_ty(cx, ty, is_local);
165171
}
166172
},
167173
QPath::Resolved(None, ref p) => {
168174
for ty in p.segments.iter().flat_map(|seg| seg.parameters.types()) {
169-
check_ty(cx, ty);
175+
check_ty(cx, ty, is_local);
170176
}
171177
},
172178
QPath::TypeRelative(ref ty, ref seg) => {
173-
check_ty(cx, ty);
179+
check_ty(cx, ty, is_local);
174180
for ty in seg.parameters.types() {
175-
check_ty(cx, ty);
181+
check_ty(cx, ty, is_local);
176182
}
177183
},
178184
}
@@ -213,18 +219,18 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty) {
213219
}};
214220
}
215221
}
216-
check_ty(cx, ty);
222+
check_ty(cx, ty, is_local);
217223
},
218-
_ => check_ty(cx, ty),
224+
_ => check_ty(cx, ty, is_local),
219225
}
220226
},
221227
// recurse
222228
TySlice(ref ty) |
223229
TyArray(ref ty, _) |
224-
TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty),
230+
TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty, is_local),
225231
TyTup(ref tys) => {
226232
for ty in tys {
227-
check_ty(cx, ty);
233+
check_ty(cx, ty, is_local);
228234
}
229235
},
230236
_ => {},

clippy_tests/examples/borrow_box.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ note: lint level defined here
1010
4 | #![deny(borrowed_box)]
1111
| ^^^^^^^^^^^^
1212

13+
error: you seem to be trying to use `&Box<T>`. Consider using just `&T`
14+
--> borrow_box.rs:14:14
15+
|
16+
14 | let foo: &Box<bool>;
17+
| ^^^^^^^^^^ help: try `&bool`
18+
1319
error: you seem to be trying to use `&Box<T>`. Consider using just `&T`
1420
--> borrow_box.rs:18:10
1521
|

0 commit comments

Comments
 (0)