And the tools too
This commit is contained in:
parent
7be0dbe772
commit
6a40dabff9
20 changed files with 44 additions and 71 deletions
|
@ -4,18 +4,13 @@ use rustc_errors::Applicability;
|
|||
use rustc_hir::{Expr, ExprKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::mir::Mutability;
|
||||
use rustc_middle::ty::{self, Ty, TypeAndMut};
|
||||
use rustc_middle::ty::{self, Ty};
|
||||
|
||||
use super::AS_PTR_CAST_MUT;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>) {
|
||||
if let ty::RawPtr(TypeAndMut {
|
||||
mutbl: Mutability::Mut,
|
||||
ty: ptrty,
|
||||
}) = cast_to.kind()
|
||||
&& let ty::RawPtr(TypeAndMut {
|
||||
mutbl: Mutability::Not, ..
|
||||
}) = cx.typeck_results().node_type(cast_expr.hir_id).kind()
|
||||
if let ty::RawPtr(ptrty, Mutability::Mut) = cast_to.kind()
|
||||
&& let ty::RawPtr(_, Mutability::Not) = cx.typeck_results().node_type(cast_expr.hir_id).kind()
|
||||
&& let ExprKind::MethodCall(method_name, receiver, [], _) = cast_expr.peel_blocks().kind
|
||||
&& method_name.ident.name == rustc_span::sym::as_ptr
|
||||
&& let Some(as_ptr_did) = cx
|
||||
|
|
|
@ -33,13 +33,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||
}
|
||||
|
||||
fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) {
|
||||
if let ty::RawPtr(from_ptr_ty) = &cast_from.kind()
|
||||
&& let ty::RawPtr(to_ptr_ty) = &cast_to.kind()
|
||||
&& let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty)
|
||||
&& let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty)
|
||||
if let ty::RawPtr(from_ptr_ty, _) = *cast_from.kind()
|
||||
&& let ty::RawPtr(to_ptr_ty, _) = *cast_to.kind()
|
||||
&& let Ok(from_layout) = cx.layout_of(from_ptr_ty)
|
||||
&& let Ok(to_layout) = cx.layout_of(to_ptr_ty)
|
||||
&& from_layout.align.abi < to_layout.align.abi
|
||||
// with c_void, we inherently need to trust the user
|
||||
&& !is_c_void(cx, from_ptr_ty.ty)
|
||||
&& !is_c_void(cx, from_ptr_ty)
|
||||
// when casting from a ZST, we don't know enough to properly lint
|
||||
&& !from_layout.is_zst()
|
||||
&& !is_used_as_unaligned(cx, expr)
|
||||
|
|
|
@ -25,8 +25,8 @@ fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option<RawPartsKind> {
|
|||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: &Msrv) {
|
||||
if msrv.meets(msrvs::PTR_SLICE_RAW_PARTS)
|
||||
&& let ty::RawPtr(ptrty) = cast_to.kind()
|
||||
&& let ty::Slice(_) = ptrty.ty.kind()
|
||||
&& let ty::RawPtr(ptrty, _) = cast_to.kind()
|
||||
&& let ty::Slice(_) = ptrty.kind()
|
||||
&& let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind
|
||||
&& let ExprKind::Path(ref qpath) = fun.kind
|
||||
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
|
||||
|
|
|
@ -6,7 +6,7 @@ use rustc_errors::Applicability;
|
|||
use rustc_hir::{Expr, ExprKind, Mutability, QPath, TyKind};
|
||||
use rustc_hir_pretty::qpath_to_string;
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, TypeAndMut};
|
||||
use rustc_middle::ty;
|
||||
use rustc_span::sym;
|
||||
|
||||
use super::PTR_AS_PTR;
|
||||
|
|
|
@ -4,7 +4,7 @@ use clippy_utils::sugg::Sugg;
|
|||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, Mutability};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, Ty, TypeAndMut};
|
||||
use rustc_middle::ty::{self, Ty};
|
||||
|
||||
use super::PTR_CAST_CONSTNESS;
|
||||
|
||||
|
@ -17,14 +17,8 @@ pub(super) fn check<'tcx>(
|
|||
msrv: &Msrv,
|
||||
) {
|
||||
if msrv.meets(msrvs::POINTER_CAST_CONSTNESS)
|
||||
&& let ty::RawPtr(TypeAndMut {
|
||||
mutbl: from_mutbl,
|
||||
ty: from_ty,
|
||||
}) = cast_from.kind()
|
||||
&& let ty::RawPtr(TypeAndMut {
|
||||
mutbl: to_mutbl,
|
||||
ty: to_ty,
|
||||
}) = cast_to.kind()
|
||||
&& let ty::RawPtr(from_ty, from_mutbl) = cast_from.kind()
|
||||
&& let ty::RawPtr(to_ty, to_mutbl) = cast_to.kind()
|
||||
&& matches!(
|
||||
(from_mutbl, to_mutbl),
|
||||
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)
|
||||
|
|
|
@ -5,7 +5,7 @@ use clippy_utils::{expr_use_ctxt, is_no_std_crate, ExprUseNode};
|
|||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, Mutability, Ty, TyKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, TypeAndMut};
|
||||
use rustc_middle::ty;
|
||||
|
||||
use super::REF_AS_PTR;
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ use clippy_utils::ty::is_c_void;
|
|||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::{Expr, ExprKind, QPath};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{RawPtr, TypeAndMut};
|
||||
use rustc_middle::ty::{RawPtr};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::sym;
|
||||
|
||||
|
@ -44,7 +44,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
|
|||
&& seg.ident.name == sym!(from_raw)
|
||||
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
|
||||
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
|
||||
&& let ty::RawPtr(ty, _) = arg_kind
|
||||
&& let RawPtr(ty, _) = arg_kind
|
||||
&& is_c_void(cx, *ty)
|
||||
{
|
||||
let msg = format!("creating a `{type_str}` from a void raw pointer");
|
||||
|
|
|
@ -10,7 +10,7 @@ use rustc_errors::Applicability;
|
|||
use rustc_hir::{Expr, Mutability};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
|
||||
use rustc_middle::ty::{self, EarlyBinder, Ty, TypeAndMut};
|
||||
use rustc_middle::ty::{self, EarlyBinder, Ty};
|
||||
use rustc_span::sym;
|
||||
|
||||
pub(super) fn check(
|
||||
|
@ -160,7 +160,7 @@ fn is_ref_iterable<'tcx>(
|
|||
let self_ty = if mutbl.is_mut() {
|
||||
self_ty
|
||||
} else {
|
||||
Ty::new_ref(cx.tcx, region, TypeAndMut { ty, mutbl })
|
||||
Ty::new_ref(cx.tcx, region, ty, mutbl)
|
||||
};
|
||||
if implements_trait(cx, self_ty, trait_id, &[])
|
||||
&& let Some(ty) =
|
||||
|
@ -175,7 +175,7 @@ fn is_ref_iterable<'tcx>(
|
|||
&& !self_ty.is_ref()
|
||||
{
|
||||
// Attempt to borrow
|
||||
let self_ty = Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, TypeAndMut { ty: self_ty, mutbl });
|
||||
let self_ty = Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, self_ty, mutbl);
|
||||
if implements_trait(cx, self_ty, trait_id, &[])
|
||||
&& let Some(ty) = make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
|
||||
&& ty == res_ty
|
||||
|
|
|
@ -84,9 +84,7 @@ fn check_arguments<'tcx>(
|
|||
for (argument, parameter) in iter::zip(arguments, parameters) {
|
||||
match parameter.kind() {
|
||||
ty::Ref(_, _, Mutability::Not)
|
||||
| ty::RawPtr(ty::TypeAndMut {
|
||||
mutbl: Mutability::Not, ..
|
||||
}) => {
|
||||
| ty::RawPtr(_, Mutability::Not) => {
|
||||
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
|
||||
span_lint(
|
||||
cx,
|
||||
|
|
|
@ -7,7 +7,7 @@ use rustc_hir::def::{DefKind, Res};
|
|||
use rustc_hir::intravisit::{walk_expr, Visitor};
|
||||
use rustc_hir::{self as hir};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::ty::{GenericArgKind, Ty, TypeAndMut};
|
||||
use rustc_middle::ty::{GenericArgKind, Ty};
|
||||
use rustc_session::impl_lint_pass;
|
||||
use rustc_span::symbol::Ident;
|
||||
use rustc_span::{sym, Span, DUMMY_SP};
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{self, Ty, TypeAndMut};
|
||||
use rustc_middle::ty::{self, Ty};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::sym;
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ use rustc_middle::ty::{self, Ty};
|
|||
/// Checks for `crosspointer_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>) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
|
||||
match (*from_ty.kind(), *to_ty.kind()) {
|
||||
(ty::RawPtr(from_ptr_ty, _), _) if from_ptr_ty == to_ty => {
|
||||
span_lint(
|
||||
cx,
|
||||
CROSSPOINTER_TRANSMUTE,
|
||||
|
@ -17,7 +17,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty
|
|||
);
|
||||
true
|
||||
},
|
||||
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
|
||||
(_, ty::RawPtr(to_ptr_ty, _)) if to_ptr_ty == from_ty => {
|
||||
span_lint(
|
||||
cx,
|
||||
CROSSPOINTER_TRANSMUTE,
|
||||
|
|
|
@ -16,7 +16,7 @@ pub(super) fn check<'tcx>(
|
|||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::RawPtr(_, _), ty::RawPtr(to_ty)) => {
|
||||
(ty::RawPtr(_, _), ty::RawPtr(to_ty, to_mutbl)) => {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
TRANSMUTE_PTR_TO_PTR,
|
||||
|
@ -24,7 +24,7 @@ pub(super) fn check<'tcx>(
|
|||
"transmute from a pointer to a pointer",
|
||||
|diag| {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
let sugg = arg.as_ty(Ty::new_ptr(cx.tcx, *to_ty));
|
||||
let sugg = arg.as_ty(Ty::new_ptr(cx.tcx, *to_ty, *to_mutbl));
|
||||
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
|
||||
}
|
||||
},
|
||||
|
|
|
@ -20,7 +20,7 @@ pub(super) fn check<'tcx>(
|
|||
msrv: &Msrv,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
(ty::RawPtr(from_ptr_ty), ty::Ref(_, to_ref_ty, mutbl)) => {
|
||||
(ty::RawPtr(from_ptr_ty, _), ty::Ref(_, to_ref_ty, mutbl)) => {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
TRANSMUTE_PTR_TO_REF,
|
||||
|
@ -44,7 +44,7 @@ pub(super) fn check<'tcx>(
|
|||
} else {
|
||||
sugg::make_unop(deref, arg.as_ty(format!("{cast} {ty_snip}"))).to_string()
|
||||
}
|
||||
} else if from_ptr_ty.ty == *to_ref_ty {
|
||||
} else if *from_ptr_ty == *to_ref_ty {
|
||||
if from_ptr_ty.has_erased_regions() {
|
||||
if msrv.meets(msrvs::POINTER_CAST) {
|
||||
format!("{deref}{}.cast::<{to_ref_ty}>()", arg.maybe_par())
|
||||
|
|
|
@ -19,7 +19,7 @@ pub(super) fn check<'tcx>(
|
|||
) -> 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 let (ty::Ref(_, ty_from, from_mutbl), ty::Ref(_, ty_to, to_mutbl)) = (*from_ty.kind(), *to_ty.kind()) {
|
||||
if let ty::Slice(slice_ty) = *ty_from.kind()
|
||||
&& ty_to.is_str()
|
||||
&& let ty::Uint(ty::UintTy::U8) = slice_ty.kind()
|
||||
|
@ -27,7 +27,7 @@ pub(super) fn check<'tcx>(
|
|||
{
|
||||
let Some(top_crate) = std_or_core(cx) else { return true };
|
||||
|
||||
let postfix = if *from_mutbl == Mutability::Mut { "_mut" } else { "" };
|
||||
let postfix = if from_mutbl == Mutability::Mut { "_mut" } else { "" };
|
||||
|
||||
let snippet = snippet(cx, arg.span, "..");
|
||||
|
||||
|
@ -53,18 +53,10 @@ pub(super) fn check<'tcx>(
|
|||
"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(Ty::new_ptr(cx.tcx, ty_from_and_mut))
|
||||
.as_ty(Ty::new_ptr(cx.tcx, ty_to_and_mut));
|
||||
let sugg = if *to_mutbl == Mutability::Mut {
|
||||
.as_ty(Ty::new_ptr(cx.tcx, ty_from, from_mutbl))
|
||||
.as_ty(Ty::new_ptr(cx.tcx, ty_to, to_mutbl));
|
||||
let sugg = if to_mutbl == Mutability::Mut {
|
||||
sugg_paren.mut_addr_deref()
|
||||
} else {
|
||||
sugg_paren.addr_deref()
|
||||
|
|
|
@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
|
|||
use clippy_utils::ty::is_c_void;
|
||||
use rustc_hir::Expr;
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, GenericArgsRef, IntTy, Ty, TypeAndMut, UintTy};
|
||||
use rustc_middle::ty::{self, GenericArgsRef, IntTy, Ty, UintTy};
|
||||
|
||||
#[expect(clippy::too_many_lines)]
|
||||
pub(super) fn check<'tcx>(
|
||||
|
|
|
@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(
|
|||
to_ty: Ty<'tcx>,
|
||||
arg: &'tcx Expr<'_>,
|
||||
) -> bool {
|
||||
match (&from_ty.kind(), &to_ty.kind()) {
|
||||
match (*from_ty.kind(), *to_ty.kind()) {
|
||||
_ if from_ty == to_ty && !from_ty.has_erased_regions() => {
|
||||
span_lint(
|
||||
cx,
|
||||
|
@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
|
|||
);
|
||||
true
|
||||
},
|
||||
(ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty)) => {
|
||||
(ty::Ref(_, rty, rty_mutbl), ty::RawPtr(ptr_ty, ptr_mutbl)) => {
|
||||
// No way to give the correct suggestion here. Avoid linting for now.
|
||||
if !rty.has_erased_regions() {
|
||||
span_lint_and_then(
|
||||
|
@ -35,15 +35,10 @@ pub(super) fn check<'tcx>(
|
|||
"transmute from a reference to a pointer",
|
||||
|diag| {
|
||||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
|
||||
let rty_and_mut = ty::TypeAndMut {
|
||||
ty: *rty,
|
||||
mutbl: *rty_mutbl,
|
||||
};
|
||||
|
||||
let sugg = if *ptr_ty == rty_and_mut {
|
||||
let sugg = if ptr_ty == rty && rty_mutbl == ptr_mutbl {
|
||||
arg.as_ty(to_ty)
|
||||
} else {
|
||||
arg.as_ty(Ty::new_ptr(cx.tcx, rty_and_mut)).as_ty(to_ty)
|
||||
arg.as_ty(Ty::new_ptr(cx.tcx, rty, rty_mutbl)).as_ty(to_ty)
|
||||
};
|
||||
|
||||
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
|
||||
|
|
|
@ -112,7 +112,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType;
|
|||
use rustc_middle::ty::layout::IntegerExt;
|
||||
use rustc_middle::ty::{
|
||||
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgsRef, IntTy, ParamEnv,
|
||||
ParamEnvAnd, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, UintTy, UpvarCapture,
|
||||
ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, UintTy, UpvarCapture,
|
||||
};
|
||||
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
||||
use rustc_span::source_map::SourceMap;
|
||||
|
|
|
@ -90,7 +90,7 @@ impl NewPermission {
|
|||
}
|
||||
}
|
||||
}
|
||||
ty::RawPtr(ty::TypeAndMut { mutbl: Mutability::Mut, .. }) => {
|
||||
ty::RawPtr(_, Mutability::Mut) => {
|
||||
assert!(protector.is_none()); // RetagKind can't be both FnEntry and Raw.
|
||||
// Mutable raw pointer. No access, not protected.
|
||||
NewPermission::Uniform {
|
||||
|
@ -114,7 +114,7 @@ impl NewPermission {
|
|||
// This fixes https://github.com/rust-lang/rust/issues/55005.
|
||||
}
|
||||
}
|
||||
ty::RawPtr(ty::TypeAndMut { mutbl: Mutability::Not, .. }) => {
|
||||
ty::RawPtr(_, Mutability::Not) => {
|
||||
assert!(protector.is_none()); // RetagKind can't be both FnEntry and Raw.
|
||||
// `*const T`, when freshly created, are read-only in the frozen part.
|
||||
NewPermission::FreezeSensitive {
|
||||
|
|
|
@ -12,7 +12,6 @@ use rand::rngs::StdRng;
|
|||
use rand::Rng;
|
||||
use rand::SeedableRng;
|
||||
|
||||
use rustc_ast::ast::Mutability;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||
#[allow(unused)]
|
||||
use rustc_data_structures::static_assert_size;
|
||||
|
@ -22,7 +21,7 @@ use rustc_middle::{
|
|||
ty::{
|
||||
self,
|
||||
layout::{LayoutCx, LayoutError, LayoutOf, TyAndLayout},
|
||||
Instance, Ty, TyCtxt, TypeAndMut,
|
||||
Instance, Ty, TyCtxt,
|
||||
},
|
||||
};
|
||||
use rustc_span::def_id::{CrateNum, DefId};
|
||||
|
|
Loading…
Add table
Reference in a new issue