Skip to content

Commit fbd25e9

Browse files
committed
Add flags to detect lints are triggered
1 parent 2c2fb39 commit fbd25e9

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

clippy_lints/src/types/box_vec.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::utils::{is_ty_param_diagnostic_item, span_lint_and_help};
66

77
use super::BOX_VEC;
88

9-
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
9+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
1010
if Some(def_id) == cx.tcx.lang_items().owned_box() {
1111
if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
1212
span_lint_and_help(
@@ -17,6 +17,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
1717
None,
1818
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation",
1919
);
20+
return true;
2021
}
2122
}
23+
false
2224
}

clippy_lints/src/types/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,11 @@ impl Types {
322322
let hir_id = hir_ty.hir_id;
323323
let res = cx.qpath_res(qpath, hir_id);
324324
if let Some(def_id) = res.opt_def_id() {
325-
box_vec::check(cx, hir_ty, qpath, def_id);
326-
redundant_allocation::check(cx, hir_ty, qpath, def_id);
327-
rc_buffer::check(cx, hir_ty, qpath, def_id);
328-
vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
325+
let mut triggered = false;
326+
triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
327+
triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
328+
triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
329+
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
329330

330331
if cx.tcx.is_diagnostic_item(sym::option_type, def_id) {
331332
if is_ty_param_diagnostic_item(cx, qpath, sym::option_type).is_some() {
@@ -349,6 +350,10 @@ impl Types {
349350
);
350351
return; // don't recurse into the type
351352
}
353+
354+
if triggered {
355+
return;
356+
}
352357
}
353358
match *qpath {
354359
QPath::Resolved(Some(ref ty), ref p) => {

clippy_lints/src/types/rc_buffer.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::utils::{
99

1010
use super::RC_BUFFER;
1111

12-
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
12+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
1313
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
1414
if let Some(alternate) = match_buffer_type(cx, qpath) {
1515
span_lint_and_sugg(
@@ -24,11 +24,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
2424
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
2525
let qpath = match &ty.kind {
2626
TyKind::Path(qpath) => qpath,
27-
_ => return,
27+
_ => return false,
2828
};
2929
let inner_span = match get_qpath_generic_tys(qpath).next() {
3030
Some(ty) => ty.span,
31-
None => return,
31+
None => return false,
3232
};
3333
let mut applicability = Applicability::MachineApplicable;
3434
span_lint_and_sugg(
@@ -43,6 +43,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
4343
),
4444
Applicability::MachineApplicable,
4545
);
46+
return true;
4647
}
4748
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
4849
if let Some(alternate) = match_buffer_type(cx, qpath) {
@@ -58,11 +59,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
5859
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
5960
let qpath = match &ty.kind {
6061
TyKind::Path(qpath) => qpath,
61-
_ => return,
62+
_ => return false,
6263
};
6364
let inner_span = match get_qpath_generic_tys(qpath).next() {
6465
Some(ty) => ty.span,
65-
None => return,
66+
None => return false,
6667
};
6768
let mut applicability = Applicability::MachineApplicable;
6869
span_lint_and_sugg(
@@ -77,8 +78,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
7778
),
7879
Applicability::MachineApplicable,
7980
);
81+
return true;
8082
}
8183
}
84+
85+
false
8286
}
8387

8488
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::utils::{
1010

1111
use super::{utils, REDUNDANT_ALLOCATION};
1212

13-
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
13+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
1414
if Some(def_id) == cx.tcx.lang_items().owned_box() {
1515
if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
1616
let mut applicability = Applicability::MachineApplicable;
@@ -23,7 +23,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
2323
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
2424
applicability,
2525
);
26-
return;
26+
return true;
2727
}
2828
}
2929

@@ -39,14 +39,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
3939
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
4040
applicability,
4141
);
42+
true
4243
} else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
4344
let qpath = match &ty.kind {
4445
TyKind::Path(qpath) => qpath,
45-
_ => return,
46+
_ => return false,
4647
};
4748
let inner_span = match get_qpath_generic_tys(qpath).next() {
4849
Some(ty) => ty.span,
49-
None => return,
50+
None => return false,
5051
};
5152
let mut applicability = Applicability::MachineApplicable;
5253
span_lint_and_sugg(
@@ -61,6 +62,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
6162
),
6263
applicability,
6364
);
65+
true
6466
} else if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
6567
let mut applicability = Applicability::MachineApplicable;
6668
span_lint_and_sugg(
@@ -72,7 +74,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
7274
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
7375
applicability,
7476
);
75-
return; // don't recurse into the type
77+
true
78+
} else {
79+
false
7680
}
81+
} else {
82+
false
7783
}
7884
}

clippy_lints/src/types/vec_box.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) fn check(
1818
qpath: &QPath<'_>,
1919
def_id: DefId,
2020
box_size_threshold: u64,
21-
) {
21+
) -> bool {
2222
if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
2323
if_chain! {
2424
// Get the _ part of Vec<_>
@@ -53,7 +53,12 @@ pub(super) fn check(
5353
format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
5454
Applicability::MachineApplicable,
5555
);
56+
true
57+
} else {
58+
false
5659
}
5760
}
61+
} else {
62+
false
5863
}
5964
}

0 commit comments

Comments
 (0)