Skip to content

Commit 86fbb6d

Browse files
committed
[early_drop_lock] Add initial skeleton
1 parent 61ff54e commit 86fbb6d

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,7 @@ Released 2018-09-13
41584158
[`duplicate_mod`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicate_mod
41594159
[`duplicate_underscore_argument`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicate_underscore_argument
41604160
[`duration_subsec`]: https://rust-lang.github.io/rust-clippy/master/index.html#duration_subsec
4161+
[`early_drop_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#early_drop_lock
41614162
[`else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else
41624163
[`empty_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_drop
41634164
[`empty_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
137137
crate::drop_forget_ref::FORGET_REF_INFO,
138138
crate::drop_forget_ref::UNDROPPED_MANUALLY_DROPS_INFO,
139139
crate::duplicate_mod::DUPLICATE_MOD_INFO,
140+
crate::early_drop_lock::EARLY_DROP_LOCK_INFO,
140141
crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO,
141142
crate::empty_drop::EMPTY_DROP_INFO,
142143
crate::empty_enum::EMPTY_ENUM_INFO,

clippy_lints/src/early_drop_lock.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use rustc_lint::LateLintPass;
2+
use rustc_session::{declare_tool_lint, impl_lint_pass};
3+
4+
declare_clippy_lint! {
5+
/// ### What it does
6+
///
7+
/// Searches for lock primitives that could be early dropped but are in fact dropped at the end
8+
/// of their scopes.
9+
///
10+
/// ### Why is this bad?
11+
///
12+
/// A lock unnecessarily held can imply unnecessary resource contention.
13+
///
14+
/// ### Example
15+
///
16+
/// ```rust
17+
/// fn example() {
18+
/// let locked = some_sync_resource.lock();
19+
/// let owned_rslt = locked.do_stuff_with_resource();
20+
/// // Only `owned_rslt` is needed but `locked` is still held
21+
/// do_heavy_computation_that_takes_time(owned_rslt);
22+
/// }
23+
/// ```
24+
///
25+
/// Use instead:
26+
///
27+
/// ```rust
28+
/// fn example() {
29+
/// let owned_rslt = some_sync_resource.lock().do_stuff_with_resource();
30+
/// do_heavy_computation_that_takes_time(owned_rslt);
31+
/// }
32+
/// ```
33+
#[clippy::version = "1.67.0"]
34+
pub EARLY_DROP_LOCK,
35+
nursery,
36+
"Searches for lock primitives that could be early dropped in order to prevent unnecessary resource contention."
37+
}
38+
39+
impl_lint_pass!(EarlyDropLock => [EARLY_DROP_LOCK]);
40+
41+
pub struct EarlyDropLock;
42+
43+
impl<'tcx> LateLintPass<'tcx> for EarlyDropLock {}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ mod doc;
108108
mod double_parens;
109109
mod drop_forget_ref;
110110
mod duplicate_mod;
111+
mod early_drop_lock;
111112
mod else_if_without_else;
112113
mod empty_drop;
113114
mod empty_enum;
@@ -557,6 +558,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
557558
store.register_late_pass(|_| Box::new(eta_reduction::EtaReduction));
558559
store.register_late_pass(|_| Box::new(mut_mut::MutMut));
559560
store.register_late_pass(|_| Box::new(mut_reference::UnnecessaryMutPassed));
561+
store.register_late_pass(|_| Box::new(early_drop_lock::EarlyDropLock));
560562
store.register_late_pass(|_| Box::new(len_zero::LenZero));
561563
store.register_late_pass(|_| Box::new(attrs::Attributes));
562564
store.register_late_pass(|_| Box::new(blocks_in_if_conditions::BlocksInIfConditions));

0 commit comments

Comments
 (0)