Skip to content

Commit 2dd7175

Browse files
committed
Apply rc_buffer lint to Arc<T>
1 parent 1b5317f commit 2dd7175

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

clippy_lints/src/types.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,46 @@ impl Types {
480480
);
481481
return; // don't recurse into the type
482482
}
483+
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
484+
if let Some(alternate) = match_buffer_type(cx, qpath) {
485+
span_lint_and_sugg(
486+
cx,
487+
RC_BUFFER,
488+
hir_ty.span,
489+
"usage of `Arc<T>` when T is a buffer type",
490+
"try",
491+
format!("Arc<{}>", alternate),
492+
Applicability::MachineApplicable,
493+
);
494+
return; // don't recurse into the type
495+
}
496+
if match_type_parameter(cx, qpath, &paths::VEC).is_some() {
497+
let vec_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
498+
GenericArg::Type(ty) => match &ty.kind {
499+
TyKind::Path(qpath) => qpath,
500+
_ => return,
501+
},
502+
_ => return,
503+
};
504+
let inner_span = match &last_path_segment(&vec_ty).args.unwrap().args[0] {
505+
GenericArg::Type(ty) => ty.span,
506+
_ => return,
507+
};
508+
let mut applicability = Applicability::MachineApplicable;
509+
span_lint_and_sugg(
510+
cx,
511+
RC_BUFFER,
512+
hir_ty.span,
513+
"usage of `Arc<T>` when T is a buffer type",
514+
"try",
515+
format!(
516+
"Arc<[{}]>",
517+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
518+
),
519+
Applicability::MachineApplicable,
520+
);
521+
return; // don't recurse into the type
522+
}
483523
} else if cx.tcx.is_diagnostic_item(sym!(vec_type), def_id) {
484524
if_chain! {
485525
// Get the _ part of Vec<_>

tests/ui/rc_buffer_arc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::ffi::OsString;
2+
use std::path::PathBuf;
3+
use std::sync::Arc;
4+
5+
#[warn(clippy::rc_buffer)]
6+
struct S {
7+
a: Arc<String>,
8+
b: Arc<PathBuf>,
9+
c: Arc<Vec<u8>>,
10+
d: Arc<OsString>,
11+
}
12+
13+
fn main() {}

tests/ui/rc_buffer_arc.stderr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: usage of `Arc<T>` when T is a buffer type
2+
--> $DIR/rc_buffer_arc.rs:7:8
3+
|
4+
LL | a: Arc<String>,
5+
| ^^^^^^^^^^^ help: try: `Arc<str>`
6+
|
7+
= note: `-D clippy::rc-buffer` implied by `-D warnings`
8+
9+
error: usage of `Arc<T>` when T is a buffer type
10+
--> $DIR/rc_buffer_arc.rs:8:8
11+
|
12+
LL | b: Arc<PathBuf>,
13+
| ^^^^^^^^^^^^ help: try: `Arc<std::path::Path>`
14+
15+
error: usage of `Arc<T>` when T is a buffer type
16+
--> $DIR/rc_buffer_arc.rs:9:8
17+
|
18+
LL | c: Arc<Vec<u8>>,
19+
| ^^^^^^^^^^^^ help: try: `Arc<[u8]>`
20+
21+
error: usage of `Arc<T>` when T is a buffer type
22+
--> $DIR/rc_buffer_arc.rs:10:8
23+
|
24+
LL | d: Arc<OsString>,
25+
| ^^^^^^^^^^^^^ help: try: `Arc<std::ffi::OsStr>`
26+
27+
error: aborting due to 4 previous errors
28+

0 commit comments

Comments
 (0)