Skip to content

Commit 8bb8d04

Browse files
committed
use integer_sign_cast
1 parent 79f53d2 commit 8bb8d04

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

compiler/rustc_mir_transform/src/check_redundant_transmutes.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ impl<'a, 'tcx> RedundantTransmutesChecker<'a, 'tcx> {
6262
span,
6363
});
6464
}
65-
return match input.kind() {
65+
match input.kind() {
6666
// char → u32
67-
Char => matches!(fn_sig.output().kind(), Uint(UintTy::U32)).then(|| {
68-
errors::RedundantTransmute { sugg: format!("({arg}) as u32"), help: None, span }
69-
}),
67+
Char => matches!(fn_sig.output().kind(), Uint(UintTy::U32))
68+
.then(|| errors::RedundantTransmute::new(format!("({arg}) as u32"), span)),
7069
// u32 → char
7170
Uint(UintTy::U32) if *fn_sig.output().kind() == Char => {
7271
Some(errors::RedundantTransmute {
@@ -75,19 +74,26 @@ impl<'a, 'tcx> RedundantTransmutesChecker<'a, 'tcx> {
7574
span,
7675
})
7776
}
78-
// bool → (uNN ↔ iNN)
79-
Bool | Uint(..) | Int(..) => {
80-
matches!(fn_sig.output().kind(), Int(..) | Uint(..)).then(|| {
81-
// FIXME: suggest `cast_unsigned`/ `cast_signed` when `integer_sign_cast` stabilizes
82-
errors::RedundantTransmute {
83-
sugg: format!("({arg}) as {}", fn_sig.output()),
84-
help: None,
85-
span,
86-
}
87-
})
77+
// uNN → iNN
78+
Uint(ty) if matches!(fn_sig.output().kind(), Int(..)) => {
79+
Some(errors::RedundantTransmute::new(
80+
format!("{}::cast_signed({arg})", ty.name_str()),
81+
span,
82+
))
8883
}
84+
// iNN → uNN
85+
Int(ty) if matches!(fn_sig.output().kind(), Uint(..)) => {
86+
Some(errors::RedundantTransmute::new(
87+
format!("{}::cast_unsigned({arg})", ty.name_str()),
88+
span,
89+
))
90+
}
91+
// bool → { uNN, iNN }
92+
Bool if matches!(fn_sig.output().kind(), Int(..) | Uint(..)) => Some(
93+
errors::RedundantTransmute::new(format!("({arg}) as {}", fn_sig.output()), span),
94+
),
8995
_ => None,
90-
};
96+
}
9197
}
9298
}
9399

compiler/rustc_mir_transform/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ pub(crate) struct RedundantTransmute {
164164
pub help: Option<&'static str>,
165165
}
166166

167+
impl RedundantTransmute {
168+
pub(crate) fn new(sugg: String, span: Span) -> Self {
169+
Self { span, sugg, help: None }
170+
}
171+
}
172+
167173
// Needed for def_path_str
168174
impl<'a> LintDiagnostic<'a, ()> for RedundantTransmute {
169175
fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {

0 commit comments

Comments
 (0)