]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/let_underscore.rs
Merge commit 'c2c07fa9d095931eb5684a42942a7b573a0c5238' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / let_underscore.rs
1 use if_chain::if_chain;
2 use rustc_hir::{Local, PatKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_middle::lint::in_external_macro;
5 use rustc_middle::ty::subst::GenericArgKind;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7
8 use crate::utils::{is_must_use_func_call, is_must_use_ty, match_type, paths, span_lint_and_help};
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for `let _ = <expr>`
12     /// where expr is #[must_use]
13     ///
14     /// **Why is this bad?** It's better to explicitly
15     /// handle the value of a #[must_use] expr
16     ///
17     /// **Known problems:** None.
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     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:** Checks for `let _ = sync_lock`
36     ///
37     /// **Why is this bad?** This statement immediately drops the lock instead of
38     /// extending its lifetime to the end of the scope, which is often not intended.
39     /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
40     /// name instead (i.e. _lock). If you want to explicitly drop the lock,
41     /// `std::mem::drop` conveys your intention better and is less error-prone.
42     ///
43     /// **Known problems:** None.
44     ///
45     /// **Example:**
46     ///
47     /// Bad:
48     /// ```rust,ignore
49     /// let _ = mutex.lock();
50     /// ```
51     ///
52     /// Good:
53     /// ```rust,ignore
54     /// let _lock = mutex.lock();
55     /// ```
56     pub LET_UNDERSCORE_LOCK,
57     correctness,
58     "non-binding let on a synchronization lock"
59 }
60
61 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK]);
62
63 const SYNC_GUARD_PATHS: [&[&str]; 3] = [
64     &paths::MUTEX_GUARD,
65     &paths::RWLOCK_READ_GUARD,
66     &paths::RWLOCK_WRITE_GUARD,
67 ];
68
69 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
70     fn check_local(&mut self, cx: &LateContext<'_, '_>, local: &Local<'_>) {
71         if in_external_macro(cx.tcx.sess, local.span) {
72             return;
73         }
74
75         if_chain! {
76             if let PatKind::Wild = local.pat.kind;
77             if let Some(ref init) = local.init;
78             then {
79                 let init_ty = cx.tables.expr_ty(init);
80                 let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
81                     GenericArgKind::Type(inner_ty) => {
82                         SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
83                     },
84
85                     GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
86                 });
87                 if contains_sync_guard {
88                     span_lint_and_help(
89                         cx,
90                         LET_UNDERSCORE_LOCK,
91                         local.span,
92                         "non-binding let on a synchronization lock",
93                         None,
94                         "consider using an underscore-prefixed named \
95                             binding or dropping explicitly with `std::mem::drop`"
96                     )
97                 } else if is_must_use_ty(cx, cx.tables.expr_ty(init)) {
98                     span_lint_and_help(
99                         cx,
100                         LET_UNDERSCORE_MUST_USE,
101                         local.span,
102                         "non-binding let on an expression with `#[must_use]` type",
103                         None,
104                         "consider explicitly using expression value"
105                     )
106                 } else if is_must_use_func_call(cx, init) {
107                     span_lint_and_help(
108                         cx,
109                         LET_UNDERSCORE_MUST_USE,
110                         local.span,
111                         "non-binding let on a result of a `#[must_use]` function",
112                         None,
113                         "consider explicitly using function result"
114                     )
115                 }
116             }
117         }
118     }
119 }