1- use crate :: utils:: { is_type_diagnostic_item, span_lint_and_help } ;
1+ use crate :: utils:: { is_type_diagnostic_item, span_lint_and_sugg } ;
22use if_chain:: if_chain;
3+ use rustc_errors:: Applicability ;
34use rustc_hir:: { Expr , ExprKind , Mutability } ;
45use rustc_lint:: { LateContext , LateLintPass } ;
56use rustc_middle:: ty;
@@ -9,7 +10,9 @@ declare_clippy_lint! {
910 /// **What it does:** Checks for `&mut Mutex::lock` calls
1011 ///
1112 /// **Why is this bad?** `Mutex::lock` is less efficient than
12- /// calling `Mutex::get_mut`
13+ /// calling `Mutex::get_mut`. In addition you also have a statically
14+ /// guarantee that the mutex isn't locked, instead of just a runtime
15+ /// guarantee.
1316 ///
1417 /// **Known problems:** None.
1518 ///
@@ -44,19 +47,20 @@ declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
4447impl < ' tcx > LateLintPass < ' tcx > for MutMutexLock {
4548 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , ex : & ' tcx Expr < ' tcx > ) {
4649 if_chain ! {
47- if let ExprKind :: MethodCall ( path, _span , args, _) = & ex. kind;
50+ if let ExprKind :: MethodCall ( path, method_span , args, _) = & ex. kind;
4851 if path. ident. name == sym!( lock) ;
4952 let ty = cx. typeck_results( ) . expr_ty( & args[ 0 ] ) ;
5053 if let ty:: Ref ( _, inner_ty, Mutability :: Mut ) = ty. kind( ) ;
5154 if is_type_diagnostic_item( cx, inner_ty, sym!( mutex_type) ) ;
5255 then {
53- span_lint_and_help (
56+ span_lint_and_sugg (
5457 cx,
5558 MUT_MUTEX_LOCK ,
56- ex . span ,
59+ * method_span ,
5760 "calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference" ,
58- None ,
59- "use `&mut Mutex::get_mut` instead" ,
61+ "change this to" ,
62+ "get_mut" . to_owned( ) ,
63+ Applicability :: MachineApplicable ,
6064 ) ;
6165 }
6266 }
0 commit comments