1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::{is_must_use_ty, 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};
11 declare_clippy_lint! {
13 /// Checks for `let _ = <expr>`
14 /// where expr is #[must_use]
16 /// ### Why is this bad?
17 /// It's better to explicitly
18 /// handle the value of a #[must_use] expr
22 /// fn f() -> Result<u32, u32> {
27 /// // is_ok() is marked #[must_use]
28 /// let _ = f().is_ok();
30 pub LET_UNDERSCORE_MUST_USE,
32 "non-binding let on a `#[must_use]` expression"
35 declare_clippy_lint! {
37 /// Checks for `let _ = sync_lock`
39 /// ### Why is this bad?
40 /// This statement immediately drops the lock instead of
41 /// extending its lifetime to the end of the scope, which is often not intended.
42 /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
43 /// name instead (i.e. _lock). If you want to explicitly drop the lock,
44 /// `std::mem::drop` conveys your intention better and is less error-prone.
50 /// let _ = mutex.lock();
55 /// let _lock = mutex.lock();
57 pub LET_UNDERSCORE_LOCK,
59 "non-binding let on a synchronization lock"
62 declare_clippy_lint! {
64 /// Checks for `let _ = <expr>`
65 /// where expr has a type that implements `Drop`
67 /// ### Why is this bad?
68 /// This statement immediately drops the initializer
69 /// expression instead of extending its lifetime to the end of the scope, which
70 /// is often not intended. To extend the expression's lifetime to the end of the
71 /// scope, use an underscore-prefixed name instead (i.e. _var). If you want to
72 /// explicitly drop the expression, `std::mem::drop` conveys your intention
73 /// better and is less error-prone.
80 /// impl Drop for Droppable {
81 /// fn drop(&mut self) {}
84 /// let _ = Droppable;
93 /// let _droppable = Droppable;
95 /// // dropped at end of scope
98 pub LET_UNDERSCORE_DROP,
100 "non-binding let on a type that implements `Drop`"
103 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
105 const SYNC_GUARD_PATHS: [&[&str]; 3] = [
107 &paths::RWLOCK_READ_GUARD,
108 &paths::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(cx.tcx).any(|inner| match inner.unpack() {
123 GenericArgKind::Type(inner_ty) => {
124 SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
127 GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
129 if contains_sync_guard {
134 "non-binding let on a synchronization lock",
136 "consider using an underscore-prefixed named \
137 binding or dropping explicitly with `std::mem::drop`"
139 } else if init_ty.needs_drop(cx.tcx, cx.param_env) {
144 "non-binding `let` on a type that implements `Drop`",
146 "consider using an underscore-prefixed named \
147 binding or dropping explicitly with `std::mem::drop`"
149 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
152 LET_UNDERSCORE_MUST_USE,
154 "non-binding let on an expression with `#[must_use]` type",
156 "consider explicitly using expression value"
158 } else if is_must_use_func_call(cx, init) {
161 LET_UNDERSCORE_MUST_USE,
163 "non-binding let on a result of a `#[must_use]` function",
165 "consider explicitly using function result"