Skip to content

Commit 29d917a

Browse files
committed
redundant_allocation Arc; But can't pass too-many-lines lint
Author: lyj <lyj@[email protected]>
1 parent e4a1e85 commit 29d917a

File tree

5 files changed

+146
-28
lines changed

5 files changed

+146
-28
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ declare_clippy_lint! {
177177
declare_clippy_lint! {
178178
/// **What it does:** Checks for use of redundant allocations anywhere in the code.
179179
///
180-
/// **Why is this bad?** Expressions such as `Rc<&T>`, `Rc<Rc<T>>`, `Rc<Box<T>>`, `Box<&T>`
181-
/// add an unnecessary level of indirection.
180+
/// **Why is this bad?** Expressions such as `Rc<&T>`, `Rc<Rc<T>>`, `Rc<Box<T>>`, Arc<&T>`, `Arc<Arc<T>>`,
181+
/// `Arc<Box<T>>`, `Box<&T>` add an unnecessary level of indirection.
182182
///
183183
/// **Known problems:** None.
184184
///

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
3737
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
3838
applicability,
3939
);
40-
true
40+
return true;
4141
} else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
4242
let qpath = match &ty.kind {
4343
TyKind::Path(qpath) => qpath,
@@ -60,23 +60,73 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
6060
),
6161
applicability,
6262
);
63+
return true;
64+
}
65+
return utils::match_borrows_parameter(cx, qpath).map_or(false, |span| {
66+
let mut applicability = Applicability::MachineApplicable;
67+
span_lint_and_sugg(
68+
cx,
69+
REDUNDANT_ALLOCATION,
70+
hir_ty.span,
71+
"usage of `Rc<&T>`",
72+
"try",
73+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
74+
applicability,
75+
);
6376
true
64-
} else {
65-
utils::match_borrows_parameter(cx, qpath).map_or(false, |span| {
66-
let mut applicability = Applicability::MachineApplicable;
67-
span_lint_and_sugg(
68-
cx,
69-
REDUNDANT_ALLOCATION,
70-
hir_ty.span,
71-
"usage of `Rc<&T>`",
72-
"try",
73-
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
74-
applicability,
75-
);
76-
true
77-
})
77+
});
78+
}
79+
80+
if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
81+
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Arc) {
82+
let mut applicability = Applicability::MachineApplicable;
83+
span_lint_and_sugg(
84+
cx,
85+
REDUNDANT_ALLOCATION,
86+
hir_ty.span,
87+
"usage of `Arc<Arc<T>>`",
88+
"try",
89+
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
90+
applicability,
91+
);
92+
return true;
93+
} else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
94+
let qpath = match &ty.kind {
95+
TyKind::Path(qpath) => qpath,
96+
_ => return false,
97+
};
98+
let inner_span = match get_qpath_generic_tys(qpath).next() {
99+
Some(ty) => ty.span,
100+
None => return false,
101+
};
102+
let mut applicability = Applicability::MachineApplicable;
103+
span_lint_and_sugg(
104+
cx,
105+
REDUNDANT_ALLOCATION,
106+
hir_ty.span,
107+
"usage of `Arc<Box<T>>`",
108+
"try",
109+
format!(
110+
"Arc<{}>",
111+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
112+
),
113+
applicability,
114+
);
115+
return true;
78116
}
79-
} else {
80-
false
117+
return utils::match_borrows_parameter(cx, qpath).map_or(false, |span| {
118+
let mut applicability = Applicability::MachineApplicable;
119+
span_lint_and_sugg(
120+
cx,
121+
REDUNDANT_ALLOCATION,
122+
hir_ty.span,
123+
"usage of `Arc<&T>`",
124+
"try",
125+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
126+
applicability,
127+
);
128+
true
129+
});
81130
}
131+
false
82132
}

tests/ui/redundant_allocation.fixed

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use std::boxed::Box;
77
use std::rc::Rc;
8+
use std::sync::Arc;
89

910
pub struct MyStruct {}
1011

@@ -45,4 +46,22 @@ pub fn test9(foo: &MyEnum) {}
4546

4647
pub fn test10_neg(foo: Box<SubT<&usize>>) {}
4748

49+
// Arc<&T>
50+
51+
pub fn test11<T>(foo: &T) {}
52+
53+
pub fn test12(foo: &MyStruct) {}
54+
55+
pub fn test13(foo: &MyEnum) {}
56+
57+
pub fn test14_neg(foo: Arc<SubT<&usize>>) {}
58+
59+
// Arc<Arc<T>>
60+
61+
pub fn test15(a: Arc<bool>) {}
62+
63+
// Arc<Box<T>>
64+
65+
pub fn test16(a: Arc<bool>) {}
66+
4867
fn main() {}

tests/ui/redundant_allocation.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use std::boxed::Box;
77
use std::rc::Rc;
8+
use std::sync::Arc;
89

910
pub struct MyStruct {}
1011

