Skip to content

Commit bfa0ff8

Browse files
committed
useless Rc<Rc<T>>, Rc<Box<T>>
1 parent f19700b commit bfa0ff8

File tree

8 files changed

+127
-3
lines changed

8 files changed

+127
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ Released 2018-09-13
14331433
[`range_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one
14341434
[`range_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_step_by_zero
14351435
[`range_zip_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_zip_with_len
1436+
[`rc_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#rc_box
1437+
[`rc_rc`]: https://rust-lang.github.io/rust-clippy/master/index.html#rc_rc
14361438
[`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
14371439
[`redundant_closure`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
14381440
[`redundant_closure_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
77

8-
[There are 362 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 364 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
99

1010
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1111

clippy_lints/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
805805
&types::LET_UNIT_VALUE,
806806
&types::LINKEDLIST,
807807
&types::OPTION_OPTION,
808+
&types::RC_BOX,
809+
&types::RC_RC,
808810
&types::TYPE_COMPLEXITY,
809811
&types::UNIT_ARG,
810812
&types::UNIT_CMP,
@@ -1369,6 +1371,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13691371
LintId::of(&types::IMPLICIT_HASHER),
13701372
LintId::of(&types::LET_UNIT_VALUE),
13711373
LintId::of(&types::OPTION_OPTION),
1374+
LintId::of(&types::RC_BOX),
1375+
LintId::of(&types::RC_RC),
13721376
LintId::of(&types::TYPE_COMPLEXITY),
13731377
LintId::of(&types::UNIT_ARG),
13741378
LintId::of(&types::UNIT_CMP),
@@ -1654,6 +1658,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16541658
LintId::of(&trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF),
16551659
LintId::of(&types::BOX_BORROWS),
16561660
LintId::of(&types::BOX_VEC),
1661+
LintId::of(&types::RC_BOX),
1662+
LintId::of(&types::RC_RC),
16571663
LintId::of(&vec::USELESS_VEC),
16581664
]);
16591665

clippy_lints/src/types.rs

+74-1
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,63 @@ declare_clippy_lint! {
194194
"a box of borrowed type"
195195
}
196196

197+
declare_clippy_lint! {
198+
/// **What it does:** Checks for use of `Rc<Rc<T>>` anywhere in the code.
199+
///
200+
/// **Why is this bad?** Reference counting pointer of reference counting pointer
201+
/// is an unnecessary level of indirection.
202+
///
203+
/// **Known problems:** None.
204+
///
205+
/// **Example:**
206+
/// ```rust
207+
/// # use std::rc::Rc;
208+
/// fn foo(bar: Rc<Rc<usize>>) {}
209+
/// ```
210+
///
211+
/// Better:
212+
///
213+
/// ```rust
214+
/// # use std::rc::Rc;
215+
/// fn foo(bar: Rc<usize>) {}
216+
/// ```
217+
pub RC_RC,
218+
perf,
219+
"an Rc of Rc"
220+
}
221+
222+
declare_clippy_lint! {
223+
/// **What it does:** Checks for use of `Rc<Box<T>>` anywhere in the code.
224+
///
225+
/// **Why is this bad?** `Rc` already keeps its contents in a separate area on
226+
/// the heap. So if you `Box` its contents, you just add another level of indirection.
227+
///
228+
/// **Known problems:** None.
229+
///
230+
/// **Example:**
231+
/// ```rust
232+
/// # use std::rc::Rc;
233+
/// # use std::boxed::Box;
234+
/// fn foo(bar: Rc<Box<usize>>) {}
235+
/// ```
236+
///
237+
/// Better:
238+
///
239+
/// ```rust
240+
/// # use std::rc::Rc;
241+
/// # use std::boxed::Box;
242+
/// fn foo(bar: Rc<usize>) {}
243+
/// ```
244+
pub RC_BOX,
245+
perf,
246+
"an Rc of Box"
247+
}
248+
197249
pub struct Types {
198250
vec_box_size_threshold: u64,
199251
}
200252

201-
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, BOX_BORROWS]);
253+
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, BOX_BORROWS, RC_RC, RC_BOX]);
202254

