Skip to content

Commit a746638

Browse files
committed
[significant_drop_tightening] Add MVP
1 parent fd2d8be commit a746638

8 files changed

+567
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,6 +4734,7 @@ Released 2018-09-13
47344734
[`should_assert_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_assert_eq
47354735
[`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
47364736
[`significant_drop_in_scrutinee`]: https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_in_scrutinee
4737+
[`significant_drop_tightening`]: https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening
47374738
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
47384739
[`single_char_add_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
47394740
[`single_char_lifetime_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_lifetime_names

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
536536
crate::shadow::SHADOW_REUSE_INFO,
537537
crate::shadow::SHADOW_SAME_INFO,
538538
crate::shadow::SHADOW_UNRELATED_INFO,
539+
crate::significant_drop_tightening::SIGNIFICANT_DROP_TIGHTENING_INFO,
539540
crate::single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES_INFO,
540541
crate::single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS_INFO,
541542
crate::size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT_INFO,

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(box_patterns)]
44
#![feature(control_flow_enum)]
55
#![feature(drain_filter)]
6+
#![feature(if_let_guard)]
67
#![feature(iter_intersperse)]
78
#![feature(let_chains)]
89
#![feature(lint_reasons)]
@@ -264,6 +265,8 @@ mod semicolon_block;
264265
mod semicolon_if_nothing_returned;
265266
mod serde_api;
266267
mod shadow;
268+
mod sig_drop_aux;
269+
mod significant_drop_tightening;
267270
mod single_char_lifetime_names;
268271
mod single_component_path_imports;
269272
mod size_of_in_element_count;
@@ -559,6 +562,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
559562
store.register_late_pass(|_| Box::new(eta_reduction::EtaReduction));
560563
store.register_late_pass(|_| Box::new(mut_mut::MutMut));
561564
store.register_late_pass(|_| Box::new(mut_reference::UnnecessaryMutPassed));
565+
store.register_late_pass(|_| Box::new(significant_drop_tightening::SignificantDropTightening::new()));
562566
store.register_late_pass(|_| Box::new(len_zero::LenZero));
563567
store.register_late_pass(|_| Box::new(attrs::Attributes));
564568
store.register_late_pass(|_| Box::new(blocks_in_if_conditions::BlocksInIfConditions));

clippy_lints/src/sig_drop_aux.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Auxiliary structures for lints involving `#[clippy::has_significant_drop]`.
2+
3+
use crate::FxHashSet;
4+
use clippy_utils::get_attr;
5+
use rustc_lint::{LateContext, LintContext};
6+
use rustc_middle::ty::{subst::GenericArgKind, Ty, TypeAndMut};
7+
8+
pub(crate) struct SigDropChecker<'cx, 'sdt, 'tcx> {
9+
cx: &'cx LateContext<'tcx>,
10+
seen_types: &'sdt mut FxHashSet<Ty<'tcx>>,
11+
}
12+
13+
impl<'cx, 'sdt, 'tcx> SigDropChecker<'cx, 'sdt, 'tcx> {
14+
pub(crate) fn new(cx: &'cx LateContext<'tcx>, seen_types: &'sdt mut FxHashSet<Ty<'tcx>>) -> Self {
15+
seen_types.clear();
16+
Self { cx, seen_types }
17+
}
18+
19+
pub(crate) fn has_sig_drop_attr(&mut self, ty: Ty<'tcx>) -> bool {
20+
if let Some(adt) = ty.ty_adt_def() {
21+
let iter = get_attr(
22+
self.cx.sess(),
23+
self.cx.tcx.get_attrs_unchecked(adt.did()),
24+
"has_significant_drop",
25+
);
26+
if iter.count() > 0 {
27+
return true;
28+
}
29+
}
30+
match ty.kind() {
31+
rustc_middle::ty::Adt(a, b) => {
32+
for f in a.all_fields() {
33+
let ty = f.ty(self.cx.tcx, b);
34+
if !self.has_seen_ty(ty) && self.has_sig_drop_attr(ty) {
35+
return true;
36+
}
37+
}
38+
for generic_arg in b.iter() {
39+
if let GenericArgKind::Type(ty) = generic_arg.unpack() {
40+
if self.has_sig_drop_attr(ty) {
41+
return true;
42+
}
43+
}
44+
}
45+
false
46+
},
47+
rustc_middle::ty::Array(ty, _)
48+
| rustc_middle::ty::RawPtr(TypeAndMut { ty, .. })
49+
| rustc_middle::ty::Ref(_, ty, _)
50+
| rustc_middle::ty::Slice(ty) => self.has_sig_drop_attr(*ty),
51+
_ => false,
52+
}
53+
}
54+
55+
fn has_seen_ty(&mut self, ty: Ty<'tcx>) -> bool {
56+
!self.seen_types.insert(ty)
57+
}
58+
}

0 commit comments

Comments
 (0)