Skip to content

Commit f0fc4c0

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 4c0f20f commit f0fc4c0

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
@@ -99,35 +99,41 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypePass {
9999
}
100100

101101
fn check_struct_field(&mut self, cx: &LateContext, field: &StructField) {
102-
check_ty(cx, &field.ty);
102+
check_ty(cx, &field.ty, false);
103103
}
104104

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

115121
fn check_fn_decl(cx: &LateContext, decl: &FnDecl) {
116122
for input in &decl.inputs {
117-
check_ty(cx, input);
123+
check_ty(cx, input, false);
118124
}
119125

120126
if let FunctionRetTy::Return(ref ty) = decl.output {
121-
check_ty(cx, ty);
127+
check_ty(cx, ty, false);
122128
}
123129
}
124130

125-
fn check_ty(cx: &LateContext, ast_ty: &Ty) {
131+
fn check_ty(cx: &LateContext, ast_ty: &Ty, is_local: bool) {
126132
if in_macro(ast_ty.span) {
127133
return;
128134
}
129135
match ast_ty.node {
130-
TyPath(ref qpath) => {
136+
TyPath(ref qpath) if !is_local => {
131137
let def = cx.tables.qpath_def(qpath, ast_ty.id);
132138
if let Some(def_id) = opt_def_id(def) {
133139
if Some(def_id) == cx.tcx.lang_items.owned_box() {
@@ -158,20 +164,20 @@ fn check_ty(cx: &LateContext, ast_ty: &Ty) {
158164
}
159165
match *qpath {
160166
QPath::Resolved(Some(ref ty), ref p) => {
161-
check_ty(cx, ty);
167+
check_ty(cx, ty, is_local);
162168
for ty in p.segments.iter().flat_map(|seg| seg.parameters.types()) {
163-
check_ty(cx, ty);
169+
check_ty(cx, ty, is_local);
164170
}
165171
},
166172
QPath::Resolved(None, ref p) => {
167173
for ty in p.segments.iter().flat_map(|seg| seg.parameters.types()) {
168-
check_ty(cx, ty);
174+
check_ty(cx, ty, is_local);
169175
}
170176
},
171177
QPath::TypeRelative(ref ty, ref seg) => {
172-
check_ty(cx, ty);
178+
check_ty(cx, ty, is_local);
173179
for ty in seg.parameters.types() {
174-
check_ty(cx, ty);
180+
check_ty(cx, ty, is_local);
175181
}
176182
},
177183
}
@@ -212,18 +218,18 @@ fn check_ty(cx: &LateContext, ast_ty: &Ty) {
212218
}};
213219
}
214220
}
215-
check_ty(cx, ty);
221+
check_ty(cx, ty, is_local);
216222
},
217-
_ => check_ty(cx, ty),
223+
_ => check_ty(cx, ty, is_local),
218224
}
219225
},
220226
// recurse
221227
TySlice(ref ty) |
222228
TyArray(ref ty, _) |
223-
TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty),
229+
TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty, is_local),
224230
TyTup(ref tys) => {
225231
for ty in tys {
226-
check_ty(cx, ty);
232+
check_ty(cx, ty, is_local);
227233
}
228234
},
229235
_ => {},

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)