Add unnecessary_intermediate_cast lint#16501
Conversation
|
Lintcheck changes for e1c830d
This comment will be updated if you push new changes |
4a7f2b8 to
bf11f00
Compare
This comment has been minimized.
This comment has been minimized.
bf11f00 to
e1c830d
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
☔ The latest upstream changes (possibly #15979) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Do I need to do anything more here (other than rebasing/resolving conflicts)? Not sure if it got missed or whatever. |
|
You have problems with how you're handling pointers and integers. The match (a, b, c) {
(RawPtr(..), RawPtr(..), RawPtr(..)) => {}
(RawPtr(a, _), RawPtr(..), _) if !a.is_sized(cx.tcx, cx.typing_env()) => return,
(RawPtr(..), RawPtr(..), _) => {},
(Bool, Int(_) | Uint(_), Int(_) | Uint(_)) => {},
(Float(a), Float(b), _) if a < b => {},
(Float(a), Float(b), Float(c)) if a > b && b > c => {},
(a, b, c) if let Some((sign1, (_, max1))) = int_parts(a)
&& let Some((sign2, (min2, max2))) = int_parts(b)
&& let Some((_, (min3, _))) = int_parts(c)
&& !matches!(c, Char)
=> match (sign1, sign2) {
(Unsigned, _) if max1 < max2 => {},
(Unsigned, Unsigned) if max1 == max2 => {},
(Signed, Signed) if max1 <= max2 => {},
_ if min2 >= min3 => {}
_ => return,
}
_ => return,
}
enum Signedness { Signed, Unsigned }
#[derive(PartialEq, PartialOrd)]
enum Size { _8, _16, _32, _64, _128 }
fn int_parts(ty: Ty<'_>) -> Option<(Signedness, (Size, Size))> {
match ty.kind {
Int(ty) => (Signed, int_bounds(ty)),
Uint(ty) => (Unsigned, int_bounds(ty)),
RawPtr(..) => (Unsigned, (_16, _64)),
Char => (Unsigned, (_32, _32)),
_ => None,
}
}
fn int_bounds(ty: IntTy) -> (Size, Size) {
match ty {
I8 => (_8, _8),
I16 => (_16, _16),
I32 => (_32, _32),
I64 => (_64, _64),
I128 => (_128, _128),
Isize => (_16, _64),
}
} |
|
@rustbot label lint-nominated |
|
This lint has been nominated for inclusion. |
This adds a lint to check for two casts in a row. It lints if the first cast is redundant, and the same effect could be achieved by casting straight to the final type. It's probably not so likely to find this in human-written code, but can be useful for cleaning up automatically generated code before further human consumption, like for example in transpiling software.
This is my first lint PR, so please let me know if I missed anything. In particular, I'm not entirely sure if it will interfere with other casting lints (like
unnecessary_cast), nor how to remedy it in case it does.changelog: new lint: [
unnecessary_intermediate_cast]