]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/let_underscore.rs
Split out `infalliable_detructuring_match`
[rust.git] / 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     ///
49     /// Bad:
50     /// ```rust,ignore
51     /// let _ = mutex.lock();
52     /// ```
53     ///
54     /// Good:
55     /// ```rust,ignore
56     /// let _lock = mutex.lock();
57     /// ```
58     #[clippy::version = "1.43.0"]
59     pub LET_UNDERSCORE_LOCK,
60     correctness,
61     "non-binding let on a synchronization lock"
62 }
63
64 declare_clippy_lint! {
65     /// ### What it does
66     /// Checks for `let _ = <expr>`
67     /// where expr has a type that implements `Drop`
68     ///
69     /// ### Why is this bad?
70     /// This statement immediately drops the initializer
71     /// expression instead of extending its lifetime to the end of the scope, which
72     /// is often not intended. To extend the expression's lifetime to the end of the
73     /// scope, use an underscore-prefixed name instead (i.e. _var). If you want to
74     /// explicitly drop the expression, `std::mem::drop` conveys your intention
75     /// better and is less error-prone.
76     ///
77     /// ### Example
78     ///
79     /// Bad:
80     /// ```rust,ignore
81     /// struct Droppable;
82     /// impl Drop for Droppable {
83     ///     fn drop(&mut self) {}
84     /// }
85     /// {
86     ///     let _ = Droppable;
87     ///     //               ^ dropped here
88     ///     /* more code */
89     /// }
90     /// ```
91     ///
92     /// Good:
93     /// ```rust,ignore
94     /// {
95     ///     let _droppable = Droppable;
96     ///     /* more code */
97     ///     // dropped at end of scope
98     /// }
99     /// ```
100     #[clippy::version = "1.50.0"]
101     pub LET_UNDERSCORE_DROP,
102     pedantic,
103     "non-binding let on a type that implements `Drop`"
104 }
105
106 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
107
108 const SYNC_GUARD_PATHS: [&[&str]; 5] = [
109     &paths::MUTEX_GUARD,
110     &paths::RWLOCK_READ_GUARD,
111     &paths::RWLOCK_WRITE_GUARD,
112     &paths::PARKING_LOT_RAWMUTEX,
113     &paths::PARKING_LOT_RAWRWLOCK,
114 ];
115
116 impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
117     fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
118         if in_external_macro(cx.tcx.sess, local.span) {
119             return;
120         }
121
122         if_chain! {
123             if let PatKind::Wild = local.pat.kind;
124             if let Some(init) = local.init;
125             then {
126                 let init_ty = cx.typeck_results().expr_ty(init);
127                 let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
128                     GenericArgKind::Type(inner_ty) => {
129                         SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
130                     },
131
132                     GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
133                 });
134                 if contains_sync_guard {
135                     span_lint_and_help(
136                         cx,
137                         LET_UNDERSCORE_LOCK,
138                         local.span,
139                         "non-binding let on a synchronization lock",
140                         None,
141                         "consider using an underscore-prefixed named \
142                             binding or dropping explicitly with `std::mem::drop`"
143                     );
144                 } else if init_ty.needs_drop(cx.tcx, cx.param_env) {
145                     span_lint_and_help(
146                         cx,
147                         LET_UNDERSCORE_DROP,
148                         local.span,
149                         "non-binding `let` on a type that implements `Drop`",
150                         None,
151                         "consider using an underscore-prefixed named \
152                             binding or dropping explicitly with `std::mem::drop`"
153                     );
154                 } else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
155                     span_lint_and_help(
156                         cx,
157                         LET_UNDERSCORE_MUST_USE,
158                         local.span,
159                         "non-binding let on an expression with `#[must_use]` type",
160                         None,
161                         "consider explicitly using expression value"
162                     );
163                 } else if is_must_use_func_call(cx, init) {
164                     span_lint_and_help(
165                         cx,
166                         LET_UNDERSCORE_MUST_USE,
167                         local.span,
168                         "non-binding let on a result of a `#[must_use]` function",
169                         None,
170                         "consider explicitly using function result"
171                     );
172                 }
173             }
174         }
175     }
176 }