1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type};
3 use clippy_utils::{is_must_use_func_call, paths};
4 use rustc_hir::{Local, PatKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::lint::in_external_macro;
7 use rustc_middle::ty::subst::GenericArgKind;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
12 /// Checks for `let _ = <expr>` where expr is `#[must_use]`
14 /// ### Why is this bad?
15 /// It's better to explicitly handle the value of a `#[must_use]`
20 /// fn f() -> Result<u32, u32> {
25 /// // is_ok() is marked #[must_use]
26 /// let _ = f().is_ok();
28 #[clippy::version = "1.42.0"]
29 pub LET_UNDERSCORE_MUST_USE,
31 "non-binding `let` on a `#[must_use]` expression"
34 declare_clippy_lint! {
36 /// Checks for `let _ = sync_lock`. This supports `mutex` and `rwlock` in
37 /// `parking_lot`. For `std` locks see the `rustc` lint
38 /// [`let_underscore_lock`](https://doc.rust-lang.org/nightly/rustc/lints/listing/deny-by-default.html#let-underscore-lock)
40 /// ### Why is this bad?
41 /// This statement immediately drops the lock instead of
42 /// extending its lifetime to the end of the scope, which is often not intended.
43 /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
44 /// name instead (i.e. _lock). If you want to explicitly drop the lock,
45 /// `std::mem::drop` conveys your intention better and is less error-prone.
49 /// let _ = mutex.lock();
54 /// let _lock = mutex.lock();
56 #[clippy::version = "1.43.0"]
57 pub LET_UNDERSCORE_LOCK,
59 "non-binding `let` on a synchronization lock"
62 declare_clippy_lint! {
64 /// Checks for `let _ = <expr>` where the resulting type of expr implements `Future`
66 /// ### Why is this bad?
67 /// Futures must be polled for work to be done. The original intention was most likely to await the future
68 /// and ignore the resulting value.
72 /// async fn foo() -> Result<(), ()> {
80 /// # async fn context() {
81 /// async fn foo() -> Result<(), ()> {
84 /// let _ = foo().await;
87 #[clippy::version = "1.66"]
88 pub LET_UNDERSCORE_FUTURE,
90 "non-binding `let` on a future"
93 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_FUTURE]);
95 const SYNC_GUARD_PATHS: [&[&str]; 3] = [
96 &paths::PARKING_LOT_MUTEX_GUARD,
97 &paths::PARKING_LOT_RWLOCK_READ_GUARD,
98 &paths::PARKING_LOT_RWLOCK_WRITE_GUARD,
101 impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
102 fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
103 if !in_external_macro(cx.tcx.sess, local.span)
104 && let PatKind::Wild = local.pat.kind
105 && let Some(init) = local.init
107 let init_ty = cx.typeck_results().expr_ty(init);
108 let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
109 GenericArgKind::Type(inner_ty) => SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)),
110 GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
112 if contains_sync_guard {
117 "non-binding `let` on a synchronization lock",
119 "consider using an underscore-prefixed named \
120 binding or dropping explicitly with `std::mem::drop`",
122 } else if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
123 && implements_trait(cx, cx.typeck_results().expr_ty(init), future_trait_def_id, &[]) {
126 LET_UNDERSCORE_FUTURE,
128 "non-binding `let` on a future",
130 "consider awaiting the future or dropping explicitly with `std::mem::drop`"
132 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
135 LET_UNDERSCORE_MUST_USE,
137 "non-binding `let` on an expression with `#[must_use]` type",
139 "consider explicitly using expression value",
141 } else if is_must_use_func_call(cx, init) {
144 LET_UNDERSCORE_MUST_USE,
146 "non-binding `let` on a result of a `#[must_use]` function",
148 "consider explicitly using function result",