@@ -4,7 +4,7 @@ use rustc_hir::*;
44use rustc_lint:: { LateContext , LateLintPass } ;
55use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
66
7- use crate :: utils:: { is_must_use_func_call, is_must_use_ty, match_def_path , paths, span_lint_and_help} ;
7+ use crate :: utils:: { is_must_use_func_call, is_must_use_ty, match_type , paths, span_lint_and_help} ;
88
99declare_clippy_lint ! {
1010 /// **What it does:** Checks for `let _ = <expr>`
@@ -31,12 +31,13 @@ declare_clippy_lint! {
3131}
3232
3333declare_clippy_lint ! {
34- /// **What it does:** Checks for `let _ = sync_primitive.lock() `
34+ /// **What it does:** Checks for `let _ = sync_lock `
3535 ///
36- /// **Why is this bad?** This statement locks the synchronization
37- /// primitive and immediately drops the lock, which is probably
38- /// not intended. To extend lock lifetime to the end of the scope,
39- /// use an underscore-prefixed name instead (i.e. _lock).
36+ /// **Why is this bad?** This statement immediately drops the lock instead of
37+ /// extending it's lifetime to the end of the scope, which is often not intended.
38+ /// To extend lock lifetime to the end of the scope, use an underscore-prefixed
39+ /// name instead (i.e. _lock). If you want to explicitly drop the lock,
40+ /// `std::mem::drop` conveys your intention better and is less error-prone.
4041 ///
4142 /// **Known problems:** None.
4243 ///
@@ -58,7 +59,11 @@ declare_clippy_lint! {
5859
5960declare_lint_pass ! ( LetUnderscore => [ LET_UNDERSCORE_MUST_USE , LET_UNDERSCORE_LOCK ] ) ;
6061
61- const LOCK_METHODS_PATHS : [ & [ & str ] ; 3 ] = [ & paths:: MUTEX_LOCK , & paths:: RWLOCK_READ , & paths:: RWLOCK_WRITE ] ;
62+ const SYNC_GUARD_PATHS : [ & [ & str ] ; 3 ] = [
63+ & paths:: MUTEX_GUARD ,
64+ & paths:: RWLOCK_READ_GUARD ,
65+ & paths:: RWLOCK_WRITE_GUARD ,
66+ ] ;
6267
6368impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for LetUnderscore {
6469 fn check_stmt ( & mut self , cx : & LateContext < ' _ , ' _ > , stmt : & Stmt < ' _ > ) {
@@ -71,37 +76,32 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore {
7176 if let PatKind :: Wild = local. pat. kind;
7277 if let Some ( ref init) = local. init;
7378 then {
74- if_chain! {
75- if let ExprKind :: MethodCall ( _, _, _) = init. kind;
76- let method_did = cx. tables. type_dependent_def_id( init. hir_id) . unwrap( ) ;
77- if LOCK_METHODS_PATHS . iter( ) . any( |path| match_def_path( cx, method_did, path) ) ;
78- then {
79- span_lint_and_help(
80- cx,
81- LET_UNDERSCORE_LOCK ,
82- stmt. span,
83- "non-binding let on a synchronization lock" ,
84- "consider using an underscore-prefixed named binding"
85- )
86- } else {
87- if is_must_use_ty( cx, cx. tables. expr_ty( init) ) {
88- span_lint_and_help(
89- cx,
90- LET_UNDERSCORE_MUST_USE ,
91- stmt. span,
92- "non-binding let on an expression with `#[must_use]` type" ,
93- "consider explicitly using expression value"
94- )
95- } else if is_must_use_func_call( cx, init) {
96- span_lint_and_help(
97- cx,
98- LET_UNDERSCORE_MUST_USE ,
99- stmt. span,
100- "non-binding let on a result of a `#[must_use]` function" ,
101- "consider explicitly using function result"
102- )
103- }
104- }
79+ let check_ty = |ty| SYNC_GUARD_PATHS . iter( ) . any( |path| match_type( cx, ty, path) ) ;
80+ if cx. tables. expr_ty( init) . walk( ) . any( check_ty) {
81+ span_lint_and_help(
82+ cx,
83+ LET_UNDERSCORE_LOCK ,
84+ stmt. span,
85+ "non-binding let on a synchronization lock" ,
86+ "consider using an underscore-prefixed named \
87+ binding or dropping explicitly with `std::mem::drop`"
88+ )
89+ } else if is_must_use_ty( cx, cx. tables. expr_ty( init) ) {
90+ span_lint_and_help(
91+ cx,
92+ LET_UNDERSCORE_MUST_USE ,
93+ stmt. span,
94+ "non-binding let on an expression with `#[must_use]` type" ,
95+ "consider explicitly using expression value"
96+ )
97+ } else if is_must_use_func_call( cx, init) {
98+ span_lint_and_help(
99+ cx,
100+ LET_UNDERSCORE_MUST_USE ,
101+ stmt. span,
102+ "non-binding let on a result of a `#[must_use]` function" ,
103+ "consider explicitly using function result"
104+ )
105105 }
106106 }
107107 }
0 commit comments