2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2020-10-28 22:36:07 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Expr, ExprKind, Mutability};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-10-02 23:51:01 +00:00
|
|
|
use rustc_span::sym;
|
2020-10-28 22:36:07 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `&mut Mutex::lock` calls
|
2020-10-28 22:36:07 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// `Mutex::lock` is less efficient than
|
2020-10-28 22:36:07 +00:00
|
|
|
/// calling `Mutex::get_mut`. In addition you also have a statically
|
|
|
|
/// guarantee that the mutex isn't locked, instead of just a runtime
|
|
|
|
/// guarantee.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2020-10-28 22:36:07 +00:00
|
|
|
/// ```rust
|
|
|
|
/// use std::sync::{Arc, Mutex};
|
|
|
|
///
|
|
|
|
/// let mut value_rc = Arc::new(Mutex::new(42_u8));
|
|
|
|
/// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
|
|
|
|
///
|
|
|
|
/// let mut value = value_mutex.lock().unwrap();
|
|
|
|
/// *value += 1;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// use std::sync::{Arc, Mutex};
|
|
|
|
///
|
|
|
|
/// let mut value_rc = Arc::new(Mutex::new(42_u8));
|
|
|
|
/// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
|
|
|
|
///
|
|
|
|
/// let value = value_mutex.get_mut().unwrap();
|
|
|
|
/// *value += 1;
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.49.0"]
|
2020-10-28 22:36:07 +00:00
|
|
|
pub MUT_MUTEX_LOCK,
|
|
|
|
style,
|
|
|
|
"`&mut Mutex::lock` does unnecessary locking"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
|
|
|
|
if_chain! {
|
2021-12-01 17:17:50 +00:00
|
|
|
if let ExprKind::MethodCall(path, [self_arg, ..], _) = &ex.kind;
|
2020-10-28 22:36:07 +00:00
|
|
|
if path.ident.name == sym!(lock);
|
2021-09-08 14:31:47 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(self_arg);
|
2020-10-28 22:36:07 +00:00
|
|
|
if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
if is_type_diagnostic_item(cx, *inner_ty, sym::Mutex);
|
2020-10-28 22:36:07 +00:00
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MUT_MUTEX_LOCK,
|
2021-12-01 17:17:50 +00:00
|
|
|
path.ident.span,
|
2020-10-28 22:36:07 +00:00
|
|
|
"calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference",
|
|
|
|
"change this to",
|
|
|
|
"get_mut".to_owned(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|