Skip to content

Commit f110c5e

Browse files
committed
Move rc_buffer to its own module
1 parent 128f1f5 commit f110c5e

File tree

2 files changed

+102
-92
lines changed

2 files changed

+102
-92
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(rustc::default_hash_types)]
22

33
mod box_vec;
4+
mod rc_buffer;
45
mod redundant_allocation;
56
mod utils;
67

@@ -36,11 +37,11 @@ use crate::consts::{constant, Constant};
3637
use crate::utils::paths;
3738
use crate::utils::sugg::Sugg;
3839
use crate::utils::{
39-
clip, comparisons, differing_macro_contexts, get_qpath_generic_tys, higher, in_constant, indent_of, int_bits,
40-
is_hir_ty_cfg_dependant, is_ty_param_diagnostic_item, is_type_diagnostic_item, last_path_segment, match_def_path,
41-
match_path, meets_msrv, method_chain_args, multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline,
42-
sext, snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help,
43-
span_lint_and_sugg, span_lint_and_then, unsext,
40+
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_hir_ty_cfg_dependant,
41+
is_ty_param_diagnostic_item, is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv,
42+
method_chain_args, multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt,
43+
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
44+
span_lint_and_then, unsext,
4445
};
4546

4647
declare_clippy_lint! {
@@ -291,18 +292,6 @@ impl<'tcx> LateLintPass<'tcx> for Types {
291292
}
292293
}
293294

294-
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
295-
if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
296-
Some("str")
297-
} else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
298-
Some("std::ffi::OsStr")
299-
} else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
300-
Some("std::path::Path")
301-
} else {
302-
None
303-
}
304-
}
305-
306295
impl Types {
307296
pub fn new(vec_box_size_threshold: u64) -> Self {
308297
Self { vec_box_size_threshold }
@@ -335,81 +324,8 @@ impl Types {
335324
if let Some(def_id) = res.opt_def_id() {
336325
box_vec::check(cx, hir_ty, qpath, def_id);
337326
redundant_allocation::check(cx, hir_ty, qpath, def_id);
338-
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
339-
if let Some(alternate) = match_buffer_type(cx, qpath) {
340-
span_lint_and_sugg(
341-
cx,
342-
RC_BUFFER,
343-
hir_ty.span,
344-
"usage of `Rc<T>` when T is a buffer type",
345-
"try",
346-
format!("Rc<{}>", alternate),
347-
Applicability::MachineApplicable,
348-
);
349-
return; // don't recurse into the type
350-
}
351-
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
352-
let qpath = match &ty.kind {
353-
TyKind::Path(qpath) => qpath,
354-
_ => return,
355-
};
356-
let inner_span = match get_qpath_generic_tys(qpath).next() {
357-
Some(ty) => ty.span,
358-
None => return,
359-
};
360-
let mut applicability = Applicability::MachineApplicable;
361-
span_lint_and_sugg(
362-
cx,
363-
RC_BUFFER,
364-
hir_ty.span,
365-
"usage of `Rc<T>` when T is a buffer type",
366-
"try",
367-
format!(
368-
"Rc<[{}]>",
369-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
370-
),
371-
Applicability::MachineApplicable,
372-
);
373-
return; // don't recurse into the type
374-
}
375-
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
376-
if let Some(alternate) = match_buffer_type(cx, qpath) {
377-
span_lint_and_sugg(
378-
cx,
379-
RC_BUFFER,
380-
hir_ty.span,
381-
"usage of `Arc<T>` when T is a buffer type",
382-
"try",
383-
format!("Arc<{}>", alternate),
384-
Applicability::MachineApplicable,
385-
);
386-
return; // don't recurse into the type
387-
}
388-
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
389-
let qpath = match &ty.kind {
390-
TyKind::Path(qpath) => qpath,
391-
_ => return,
392-
};
393-
let inner_span = match get_qpath_generic_tys(qpath).next() {
394-
Some(ty) => ty.span,
395-
None => return,
396-
};
397-
let mut applicability = Applicability::MachineApplicable;
398-
span_lint_and_sugg(
399-
cx,
400-
RC_BUFFER,
401-
hir_ty.span,
402-
"usage of `Arc<T>` when T is a buffer type",
403-
"try",
404-
format!(
405-
"Arc<[{}]>",
406-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
407-
),
408-
Applicability::MachineApplicable,
409-
);
410-
return; // don't recurse into the type
411-
}
412-
} else if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
327+
rc_buffer::check(cx, hir_ty, qpath, def_id);
328+
if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
413329
if_chain! {
414330
// Get the _ part of Vec<_>
415331
if let Some(ref last) = last_path_segment(qpath).args;

clippy_lints/src/types/rc_buffer.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use rustc_errors::Applicability;
2+
use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
3+
use rustc_lint::LateContext;
4+
use rustc_span::symbol::sym;
5+
6+
use crate::utils::{
7+
get_qpath_generic_tys, is_ty_param_diagnostic_item, snippet_with_applicability, span_lint_and_sugg,
8+
};
9+
10+
use super::RC_BUFFER;
11+
12+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
13+
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
14+
if let Some(alternate) = match_buffer_type(cx, qpath) {
15+
span_lint_and_sugg(
16+
cx,
17+
RC_BUFFER,
18+
hir_ty.span,
19+
"usage of `Rc<T>` when T is a buffer type",
20+
"try",
21+
format!("Rc<{}>", alternate),
22+
Applicability::MachineApplicable,
23+
);
24+
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
25+
let qpath = match &ty.kind {
26+
TyKind::Path(qpath) => qpath,
27+
_ => return,
28+
};
29+
let inner_span = match get_qpath_generic_tys(qpath).next() {
30+
Some(ty) => ty.span,
31+
None => return,
32+
};
33+
let mut applicability = Applicability::MachineApplicable;
34+
span_lint_and_sugg(
35+
cx,
36+
RC_BUFFER,
37+
hir_ty.span,
38+
"usage of `Rc<T>` when T is a buffer type",
39+
"try",
40+
format!(
41+
"Rc<[{}]>",
42+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
43+
),
44+
Applicability::MachineApplicable,
45+
);
46+
}
47+
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
48+
if let Some(alternate) = match_buffer_type(cx, qpath) {
49+
span_lint_and_sugg(
50+
cx,
51+
RC_BUFFER,
52+
hir_ty.span,
53+
"usage of `Arc<T>` when T is a buffer type",
54+
"try",
55+
format!("Arc<{}>", alternate),
56+
Applicability::MachineApplicable,
57+
);
58+
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
59+
let qpath = match &ty.kind {
60+
TyKind::Path(qpath) => qpath,
61+
_ => return,
62+
};
63+
let inner_span = match get_qpath_generic_tys(qpath).next() {
64+
Some(ty) => ty.span,
65+
None => return,
66+
};
67+
let mut applicability = Applicability::MachineApplicable;
68+
span_lint_and_sugg(
69+
cx,
70+
RC_BUFFER,
71+
hir_ty.span,
72+
"usage of `Arc<T>` when T is a buffer type",
73+
"try",
74+
format!(
75+
"Arc<[{}]>",
76+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
77+
),
78+
Applicability::MachineApplicable,
79+
);
80+
}
81+
}
82+
}
83+
84+
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
85+
if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
86+
Some("str")
87+
} else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
88+
Some("std::ffi::OsStr")
89+
} else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
90+
Some("std::path::Path")
91+
} else {
92+
None
93+
}
94+
}

0 commit comments

Comments
 (0)