203255
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Types {
204256
fn check_fn(
@@ -322,6 +374,27 @@ impl Types {
322374
);
323375
return; // don't recurse into the type
324376
}
377+
} else if Some(def_id) == cx.tcx.lang_items().rc() {
378+
if match_type_parameter(cx, qpath, &paths::RC) {
379+
span_lint_and_help(
380+
cx,
381+
RC_RC,
382+
hir_ty.span,
383+
"usage of `Rc<Rc<T>>`",
384+
"Rc<Rc<T>> can be simplified to Rc<T>",
385+
);
386+
return; // don't recurse into the type
387+
}
388+
if match_type_parameter(cx, qpath, &paths::BOX) {
389+
span_lint_and_help(
390+
cx,
391+
RC_BOX,
392+
hir_ty.span,
393+
"usage of `Rc<Box<T>>`",
394+
"Rc<Box<T>> can be simplified to Rc<T>",
395+
);
396+
return; // don't recurse into the type
397+
}
325398
} else if cx.tcx.is_diagnostic_item(Symbol::intern("vec_type"), def_id) {
326399
if_chain! {
327400
// Get the _ part of Vec<_>

clippy_lints/src/utils/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub const BEGIN_PANIC: [&str; 3] = ["std", "panicking", "begin_panic"];
99
pub const BEGIN_PANIC_FMT: [&str; 3] = ["std", "panicking", "begin_panic_fmt"];
1010
pub const BINARY_HEAP: [&str; 4] = ["alloc", "collections", "binary_heap", "BinaryHeap"];
1111
pub const BORROW_TRAIT: [&str; 3] = ["core", "borrow", "Borrow"];
12+
pub const BOX: [&str; 3] = ["alloc", "boxed", "Box"];
1213
pub const BTREEMAP: [&str; 5] = ["alloc", "collections", "btree", "map", "BTreeMap"];
1314
pub const BTREEMAP_ENTRY: [&str; 5] = ["alloc", "collections", "btree", "map", "Entry"];
1415
pub const BTREESET: [&str; 5] = ["alloc", "collections", "btree", "set", "BTreeSet"];

src/lintlist/mod.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 362] = [
9+
pub const ALL_LINTS: [Lint; 364] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1722,6 +1722,20 @@ pub const ALL_LINTS: [Lint; 362] = [
17221722
deprecation: None,
17231723
module: "ranges",
17241724
},
1725+
Lint {
1726+
name: "rc_box",
1727+
group: "perf",
1728+
desc: "an Rc of Box",
1729+
deprecation: None,
1730+
module: "types",
1731+
},
1732+
Lint {
1733+
name: "rc_rc",
1734+
group: "perf",
1735+
desc: "an Rc of Rc",
1736+
deprecation: None,
1737+
module: "types",
1738+
},
17251739
Lint {
17261740
name: "redundant_clone",
17271741
group: "perf",

tests/ui/rc_rc.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::boxed::Box;
2+
use std::rc::Rc;
3+
4+
pub fn test(a: Rc<Rc<bool>>) {}
5+
6+
pub fn test2(a: Rc<Box<bool>>) {}
7+
8+
fn main() {}

tests/ui/rc_rc.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: usage of `Rc<Rc<T>>`
2+
--> $DIR/rc_rc.rs:4:16
3+
|
4+
LL | pub fn test(a: Rc<Rc<bool>>) {}
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::rc-rc` implied by `-D warnings`
8+
= help: Rc<Rc<T>> can be simplified to Rc<T>
9+
10+
error: usage of `Rc<Box<T>>`
11+
--> $DIR/rc_rc.rs:6:17
12+
|
13+
LL | pub fn test2(a: Rc<Box<bool>>) {}
14+
| ^^^^^^^^^^^^^
15+
|
16+
= note: `-D clippy::rc-box` implied by `-D warnings`
17+
= help: Rc<Box<T>> can be simplified to Rc<T>
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)