-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #10193 - Jarcho:issue_9894, r=Manishearth
Fix suggestion in `transmutes_expressible_as_ptr_casts` when the source type is a borrow. fixes #9894 changelog: `transmutes_expressible_as_ptr_casts`: Fix suggestion when the source type is a borrow.
- Loading branch information
Showing
6 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 37 additions & 21 deletions
58
clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,52 @@ | ||
use super::utils::can_be_expressed_as_pointer_cast; | ||
use super::utils::check_cast; | ||
use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::sugg; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::sugg::Sugg; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::Ty; | ||
use rustc_middle::ty::{cast::CastKind, Ty}; | ||
|
||
/// Checks for `transmutes_expressible_as_ptr_casts` 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>, | ||
from_ty_adjusted: bool, | ||
to_ty: Ty<'tcx>, | ||
arg: &'tcx Expr<'_>, | ||
) -> bool { | ||
if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) { | ||
span_lint_and_then( | ||
cx, | ||
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, | ||
e.span, | ||
&format!("transmute from `{from_ty}` to `{to_ty}` which could be expressed as a pointer cast instead"), | ||
|diag| { | ||
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) { | ||
let sugg = arg.as_ty(to_ty.to_string()).to_string(); | ||
diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable); | ||
} | ||
}, | ||
); | ||
true | ||
} else { | ||
false | ||
} | ||
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast}; | ||
let mut app = Applicability::MachineApplicable; | ||
let sugg = match check_cast(cx, e, from_ty, to_ty) { | ||
Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => { | ||
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app) | ||
.as_ty(to_ty.to_string()) | ||
.to_string() | ||
}, | ||
Some(PtrAddrCast) if !from_ty_adjusted => Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app) | ||
.as_ty(to_ty.to_string()) | ||
.to_string(), | ||
|
||
// The only adjustments here would be ref-to-ptr and unsize coercions. The result of an unsize coercions can't | ||
// be transmuted to a usize. For ref-to-ptr coercions, borrows need to be cast to a pointer before being cast to | ||
// a usize. | ||
Some(PtrAddrCast) => format!( | ||
"{} as {to_ty}", | ||
Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app).as_ty(from_ty) | ||
), | ||
_ => return false, | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, | ||
e.span, | ||
&format!("transmute from `{from_ty}` to `{to_ty}` which could be expressed as a pointer cast instead"), | ||
"try", | ||
sugg, | ||
app, | ||
); | ||
true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters