2021-03-12 08:30:50 -06:00
|
|
|
use super::USELESS_TRANSMUTE;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
|
|
|
use clippy_utils::sugg;
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
|
|
|
|
/// Checks for `useless_transmute` lint.
|
|
|
|
/// Returns `true` if it's triggered, otherwise returns `false`.
|
|
|
|
pub(super) fn check<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
e: &'tcx Expr<'_>,
|
|
|
|
from_ty: Ty<'tcx>,
|
|
|
|
to_ty: Ty<'tcx>,
|
2022-02-10 11:40:06 -06:00
|
|
|
arg: &'tcx Expr<'_>,
|
2021-03-12 08:30:50 -06:00
|
|
|
) -> bool {
|
|
|
|
match (&from_ty.kind(), &to_ty.kind()) {
|
|
|
|
_ if from_ty == to_ty => {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
USELESS_TRANSMUTE,
|
|
|
|
e.span,
|
|
|
|
&format!("transmute from a type (`{}`) to itself", from_ty),
|
|
|
|
);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
(ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
USELESS_TRANSMUTE,
|
|
|
|
e.span,
|
|
|
|
"transmute from a reference to a pointer",
|
|
|
|
|diag| {
|
2022-02-10 11:40:06 -06:00
|
|
|
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
2021-03-12 08:30:50 -06:00
|
|
|
let rty_and_mut = ty::TypeAndMut {
|
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-24 21:13:38 -06:00
|
|
|
ty: *rty,
|
2021-03-12 08:30:50 -06:00
|
|
|
mutbl: *rty_mutbl,
|
|
|
|
};
|
|
|
|
|
|
|
|
let sugg = if *ptr_ty == rty_and_mut {
|
|
|
|
arg.as_ty(to_ty)
|
|
|
|
} else {
|
|
|
|
arg.as_ty(cx.tcx.mk_ptr(rty_and_mut)).as_ty(to_ty)
|
|
|
|
};
|
|
|
|
|
|
|
|
diag.span_suggestion(e.span, "try", sugg.to_string(), Applicability::Unspecified);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
(ty::Int(_) | ty::Uint(_), ty::RawPtr(_)) => {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
USELESS_TRANSMUTE,
|
|
|
|
e.span,
|
|
|
|
"transmute from an integer to a pointer",
|
|
|
|
|diag| {
|
2022-02-10 11:40:06 -06:00
|
|
|
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
2021-03-12 08:30:50 -06:00
|
|
|
diag.span_suggestion(
|
|
|
|
e.span,
|
|
|
|
"try",
|
|
|
|
arg.as_ty(&to_ty.to_string()).to_string(),
|
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|