1 use if_chain::if_chain;
2 use rustc_hir::{Local, PatKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_middle::lint::in_external_macro;
5 use rustc_middle::ty::subst::GenericArgKind;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use crate::utils::{implements_trait, is_must_use_func_call, is_must_use_ty, match_type, paths, span_lint_and_help};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for `let _ = <expr>`
12 /// where expr is #[must_use]
14 /// **Why is this bad?** It's better to explicitly
15 /// handle the value of a #[must_use] expr
17 /// **Known problems:** None.
21 /// fn f() -> Result<u32, u32> {
26 /// // is_ok() is marked #[must_use]
27 /// let _ = f().is_ok();
29 pub LET_UNDERSCORE_MUST_USE,
31 "non-binding let on a `#[must_use]` expression"
34 declare_clippy_lint! {
35 /// **What it does:** Checks for `let _ = sync_lock`
37 /// **Why is this bad?** This statement immediately drops the lock instead of
38 /// extending its lifetime to the end of the scope, which is often not intended.
39 /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
40 /// name instead (i.e. _lock). If you want to explicitly drop the lock,
41 /// `std::mem::drop` conveys your intention better and is less error-prone.
43 /// **Known problems:** None.
49 /// let _ = mutex.lock();
54 /// let _lock = mutex.lock();
56 pub LET_UNDERSCORE_LOCK,
58 "non-binding let on a synchronization lock"
61 declare_clippy_lint! {
62 /// **What it does:** Checks for `let _ = <expr>`
63 /// where expr has a type that implements `Drop`
65 /// **Why is this bad?** This statement immediately drops the initializer
66 /// expression instead of extending its lifetime to the end of the scope, which
67 /// is often not intended. To extend the expression's lifetime to the end of the
68 /// scope, use an underscore-prefixed name instead (i.e. _var). If you want to
69 /// explicitly drop the expression, `std::mem::drop` conveys your intention
70 /// better and is less error-prone.
72 /// **Known problems:** None.
79 /// impl Drop for Droppable {
80 /// fn drop(&mut self) {}
83 /// let _ = Droppable;
92 /// let _droppable = Droppable;
94 /// // dropped at end of scope
97 pub LET_UNDERSCORE_DROP,
99 "non-binding let on a type that implements `Drop`"
102 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
104 const SYNC_GUARD_PATHS: [&[&str]; 3] = [
106 &paths::RWLOCK_READ_GUARD,
107 &paths::RWLOCK_WRITE_GUARD,
110 impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
111 fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
112 if in_external_macro(cx.tcx.sess, local.span) {
117 if let PatKind::Wild = local.pat.kind;
118 if let Some(ref init) = local.init;
120 let init_ty = cx.typeck_results().expr_ty(init);
121 let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
122 GenericArgKind::Type(inner_ty) => {
123 SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
126 GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
128 let implements_drop = cx.tcx.lang_items().drop_trait().map_or(false, |drop_trait|
129 init_ty.walk().any(|inner| match inner.unpack() {
130 GenericArgKind::Type(inner_ty) => {
131 implements_trait(cx, inner_ty, drop_trait, &[])
134 GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
137 if contains_sync_guard {
142 "non-binding let on a synchronization lock",
144 "consider using an underscore-prefixed named \
145 binding or dropping explicitly with `std::mem::drop`"
147 } else if implements_drop {
152 "non-binding `let` on a type that implements `Drop`",
154 "consider using an underscore-prefixed named \
155 binding or dropping explicitly with `std::mem::drop`"
157 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
160 LET_UNDERSCORE_MUST_USE,
162 "non-binding let on an expression with `#[must_use]` type",
164 "consider explicitly using expression value"
166 } else if is_must_use_func_call(cx, init) {
169 LET_UNDERSCORE_MUST_USE,
171 "non-binding let on a result of a `#[must_use]` function",
173 "consider explicitly using function result"