Skip to content

Commit 9afc8c9

Browse files
author
scott-linder
committed
check for borrowed box types
1 parent b1be0d6 commit 9afc8c9

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ All notable changes to this project will be documented in this file.
275275
[`block_in_if_condition_expr`]: https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_expr
276276
[`block_in_if_condition_stmt`]: https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_stmt
277277
[`bool_comparison`]: https://github.com/Manishearth/rust-clippy/wiki#bool_comparison
278+
[`borrowed_box`]: https://github.com/Manishearth/rust-clippy/wiki#borrowed_box
278279
[`box_vec`]: https://github.com/Manishearth/rust-clippy/wiki#box_vec
279280
[`boxed_local`]: https://github.com/Manishearth/rust-clippy/wiki#boxed_local
280281
[`builtin_type_shadow`]: https://github.com/Manishearth/rust-clippy/wiki#builtin_type_shadow

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ transparently:
180180

181181
## Lints
182182

183-
There are 184 lints included in this crate:
183+
There are 185 lints included in this crate:
184184

185185
name | default | triggers on
186186
-----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -194,6 +194,7 @@ name
194194
[block_in_if_condition_expr](https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_expr) | warn | braces that can be eliminated in conditions, e.g `if { true } ...`
195195
[block_in_if_condition_stmt](https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_stmt) | warn | complex blocks in conditions, e.g. `if { let x = true; x } ...`
196196
[bool_comparison](https://github.com/Manishearth/rust-clippy/wiki#bool_comparison) | warn | comparing a variable to a boolean, e.g. `if x == true`
197+
[borrowed_box](https://github.com/Manishearth/rust-clippy/wiki#borrowed_box) | warn | a borrow of a boxed type
197198
[box_vec](https://github.com/Manishearth/rust-clippy/wiki#box_vec) | warn | usage of `Box<Vec<T>>`, vector elements are already on the heap
198199
[boxed_local](https://github.com/Manishearth/rust-clippy/wiki#boxed_local) | warn | using `Box<T>` where unnecessary
199200
[builtin_type_shadow](https://github.com/Manishearth/rust-clippy/wiki#builtin_type_shadow) | warn | shadowing a builtin type

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
480480
transmute::USELESS_TRANSMUTE,
481481
transmute::WRONG_TRANSMUTE,
482482
types::ABSURD_EXTREME_COMPARISONS,
483+
types::BORROWED_BOX,
483484
types::BOX_VEC,
484485
types::CHAR_LIT_AS_U8,
485486
types::LET_UNIT_VALUE,

clippy_lints/src/types.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,25 @@ declare_lint! {
6363
structure like a VecDeque"
6464
}
6565

66+
/// **What it does:** Checks for use of `&Box<T>` anywhere in the code.
67+
///
68+
/// **Why is this bad?** Any `&Box<T>` can also be a `&T`, which is more general.
69+
///
70+
/// **Known problems:** None.
71+
///
72+
/// **Example:**
73+
/// ```rust
74+
/// fn foo(bar: &Box<T>) { ... }
75+
/// ```
76+
declare_lint! {
77+
pub BORROWED_BOX,
78+
Warn,
79+
"a borrow of a boxed type"
80+
}
81+
6682
impl LintPass for TypePass {
6783
fn get_lints(&self) -> LintArray {
68-
lint_array!(BOX_VEC, LINKEDLIST)
84+
lint_array!(BOX_VEC, LINKEDLIST, BORROWED_BOX)
6985
}
7086
}
7187

@@ -159,11 +175,28 @@ fn check_ty(cx: &LateContext, ast_ty: &Ty) {
159175
},
160176
}
161177
},
178+
TyRptr(_, MutTy { ref ty, .. }) => {
179+
match ty.node {
180+
TyPath(ref qpath) => {
181+
let def = cx.tables.qpath_def(qpath, ast_ty.id);
182+
if let Some(def_id) = opt_def_id(def) {
183+
if Some(def_id) == cx.tcx.lang_items.owned_box() {
184+
span_help_and_lint(cx,
185+
BOX_VEC,
186+
ast_ty.span,
187+
"you seem to be trying to use `&Box<T>`. Consider using just `&T`",
188+
"replace `&Box<T>` with simply `&T`");
189+
return; // don't recurse into the type
190+
}
191+
}
192+
},
193+
_ => check_ty(cx, ty),
194+
}
195+
},
162196
// recurse
163197
TySlice(ref ty) |
164198
TyArray(ref ty, _) |
165-
TyPtr(MutTy { ref ty, .. }) |
166-
TyRptr(_, MutTy { ref ty, .. }) => check_ty(cx, ty),
199+
TyPtr(MutTy { ref ty, .. }) => check_ty(cx, ty),
167200
TyTup(ref tys) => {
168201
for ty in tys {
169202
check_ty(cx, ty);

tests/compile-fail/borrow_box.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#![deny(clippy)]
5+
#![allow(boxed_local)]
6+
#![allow(blacklisted_name)]
7+
8+
pub fn test(foo: &Box<bool>) { //~ ERROR you seem to be trying to use `&Box<T>`
9+
println!("{:?}", foo)
10+
}
11+
12+
fn main(){
13+
test(&Box::new(false));
14+
}

0 commit comments

Comments
 (0)