Skip to content

use uX::from instead of _ as uX in non - const contexts #140435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions compiler/rustc_mir_transform/src/check_unnecessary_transmutes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
function: &Operand<'tcx>,
arg: String,
span: Span,
is_in_const: bool,
) -> Option<Error> {
let fn_sig = function.ty(self.body, self.tcx).fn_sig(self.tcx).skip_binder();
let [input] = fn_sig.inputs() else { return None };
Expand Down Expand Up @@ -97,8 +98,14 @@ impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> {
)),
// uNN → fNN
(Uint(_), Float(ty)) => err(format!("{}::from_bits({arg})", ty.name_str())),
// bool → { x8 }
(Bool, Int(..) | Uint(..)) => err(format!("({arg}) as {}", fn_sig.output())),
// bool → { x8 } in const context since `From::from` is not const yet
// FIXME: is it possible to know when the parentheses arent necessary?
// FIXME(const_traits): Remove this when From::from is constified?
(Bool, Int(..) | Uint(..)) if is_in_const => {
err(format!("({arg}) as {}", fn_sig.output()))
}
// " using `x8::from`
(Bool, Int(..) | Uint(..)) => err(format!("{}::from({arg})", fn_sig.output())),
_ => return None,
})
}
Expand All @@ -114,7 +121,13 @@ impl<'tcx> Visitor<'tcx> for UnnecessaryTransmuteChecker<'_, 'tcx> {
&& self.tcx.is_intrinsic(func_def_id, sym::transmute)
&& let span = self.body.source_info(location).span
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(arg)
&& let Some(lint) = self.is_unnecessary_transmute(func, snippet, span)
&& let def_id = self.body.source.def_id()
&& let Some(lint) = self.is_unnecessary_transmute(
func,
snippet,
span,
self.tcx.hir_body_const_context(def_id.expect_local()).is_some(),
)
&& let Some(hir_id) = terminator.source_info.scope.lint_root(&self.body.source_scopes)
{
self.tcx.emit_node_span_lint(UNNECESSARY_TRANSMUTES, hir_id, span, lint);
Expand Down
24 changes: 22 additions & 2 deletions tests/ui/transmute/unnecessary-transmutation.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@ pub fn bytes_at_home(x: u32) -> [u8; 4] {
//~^ ERROR
}

pub const fn intinator_const(from: bool) -> u8 {
unsafe { (from) as u8 }
//~^ ERROR
}

pub static X: u8 = unsafe { (true) as u8 };
//~^ ERROR
pub const Y: u8 = unsafe { (true) as u8 };
//~^ ERROR

pub struct Z {}
impl Z {
pub const fn intinator_assoc(x: bool) -> u8 {
unsafe { (x) as u8 }
//~^ ERROR
}
}

fn main() {
const { unsafe { (true) as u8 } };
//~^ ERROR
unsafe {
let x: u16 = u16::from_ne_bytes(*b"01");
//~^ ERROR
Expand Down Expand Up @@ -83,12 +103,12 @@ fn main() {

let z: bool = transmute(1u8);
// clippy
let z: u8 = (z) as u8;
let z: u8 = u8::from(z);
//~^ ERROR

let z: bool = transmute(1i8);
// clippy
let z: i8 = (z) as i8;
let z: i8 = i8::from(z);
//~^ ERROR
}
}
20 changes: 20 additions & 0 deletions tests/ui/transmute/unnecessary-transmutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@ pub fn bytes_at_home(x: u32) -> [u8; 4] {
//~^ ERROR
}

pub const fn intinator_const(from: bool) -> u8 {
unsafe { transmute(from) }
//~^ ERROR
}

pub static X: u8 = unsafe { transmute(true) };
//~^ ERROR
pub const Y: u8 = unsafe { transmute(true) };
//~^ ERROR

pub struct Z {}
impl Z {
pub const fn intinator_assoc(x: bool) -> u8 {
unsafe { transmute(x) }
//~^ ERROR
}
}

fn main() {
const { unsafe { transmute::<_, u8>(true) } };
//~^ ERROR
unsafe {
let x: u16 = transmute(*b"01");
//~^ ERROR
Expand Down
Loading
Loading