mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-01-02 00:09:08 +00:00
5fa961b951
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.
87 lines
3.3 KiB
Rust
87 lines
3.3 KiB
Rust
use super::{TRANSMUTE_BYTES_TO_STR, TRANSMUTE_PTR_TO_PTR};
|
|
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
|
|
use clippy_utils::source::snippet;
|
|
use clippy_utils::sugg;
|
|
use if_chain::if_chain;
|
|
use rustc_errors::Applicability;
|
|
use rustc_hir::{Expr, Mutability};
|
|
use rustc_lint::LateContext;
|
|
use rustc_middle::ty::{self, Ty};
|
|
|
|
/// Checks for `transmute_bytes_to_str` and `transmute_ptr_to_ptr` lints.
|
|
/// Returns `true` if either one triggered, otherwise returns `false`.
|
|
pub(super) fn check<'tcx>(
|
|
cx: &LateContext<'tcx>,
|
|
e: &'tcx Expr<'_>,
|
|
from_ty: Ty<'tcx>,
|
|
to_ty: Ty<'tcx>,
|
|
arg: &'tcx Expr<'_>,
|
|
const_context: bool,
|
|
) -> bool {
|
|
let mut triggered = false;
|
|
|
|
if let (ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) = (&from_ty.kind(), &to_ty.kind()) {
|
|
if_chain! {
|
|
if let (&ty::Slice(slice_ty), &ty::Str) = (&ty_from.kind(), &ty_to.kind());
|
|
if let ty::Uint(ty::UintTy::U8) = slice_ty.kind();
|
|
if from_mutbl == to_mutbl;
|
|
then {
|
|
let postfix = if *from_mutbl == Mutability::Mut {
|
|
"_mut"
|
|
} else {
|
|
""
|
|
};
|
|
|
|
span_lint_and_sugg(
|
|
cx,
|
|
TRANSMUTE_BYTES_TO_STR,
|
|
e.span,
|
|
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|
|
"consider using",
|
|
format!(
|
|
"std::str::from_utf8{}({}).unwrap()",
|
|
postfix,
|
|
snippet(cx, arg.span, ".."),
|
|
),
|
|
Applicability::Unspecified,
|
|
);
|
|
triggered = true;
|
|
} else {
|
|
if (cx.tcx.erase_regions(from_ty) != cx.tcx.erase_regions(to_ty))
|
|
&& !const_context {
|
|
span_lint_and_then(
|
|
cx,
|
|
TRANSMUTE_PTR_TO_PTR,
|
|
e.span,
|
|
"transmute from a reference to a reference",
|
|
|diag| if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
|
let ty_from_and_mut = ty::TypeAndMut {
|
|
ty: *ty_from,
|
|
mutbl: *from_mutbl
|
|
};
|
|
let ty_to_and_mut = ty::TypeAndMut { ty: *ty_to, mutbl: *to_mutbl };
|
|
let sugg_paren = arg
|
|
.as_ty(cx.tcx.mk_ptr(ty_from_and_mut))
|
|
.as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
|
|
let sugg = if *to_mutbl == Mutability::Mut {
|
|
sugg_paren.mut_addr_deref()
|
|
} else {
|
|
sugg_paren.addr_deref()
|
|
};
|
|
diag.span_suggestion(
|
|
e.span,
|
|
"try",
|
|
sugg.to_string(),
|
|
Applicability::Unspecified,
|
|
);
|
|
},
|
|
);
|
|
|
|
triggered = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
triggered
|
|
}
|