Skip to content

Commit 128f1f5

Browse files
committed
Move redundant_allocation to its own module
1 parent df307c0 commit 128f1f5

File tree

3 files changed

+112
-88
lines changed

3 files changed

+112
-88
lines changed

clippy_lints/src/types/mod.rs

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

33
mod box_vec;
4+
mod redundant_allocation;
5+
mod utils;
46

57
use std::borrow::Cow;
68
use std::cmp::Ordering;
@@ -13,8 +15,8 @@ use rustc_hir as hir;
1315
use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisitorMap, Visitor};
1416
use rustc_hir::{
1517
BinOpKind, Block, Body, Expr, ExprKind, FnDecl, FnRetTy, FnSig, GenericArg, GenericBounds, GenericParamKind, HirId,
16-
ImplItem, ImplItemKind, Item, ItemKind, LangItem, Lifetime, Lit, Local, MatchSource, MutTy, Mutability, Node,
17-
QPath, Stmt, StmtKind, SyntheticTyParamKind, TraitFn, TraitItem, TraitItemKind, TyKind, UnOp,
18+
ImplItem, ImplItemKind, Item, ItemKind, Lifetime, Lit, Local, MatchSource, MutTy, Mutability, Node, QPath, Stmt,
19+
StmtKind, SyntheticTyParamKind, TraitFn, TraitItem, TraitItemKind, TyKind, UnOp,
1820
};
1921
use rustc_lint::{LateContext, LateLintPass, LintContext};
2022
use rustc_middle::hir::map::Map;
@@ -35,10 +37,10 @@ use crate::utils::paths;
3537
use crate::utils::sugg::Sugg;
3638
use crate::utils::{
3739
clip, comparisons, differing_macro_contexts, get_qpath_generic_tys, higher, in_constant, indent_of, int_bits,
38-
is_hir_ty_cfg_dependant, is_ty_param_diagnostic_item, is_ty_param_lang_item, is_type_diagnostic_item,
39-
last_path_segment, match_def_path, match_path, meets_msrv, method_chain_args, multispan_sugg,
40-
numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt, snippet_with_applicability,
41-
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
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,
4244
};
4345

4446
declare_clippy_lint! {
@@ -301,23 +303,6 @@ fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static
301303
}
302304
}
303305

