]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/let_underscore.rs
New upstream version 1.64.0+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::{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};
10
11 declare_clippy_lint! {
12 /// ### What it does
13 /// Checks for `let _ = <expr>` where expr is `#[must_use]`
14 ///
15 /// ### Why is this bad?
16 /// It's better to explicitly handle the value of a `#[must_use]`
17 /// expr
18 ///
19 /// ### Example
20 /// ```rust
21 /// fn f() -> Result<u32, u32> {
22 /// Ok(0)
23 /// }
24 ///
25 /// let _ = f();
26 /// // is_ok() is marked #[must_use]
27 /// let _ = f().is_ok();
28 /// ```
29 #[clippy::version = "1.42.0"]
30 pub LET_UNDERSCORE_MUST_USE,
31 restriction,
32 "non-binding let on a `#[must_use]` expression"
33 }
34
35 declare_clippy_lint! {
36 /// ### What it does
37 /// Checks for `let _ = sync_lock`.
38 /// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`.
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>`
65 /// where expr has a type that implements `Drop`
66 ///
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.
74 ///
75 /// ### Example
76 /// ```rust
77 /// # struct DroppableItem;
78 /// {
79 /// let _ = DroppableItem;
80 /// // ^ dropped here
81 /// /* more code */
82 /// }
83 /// ```
84 ///
85 /// Use instead:
86 /// ```rust
87 /// # struct DroppableItem;
88 /// {
89 /// let _droppable = DroppableItem;
90 /// /* more code */
91 /// // dropped at end of scope
92 /// }
93 /// ```
94 #[clippy::version = "1.50.0"]
95 pub LET_UNDERSCORE_DROP,
96 pedantic,
97 "non-binding let on a type that implements `Drop`"
98 }
99
100 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
101
102 const SYNC_GUARD_PATHS: [&[&str]; 6] = [
103 &paths::MUTEX_GUARD,
104 &paths::RWLOCK_READ_GUARD,
105 &paths::RWLOCK_WRITE_GUARD,
106 &paths::PARKING_LOT_MUTEX_GUARD,
107 &paths::PARKING_LOT_RWLOCK_READ_GUARD,
108 &paths::PARKING_LOT_RWLOCK_WRITE_GUARD,
109 ];
110
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) {
114 return;
115 }
116
117 if_chain! {
118 if let PatKind::Wild = local.pat.kind;
119 if let Some(init) = local.init;
120 then {
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) => {
124 SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
125 },
126
127 GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
128 });
129 if contains_sync_guard {
130 span_lint_and_help(
131 cx,
132 LET_UNDERSCORE_LOCK,
133 local.span,
134 "non-binding let on a synchronization lock",
135 None,
136 "consider using an underscore-prefixed named \
137 binding or dropping explicitly with `std::mem::drop`"
138 );
139 } else if init_ty.needs_drop(cx.tcx, cx.param_env) {
140 span_lint_and_help(
141 cx,
142 LET_UNDERSCORE_DROP,
143 local.span,
144 "non-binding `let` on a type that implements `Drop`",
145 None,
146 "consider using an underscore-prefixed named \
147 binding or dropping explicitly with `std::mem::drop`"
148 );
149 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
150 span_lint_and_help(
151 cx,
152 LET_UNDERSCORE_MUST_USE,
153 local.span,
154 "non-binding let on an expression with `#[must_use]` type",
155 None,
156 "consider explicitly using expression value"
157 );
158 } else if is_must_use_func_call(cx, init) {
159 span_lint_and_help(
160 cx,
161 LET_UNDERSCORE_MUST_USE,
162 local.span,
163 "non-binding let on a result of a `#[must_use]` function",
164 None,
165 "consider explicitly using function result"
166 );
167 }
168 }
169 }
170 }
171 }