@@ -45,4 +46,22 @@ pub fn test9(foo: Box<&MyEnum>) {}
4546

4647
pub fn test10_neg(foo: Box<SubT<&usize>>) {}
4748

49+
// Arc<&T>
50+
51+
pub fn test11<T>(foo: Arc<&T>) {}
52+
53+
pub fn test12(foo: Arc<&MyStruct>) {}
54+
55+
pub fn test13(foo: Arc<&MyEnum>) {}
56+
57+
pub fn test14_neg(foo: Arc<SubT<&usize>>) {}
58+
59+
// Arc<Arc<T>>
60+
61+
pub fn test15(a: Arc<Arc<bool>>) {}
62+
63+
// Arc<Box<T>>
64+
65+
pub fn test16(a: Arc<Box<bool>>) {}
66+
4867
fn main() {}

tests/ui/redundant_allocation.stderr

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,82 @@
11
error: usage of `Rc<&T>`
2-
--> $DIR/redundant_allocation.rs:22:22
2+
--> $DIR/redundant_allocation.rs:23:22
33
|
44
LL | pub fn test1<T>(foo: Rc<&T>) {}
55
| ^^^^^^ help: try: `&T`
66
|
77
= note: `-D clippy::redundant-allocation` implied by `-D warnings`
88

99
error: usage of `Rc<&T>`
10-
--> $DIR/redundant_allocation.rs:24:19
10+
--> $DIR/redundant_allocation.rs:25:19
1111
|
1212
LL | pub fn test2(foo: Rc<&MyStruct>) {}
1313
| ^^^^^^^^^^^^^ help: try: `&MyStruct`
1414

1515
error: usage of `Rc<&T>`
16-
--> $DIR/redundant_allocation.rs:26:19
16+
--> $DIR/redundant_allocation.rs:27:19
1717
|
1818
LL | pub fn test3(foo: Rc<&MyEnum>) {}
1919
| ^^^^^^^^^^^ help: try: `&MyEnum`
2020

2121
error: usage of `Rc<Rc<T>>`
22-
--> $DIR/redundant_allocation.rs:32:17
22+
--> $DIR/redundant_allocation.rs:33:17
2323
|
2424
LL | pub fn test5(a: Rc<Rc<bool>>) {}
2525
| ^^^^^^^^^^^^ help: try: `Rc<bool>`
2626

2727
error: usage of `Rc<Box<T>>`
28-
--> $DIR/redundant_allocation.rs:36:17
28+
--> $DIR/redundant_allocation.rs:37:17
2929
|
3030
LL | pub fn test6(a: Rc<Box<bool>>) {}
3131
| ^^^^^^^^^^^^^ help: try: `Rc<bool>`
3232

3333
error: usage of `Box<&T>`
34-
--> $DIR/redundant_allocation.rs:40:22
34+
--> $DIR/redundant_allocation.rs:41:22
3535
|
3636
LL | pub fn test7<T>(foo: Box<&T>) {}
3737
| ^^^^^^^ help: try: `&T`
3838

3939
error: usage of `Box<&T>`
40-
--> $DIR/redundant_allocation.rs:42:19
40+
--> $DIR/redundant_allocation.rs:43:19
4141
|
4242
LL | pub fn test8(foo: Box<&MyStruct>) {}
4343
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
4444

4545
error: usage of `Box<&T>`
46-
--> $DIR/redundant_allocation.rs:44:19
46+
--> $DIR/redundant_allocation.rs:45:19
4747
|
4848
LL | pub fn test9(foo: Box<&MyEnum>) {}
4949
| ^^^^^^^^^^^^ help: try: `&MyEnum`
5050

51-
error: aborting due to 8 previous errors
51+
error: usage of `Arc<&T>`
52+
--> $DIR/redundant_allocation.rs:51:23
53+
|
54+
LL | pub fn test11<T>(foo: Arc<&T>) {}
55+
| ^^^^^^^ help: try: `&T`
56+
57+
error: usage of `Arc<&T>`
58+
--> $DIR/redundant_allocation.rs:53:20
59+
|
60+
LL | pub fn test12(foo: Arc<&MyStruct>) {}
61+
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`
62+
63+
error: usage of `Arc<&T>`
64+
--> $DIR/redundant_allocation.rs:55:20
65+
|
66+
LL | pub fn test13(foo: Arc<&MyEnum>) {}
67+
| ^^^^^^^^^^^^ help: try: `&MyEnum`
68+
69+
error: usage of `Arc<Arc<T>>`
70+
--> $DIR/redundant_allocation.rs:61:18
71+
|
72+
LL | pub fn test15(a: Arc<Arc<bool>>) {}
73+
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`
74+
75+
error: usage of `Arc<Box<T>>`
76+
--> $DIR/redundant_allocation.rs:65:18
77+
|
78+
LL | pub fn test16(a: Arc<Box<bool>>) {}
79+
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`
80+
81+
error: aborting due to 13 previous errors
5282

0 commit comments

Comments
 (0)