]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/let_underscore.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / let_underscore.rs
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};
9
10 declare_clippy_lint! {
11 /// ### What it does
12 /// Checks for `let _ = <expr>` where expr is `#[must_use]`
13 ///
14 /// ### Why is this bad?
15 /// It's better to explicitly handle the value of a `#[must_use]`
16 /// expr
17 ///
18 /// ### Example
19 /// ```rust
20 /// fn f() -> Result<u32, u32> {
21 /// Ok(0)
22 /// }
23 ///
24 /// let _ = f();
25 /// // is_ok() is marked #[must_use]
26 /// let _ = f().is_ok();
27 /// ```
28 #[clippy::version = "1.42.0"]
29 pub LET_UNDERSCORE_MUST_USE,
30 restriction,
31 "non-binding `let` on a `#[must_use]` expression"
32 }
33
34 declare_clippy_lint! {
35 /// ### What it does
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)
39 ///
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.
46 ///
47 /// ### Example
48 /// ```rust,ignore
49 /// let _ = mutex.lock();
50 /// ```
51 ///
52 /// Use instead:
53 /// ```rust,ignore
54 /// let _lock = mutex.lock();
55 /// ```
56 #[clippy::version = "1.43.0"]
57 pub LET_UNDERSCORE_LOCK,
58 correctness,
59 "non-binding `let` on a synchronization lock"
60 }
61
62 declare_clippy_lint! {
63 /// ### What it does
64 /// Checks for `let _ = <expr>` where the resulting type of expr implements `Future`
65 ///
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.
69 ///
70 /// ### Example
71 /// ```rust
72 /// async fn foo() -> Result<(), ()> {
73 /// Ok(())
74 /// }
75 /// let _ = foo();
76 /// ```
77 ///
78 /// Use instead:
79 /// ```rust
80 /// # async fn context() {
81 /// async fn foo() -> Result<(), ()> {
82 /// Ok(())
83 /// }
84 /// let _ = foo().await;
85 /// # }
86 /// ```
87 #[clippy::version = "1.66"]
88 pub LET_UNDERSCORE_FUTURE,
89 suspicious,
90 "non-binding `let` on a future"
91 }
92
93 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_FUTURE]);
94
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,
99 ];
100
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
106 {
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,
111 });
112 if contains_sync_guard {
113 span_lint_and_help(
114 cx,
115 LET_UNDERSCORE_LOCK,
116 local.span,
117 "non-binding `let` on a synchronization lock",
118 None,
119 "consider using an underscore-prefixed named \
120 binding or dropping explicitly with `std::mem::drop`",
121 );
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, &[]) {
124 span_lint_and_help(
125 cx,
126 LET_UNDERSCORE_FUTURE,
127 local.span,
128 "non-binding `let` on a future",
129 None,
130 "consider awaiting the future or dropping explicitly with `std::mem::drop`"
131 );
132 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
133 span_lint_and_help(
134 cx,
135 LET_UNDERSCORE_MUST_USE,
136 local.span,
137 "non-binding `let` on an expression with `#[must_use]` type",
138 None,
139 "consider explicitly using expression value",
140 );
141 } else if is_must_use_func_call(cx, init) {
142 span_lint_and_help(
143 cx,
144 LET_UNDERSCORE_MUST_USE,
145 local.span,
146 "non-binding `let` on a result of a `#[must_use]` function",
147 None,
148 "consider explicitly using function result",
149 );
150 }
151 }
152 }
153 }