Skip to content

Commit

Permalink
redundant_allocation Arc; But a function is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
lyj committed Jun 2, 2021
1 parent cf5f894 commit 0bacc4c
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 26 deletions.
84 changes: 67 additions & 17 deletions clippy_lints/src/types/redundant_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
applicability,
);
true
return true;
} else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
let qpath = match &ty.kind {
TyKind::Path(qpath) => qpath,
Expand All @@ -60,23 +60,73 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
),
applicability,
);
return true;
}
return utils::match_borrows_parameter(cx, qpath).map_or(false, |span| {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
REDUNDANT_ALLOCATION,
hir_ty.span,
"usage of `Rc<&T>`",
"try",
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
applicability,
);
true
} else {
utils::match_borrows_parameter(cx, qpath).map_or(false, |span| {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
REDUNDANT_ALLOCATION,
hir_ty.span,
"usage of `Rc<&T>`",
"try",
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
applicability,
);
true
})
});
}

if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Arc) {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
REDUNDANT_ALLOCATION,
hir_ty.span,
"usage of `Arc<Arc<T>>`",
"try",
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
applicability,
);
return true;
} else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
let qpath = match &ty.kind {
TyKind::Path(qpath) => qpath,
_ => return false,
};
let inner_span = match get_qpath_generic_tys(qpath).next() {
Some(ty) => ty.span,
None => return false,
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
REDUNDANT_ALLOCATION,
hir_ty.span,
"usage of `Arc<Box<T>>`",
"try",
format!(
"Arc<{}>",
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
),
applicability,
);
return true;
}
} else {
false
return utils::match_borrows_parameter(cx, qpath).map_or(false, |span| {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
REDUNDANT_ALLOCATION,
hir_ty.span,
"usage of `Arc<&T>`",
"try",
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
applicability,
);
true
});
}
false
}
19 changes: 19 additions & 0 deletions tests/ui/redundant_allocation.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::boxed::Box;
use std::rc::Rc;
use std::sync::Arc;

pub struct MyStruct {}

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

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

// Arc<&T>

pub fn test11<T>(foo: &T) {}

pub fn test12(foo: &MyStruct) {}

pub fn test13(foo: &MyEnum) {}

pub fn test14_neg(foo: Arc<SubT<&usize>>) {}

// Arc<Arc<T>>

pub fn test15(a: Arc<bool>) {}

// Arc<Box<T>>

pub fn test16(a: Arc<bool>) {}

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/redundant_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::boxed::Box;
use std::rc::Rc;
use std::sync::Arc;

pub struct MyStruct {}

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

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

// Arc<&T>

pub fn test11<T>(foo: Arc<&T>) {}

pub fn test12(foo: Arc<&MyStruct>) {}

pub fn test13(foo: Arc<&MyEnum>) {}

pub fn test14_neg(foo: Arc<SubT<&usize>>) {}

// Arc<Arc<T>>

pub fn test15(a: Arc<Arc<bool>>) {}

// Arc<Box<T>>

pub fn test16(a: Arc<Box<bool>>) {}

fn main() {}
48 changes: 39 additions & 9 deletions tests/ui/redundant_allocation.stderr
Original file line number Diff line number Diff line change
@@ -1,52 +1,82 @@
error: usage of `Rc<&T>`
--> $DIR/redundant_allocation.rs:22:22
--> $DIR/redundant_allocation.rs:23:22
|
LL | pub fn test1<T>(foo: Rc<&T>) {}
| ^^^^^^ help: try: `&T`
|
= note: `-D clippy::redundant-allocation` implied by `-D warnings`

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

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

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

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

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

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

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

error: aborting due to 8 previous errors
error: usage of `Arc<&T>`
--> $DIR/redundant_allocation.rs:51:23
|
LL | pub fn test11<T>(foo: Arc<&T>) {}
| ^^^^^^^ help: try: `&T`

error: usage of `Arc<&T>`
--> $DIR/redundant_allocation.rs:53:20
|
LL | pub fn test12(foo: Arc<&MyStruct>) {}
| ^^^^^^^^^^^^^^ help: try: `&MyStruct`

error: usage of `Arc<&T>`
--> $DIR/redundant_allocation.rs:55:20
|
LL | pub fn test13(foo: Arc<&MyEnum>) {}
| ^^^^^^^^^^^^ help: try: `&MyEnum`

error: usage of `Arc<Arc<T>>`
--> $DIR/redundant_allocation.rs:61:18
|
LL | pub fn test15(a: Arc<Arc<bool>>) {}
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`

error: usage of `Arc<Box<T>>`
--> $DIR/redundant_allocation.rs:65:18
|
LL | pub fn test16(a: Arc<Box<bool>>) {}
| ^^^^^^^^^^^^^^ help: try: `Arc<bool>`

error: aborting due to 13 previous errors

0 comments on commit 0bacc4c

Please sign in to comment.