2022-10-07 06:14:56 -04:00
|
|
|
use crate::{
|
|
|
|
lints::{NonBindingLet, NonBindingLetSub},
|
|
|
|
LateContext, LateLintPass, LintContext,
|
|
|
|
};
|
|
|
|
use rustc_errors::MultiSpan;
|
2022-05-29 14:35:00 -04:00
|
|
|
use rustc_hir as hir;
|
2022-08-04 17:31:08 -04:00
|
|
|
use rustc_middle::ty;
|
2022-05-31 14:05:04 -04:00
|
|
|
use rustc_span::Symbol;
|
2022-05-29 14:35:00 -04:00
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
/// The `let_underscore_drop` lint checks for statements which don't bind
|
|
|
|
/// an expression which has a non-trivial Drop implementation to anything,
|
|
|
|
/// causing the expression to be dropped immediately instead of at end of
|
|
|
|
/// scope.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2022-11-14 22:55:50 +09:00
|
|
|
///
|
2022-10-25 00:59:32 +02:00
|
|
|
/// ```rust
|
2022-05-29 14:35:00 -04:00
|
|
|
/// struct SomeStruct;
|
|
|
|
/// impl Drop for SomeStruct {
|
|
|
|
/// fn drop(&mut self) {
|
|
|
|
/// println!("Dropping SomeStruct");
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2022-06-05 12:47:19 -04:00
|
|
|
/// #[warn(let_underscore_drop)]
|
2023-04-10 22:02:52 +02:00
|
|
|
/// // SomeStruct is dropped immediately instead of at end of scope,
|
2022-05-29 14:35:00 -04:00
|
|
|
/// // so "Dropping SomeStruct" is printed before "end of main".
|
|
|
|
/// // The order of prints would be reversed if SomeStruct was bound to
|
|
|
|
/// // a name (such as "_foo").
|
|
|
|
/// let _ = SomeStruct;
|
|
|
|
/// println!("end of main");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-06-05 12:47:19 -04:00
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
2022-05-29 14:35:00 -04:00
|
|
|
/// ### Explanation
|
|
|
|
///
|
|
|
|
/// Statements which assign an expression to an underscore causes the
|
|
|
|
/// expression to immediately drop instead of extending the expression's
|
|
|
|
/// lifetime to the end of the scope. This is usually unintended,
|
|
|
|
/// especially for types like `MutexGuard`, which are typically used to
|
|
|
|
/// lock a mutex for the duration of an entire scope.
|
|
|
|
///
|
|
|
|
/// If you want to extend the expression's lifetime to the end of the scope,
|
|
|
|
/// assign an underscore-prefixed name (such as `_foo`) to the expression.
|
|
|
|
/// If you do actually want to drop the expression immediately, then
|
|
|
|
/// calling `std::mem::drop` on the expression is clearer and helps convey
|
|
|
|
/// intent.
|
|
|
|
pub LET_UNDERSCORE_DROP,
|
2022-06-16 21:07:00 -04:00
|
|
|
Allow,
|
2022-05-29 14:35:00 -04:00
|
|
|
"non-binding let on a type that implements `Drop`"
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:05:04 -04:00
|
|
|
declare_lint! {
|
|
|
|
/// The `let_underscore_lock` lint checks for statements which don't bind
|
|
|
|
/// a mutex to anything, causing the lock to be released immediately instead
|
|
|
|
/// of at end of scope, which is typically incorrect.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2022-10-24 18:00:41 +02:00
|
|
|
/// ```rust,compile_fail
|
2022-05-31 14:05:04 -04:00
|
|
|
/// use std::sync::{Arc, Mutex};
|
|
|
|
/// use std::thread;
|
|
|
|
/// let data = Arc::new(Mutex::new(0));
|
|
|
|
///
|
|
|
|
/// thread::spawn(move || {
|
|
|
|
/// // The lock is immediately released instead of at the end of the
|
|
|
|
/// // scope, which is probably not intended.
|
|
|
|
/// let _ = data.lock().unwrap();
|
|
|
|
/// println!("doing some work");
|
|
|
|
/// let mut lock = data.lock().unwrap();
|
|
|
|
/// *lock += 1;
|
|
|
|
/// });
|
|
|
|
/// ```
|
2022-06-05 12:47:19 -04:00
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
2022-05-31 14:05:04 -04:00
|
|
|
/// ### Explanation
|
|
|
|
///
|
|
|
|
/// Statements which assign an expression to an underscore causes the
|
|
|
|
/// expression to immediately drop instead of extending the expression's
|
|
|
|
/// lifetime to the end of the scope. This is usually unintended,
|
|
|
|
/// especially for types like `MutexGuard`, which are typically used to
|
|
|
|
/// lock a mutex for the duration of an entire scope.
|
|
|
|
///
|
|
|
|
/// If you want to extend the expression's lifetime to the end of the scope,
|
|
|
|
/// assign an underscore-prefixed name (such as `_foo`) to the expression.
|
|
|
|
/// If you do actually want to drop the expression immediately, then
|
|
|
|
/// calling `std::mem::drop` on the expression is clearer and helps convey
|
|
|
|
/// intent.
|
|
|
|
pub LET_UNDERSCORE_LOCK,
|
2022-06-04 13:50:12 -04:00
|
|
|
Deny,
|
2022-05-31 14:05:04 -04:00
|
|
|
"non-binding let on a synchronization lock"
|
|
|
|
}
|
|
|
|
|
2022-06-05 00:05:50 -04:00
|
|
|
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK]);
|
2022-05-31 14:05:04 -04:00
|
|
|
|
2022-06-04 22:27:32 -04:00
|
|
|
const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [
|
|
|
|
rustc_span::sym::MutexGuard,
|
|
|
|
rustc_span::sym::RwLockReadGuard,
|
|
|
|
rustc_span::sym::RwLockWriteGuard,
|
2022-05-31 14:05:04 -04:00
|
|
|
];
|
2022-05-29 14:35:00 -04:00
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
|
|
|
|
fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
|
|
|
|
if !matches!(local.pat.kind, hir::PatKind::Wild) {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-08 01:15:02 +08:00
|
|
|
|
|
|
|
if matches!(local.source, rustc_hir::LocalSource::AsyncFn) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-29 14:35:00 -04:00
|
|
|
if let Some(init) = local.init {
|
|
|
|
let init_ty = cx.typeck_results().expr_ty(init);
|
2022-06-04 20:19:19 -04:00
|
|
|
// If the type has a trivial Drop implementation, then it doesn't
|
|
|
|
// matter that we drop the value immediately.
|
|
|
|
if !init_ty.needs_drop(cx.tcx, cx.param_env) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-04 22:27:32 -04:00
|
|
|
let is_sync_lock = match init_ty.kind() {
|
|
|
|
ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS
|
|
|
|
.iter()
|
|
|
|
.any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())),
|
|
|
|
_ => false,
|
|
|
|
};
|
2022-06-03 21:33:13 -04:00
|
|
|
|
2022-10-07 06:14:56 -04:00
|
|
|
let sub = NonBindingLetSub {
|
|
|
|
suggestion: local.pat.span,
|
|
|
|
multi_suggestion_start: local.span.until(init.span),
|
|
|
|
multi_suggestion_end: init.span.shrink_to_hi(),
|
2024-01-08 01:15:02 +08:00
|
|
|
is_assign_desugar: matches!(local.source, rustc_hir::LocalSource::AssignDesugar(_)),
|
2022-10-07 06:14:56 -04:00
|
|
|
};
|
2022-05-31 14:05:04 -04:00
|
|
|
if is_sync_lock {
|
2022-08-04 17:00:48 -04:00
|
|
|
let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
|
|
|
|
span.push_span_label(
|
|
|
|
local.pat.span,
|
|
|
|
"this lock is not assigned to a binding and is immediately dropped".to_string(),
|
|
|
|
);
|
|
|
|
span.push_span_label(
|
|
|
|
init.span,
|
|
|
|
"this binding will immediately drop the value assigned to it".to_string(),
|
|
|
|
);
|
2022-10-07 06:14:56 -04:00
|
|
|
cx.emit_spanned_lint(LET_UNDERSCORE_LOCK, span, NonBindingLet::SyncLock { sub });
|
2022-06-04 20:19:19 -04:00
|
|
|
} else {
|
2022-10-07 06:14:56 -04:00
|
|
|
cx.emit_spanned_lint(
|
2022-09-16 11:01:02 +04:00
|
|
|
LET_UNDERSCORE_DROP,
|
|
|
|
local.span,
|
2022-10-07 06:14:56 -04:00
|
|
|
NonBindingLet::DropType { sub },
|
|
|
|
);
|
2022-05-29 14:35:00 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 19:52:12 -04:00
|
|
|
}
|
|
|
|
}
|