304-
fn match_borrows_parameter(_cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<Span> {
305-
let last = last_path_segment(qpath);
306-
if_chain! {
307-
if let Some(ref params) = last.args;
308-
if !params.parenthesized;
309-
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
310-
GenericArg::Type(ty) => Some(ty),
311-
_ => None,
312-
});
313-
if let TyKind::Rptr(..) = ty.kind;
314-
then {
315-
return Some(ty.span);
316-
}
317-
}
318-
None
319-
}
320-
321306
impl Types {
322307
pub fn new(vec_box_size_threshold: u64) -> Self {
323308
Self { vec_box_size_threshold }
@@ -349,58 +334,8 @@ impl Types {
349334
let res = cx.qpath_res(qpath, hir_id);
350335
if let Some(def_id) = res.opt_def_id() {
351336
box_vec::check(cx, hir_ty, qpath, def_id);
352-
if Some(def_id) == cx.tcx.lang_items().owned_box() {
353-
if let Some(span) = match_borrows_parameter(cx, qpath) {
354-
let mut applicability = Applicability::MachineApplicable;
355-
span_lint_and_sugg(
356-
cx,
357-
REDUNDANT_ALLOCATION,
358-
hir_ty.span,
359-
"usage of `Box<&T>`",
360-
"try",
361-
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
362-
applicability,
363-
);
364-
return; // don't recurse into the type
365-
}
366-
} else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
367-
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Rc) {
368-
let mut applicability = Applicability::MachineApplicable;
369-
span_lint_and_sugg(
370-
cx,
371-
REDUNDANT_ALLOCATION,
372-
hir_ty.span,
373-
"usage of `Rc<Rc<T>>`",
374-
"try",
375-
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
376-
applicability,
377-
);
378-
return; // don't recurse into the type
379-
}
380-
if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
381-
let qpath = match &ty.kind {
382-
TyKind::Path(qpath) => qpath,
383-
_ => return,
384-
};
385-
let inner_span = match get_qpath_generic_tys(qpath).next() {
386-
Some(ty) => ty.span,
387-
None => return,
388-
};
389-
let mut applicability = Applicability::MachineApplicable;
390-
span_lint_and_sugg(
391-
cx,
392-
REDUNDANT_ALLOCATION,
393-
hir_ty.span,
394-
"usage of `Rc<Box<T>>`",
395-
"try",
396-
format!(
397-
"Rc<{}>",
398-
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
399-
),
400-
applicability,
401-
);
402-
return; // don't recurse into the type
403-
}
337+
redundant_allocation::check(cx, hir_ty, qpath, def_id);
338+
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
404339
if let Some(alternate) = match_buffer_type(cx, qpath) {
405340
span_lint_and_sugg(
406341
cx,
@@ -437,19 +372,6 @@ impl Types {
437372
);
438373
return; // don't recurse into the type
439374
}
440-
if let Some(span) = match_borrows_parameter(cx, qpath) {
441-
let mut applicability = Applicability::MachineApplicable;
442-
span_lint_and_sugg(
443-
cx,
444-
REDUNDANT_ALLOCATION,
445-
hir_ty.span,
446-
"usage of `Rc<&T>`",
447-
"try",
448-
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
449-
applicability,
450-
);
451-
return; // don't recurse into the type
452-
}
453375
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
454376
if let Some(alternate) = match_buffer_type(cx, qpath) {
455377
span_lint_and_sugg(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use rustc_errors::Applicability;
2+
use rustc_hir::{self as hir, def_id::DefId, LangItem, 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, is_ty_param_lang_item, snippet_with_applicability,
8+
span_lint_and_sugg,
9+
};
10+
11+
use super::{utils, REDUNDANT_ALLOCATION};
12+
13+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
14+
if Some(def_id) == cx.tcx.lang_items().owned_box() {
15+
if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
16+
let mut applicability = Applicability::MachineApplicable;
17+
span_lint_and_sugg(
18+
cx,
19+
REDUNDANT_ALLOCATION,
20+
hir_ty.span,
21+
"usage of `Box<&T>`",
22+
"try",
23+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
24+
applicability,
25+
);
26+
return;
27+
}
28+
}
29+
30+
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
31+
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Rc) {
32+
let mut applicability = Applicability::MachineApplicable;
33+
span_lint_and_sugg(
34+
cx,
35+
REDUNDANT_ALLOCATION,
36+
hir_ty.span,
37+
"usage of `Rc<Rc<T>>`",
38+
"try",
39+
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
40+
applicability,
41+
);
42+
} else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
43+
let qpath = match &ty.kind {
44+
TyKind::Path(qpath) => qpath,
45+
_ => return,
46+
};
47+
let inner_span = match get_qpath_generic_tys(qpath).next() {
48+
Some(ty) => ty.span,
49+
None => return,
50+
};
51+
let mut applicability = Applicability::MachineApplicable;
52+
span_lint_and_sugg(
53+
cx,
54+
REDUNDANT_ALLOCATION,
55+
hir_ty.span,
56+
"usage of `Rc<Box<T>>`",
57+
"try",
58+
format!(
59+
"Rc<{}>",
60+
snippet_with_applicability(cx, inner_span, "..", &mut applicability)
61+
),
62+
applicability,
63+
);
64+
} else if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
65+
let mut applicability = Applicability::MachineApplicable;
66+
span_lint_and_sugg(
67+
cx,
68+
REDUNDANT_ALLOCATION,
69+
hir_ty.span,
70+
"usage of `Rc<&T>`",
71+
"try",
72+
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
73+
applicability,
74+
);
75+
return; // don't recurse into the type
76+
}
77+
}
78+
}

clippy_lints/src/types/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use rustc_hir::{GenericArg, QPath, TyKind};
2+
use rustc_lint::LateContext;
3+
use rustc_span::source_map::Span;
4+
5+
use crate::utils::last_path_segment;
6+
7+
use if_chain::if_chain;
8+
9+
pub(super) fn match_borrows_parameter(_cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<Span> {
10+
let last = last_path_segment(qpath);
11+
if_chain! {
12+
if let Some(ref params) = last.args;
13+
if !params.parenthesized;
14+
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
15+
GenericArg::Type(ty) => Some(ty),
16+
_ => None,
17+
});
18+
if let TyKind::Rptr(..) = ty.kind;
19+
then {
20+
return Some(ty.span);
21+
}
22+
}
23+
None
24+
}

0 commit comments

Comments
 (0)