Add flags to detect lints are triggered

This commit is contained in:
Yoshitomo Nakanishi 2021-02-12 14:11:04 +09:00
parent 2c2fb3996f
commit fbd25e93a4
5 changed files with 38 additions and 16 deletions

View File

@ -6,7 +6,7 @@ use crate::utils::{is_ty_param_diagnostic_item, span_lint_and_help};
use super::BOX_VEC;
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
if Some(def_id) == cx.tcx.lang_items().owned_box() {
if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
span_lint_and_help(
@ -17,6 +17,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
None,
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation",
);
return true;
}
}
false
}

View File

@ -322,10 +322,11 @@ impl Types {
let hir_id = hir_ty.hir_id;
let res = cx.qpath_res(qpath, hir_id);
if let Some(def_id) = res.opt_def_id() {
box_vec::check(cx, hir_ty, qpath, def_id);
redundant_allocation::check(cx, hir_ty, qpath, def_id);
rc_buffer::check(cx, hir_ty, qpath, def_id);
vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
let mut triggered = false;
triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
if cx.tcx.is_diagnostic_item(sym::option_type, def_id) {
if is_ty_param_diagnostic_item(cx, qpath, sym::option_type).is_some() {
@ -349,6 +350,10 @@ impl Types {
);
return; // don't recurse into the type
}
if triggered {
return;
}
}
match *qpath {
QPath::Resolved(Some(ref ty), ref p) => {

View File

@ -9,7 +9,7 @@ use crate::utils::{
use super::RC_BUFFER;
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
if let Some(alternate) = match_buffer_type(cx, qpath) {
span_lint_and_sugg(
@ -24,11 +24,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
let qpath = match &ty.kind {
TyKind::Path(qpath) => qpath,
_ => return,
_ => return false,
};
let inner_span = match get_qpath_generic_tys(qpath).next() {
Some(ty) => ty.span,
None => return,
None => return false,
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
@ -43,6 +43,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
),
Applicability::MachineApplicable,
);
return true;
}
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
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<'_
} else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
let qpath = match &ty.kind {
TyKind::Path(qpath) => qpath,
_ => return,
_ => return false,
};
let inner_span = match get_qpath_generic_tys(qpath).next() {
Some(ty) => ty.span,
None => return,
None => return false,
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
@ -77,8 +78,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
),
Applicability::MachineApplicable,
);
return true;
}
}
false
}
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {

View File

@ -10,7 +10,7 @@ use crate::utils::{
use super::{utils, REDUNDANT_ALLOCATION};
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
if Some(def_id) == cx.tcx.lang_items().owned_box() {
if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
let mut applicability = Applicability::MachineApplicable;
@ -23,7 +23,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
applicability,
);
return;
return true;
}
}
@ -39,14 +39,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
applicability,
);
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,
_ => return false,
};
let inner_span = match get_qpath_generic_tys(qpath).next() {
Some(ty) => ty.span,
None => return,
None => return false,
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
@ -61,6 +62,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
),
applicability,
);
true
} else if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
@ -72,7 +74,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
applicability,
);
return; // don't recurse into the type
true
} else {
false
}
} else {
false
}
}

View File

@ -18,7 +18,7 @@ pub(super) fn check(
qpath: &QPath<'_>,
def_id: DefId,
box_size_threshold: u64,
) {
) -> bool {
if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
if_chain! {
// Get the _ part of Vec<_>
@ -53,7 +53,12 @@ pub(super) fn check(
format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
Applicability::MachineApplicable,
);
true
} else {
false
}
}
} else {
false
}
}