1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, match_type};
3 use clippy_utils::{is_must_use_func_call, paths};
4 use if_chain::if_chain;
5 use rustc_hir::{Local, PatKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_middle::lint::in_external_macro;
8 use rustc_middle::ty::subst::GenericArgKind;
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 use rustc_span::{sym, Symbol};
12 declare_clippy_lint! {
14 /// Checks for `let _ = <expr>` where expr is `#[must_use]`
16 /// ### Why is this bad?
17 /// It's better to explicitly handle the value of a `#[must_use]`
22 /// fn f() -> Result<u32, u32> {
27 /// // is_ok() is marked #[must_use]
28 /// let _ = f().is_ok();
30 #[clippy::version = "1.42.0"]
31 pub LET_UNDERSCORE_MUST_USE,
33 "non-binding let on a `#[must_use]` expression"
36 declare_clippy_lint! {
38 /// Checks for `let _ = sync_lock`.
39 /// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`.
41 /// ### Why is this bad?
42 /// This statement immediately drops the lock instead of
43 /// extending its lifetime to the end of the scope, which is often not intended.
44 /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
45 /// name instead (i.e. _lock). If you want to explicitly drop the lock,
46 /// `std::mem::drop` conveys your intention better and is less error-prone.
50 /// let _ = mutex.lock();
55 /// let _lock = mutex.lock();
57 #[clippy::version = "1.43.0"]
58 pub LET_UNDERSCORE_LOCK,
60 "non-binding let on a synchronization lock"
63 declare_clippy_lint! {
65 /// Checks for `let _ = <expr>`
66 /// where expr has a type that implements `Drop`
68 /// ### Why is this bad?
69 /// This statement immediately drops the initializer
70 /// expression instead of extending its lifetime to the end of the scope, which
71 /// is often not intended. To extend the expression's lifetime to the end of the
72 /// scope, use an underscore-prefixed name instead (i.e. _var). If you want to
73 /// explicitly drop the expression, `std::mem::drop` conveys your intention
74 /// better and is less error-prone.
78 /// # struct DroppableItem;
80 /// let _ = DroppableItem;
88 /// # struct DroppableItem;
90 /// let _droppable = DroppableItem;
92 /// // dropped at end of scope
95 #[clippy::version = "1.50.0"]
96 pub LET_UNDERSCORE_DROP,
98 "non-binding let on a type that implements `Drop`"
101 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
103 const SYNC_GUARD_SYMS: [Symbol; 3] = [sym::MutexGuard, sym::RwLockReadGuard, sym::RwLockWriteGuard];
105 const SYNC_GUARD_PATHS: [&[&str]; 3] = [
106 &paths::PARKING_LOT_MUTEX_GUARD,
107 &paths::PARKING_LOT_RWLOCK_READ_GUARD,
108 &paths::PARKING_LOT_RWLOCK_WRITE_GUARD,
111 impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
112 fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
113 if in_external_macro(cx.tcx.sess, local.span) {
118 if let PatKind::Wild = local.pat.kind;
119 if let Some(init) = local.init;
121 let init_ty = cx.typeck_results().expr_ty(init);
122 let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
123 GenericArgKind::Type(inner_ty) => {
126 .any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym))
127 || SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
130 GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
132 if contains_sync_guard {
137 "non-binding let on a synchronization lock",
139 "consider using an underscore-prefixed named \
140 binding or dropping explicitly with `std::mem::drop`",
142 } else if init_ty.needs_drop(cx.tcx, cx.param_env) {
147 "non-binding `let` on a type that implements `Drop`",
149 "consider using an underscore-prefixed named \
150 binding or dropping explicitly with `std::mem::drop`",
152 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
155 LET_UNDERSCORE_MUST_USE,
157 "non-binding let on an expression with `#[must_use]` type",
159 "consider explicitly using expression value",
161 } else if is_must_use_func_call(cx, init) {
164 LET_UNDERSCORE_MUST_USE,
166 "non-binding let on a result of a `#[must_use]` function",
168 "consider explicitly using function result",