diff options
| author | bors <bors@rust-lang.org> | 2022-03-18 15:54:45 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-03-18 15:54:45 +0000 |
| commit | a31dcb77be146ec04a653cd1e7dee21600b3f29e (patch) | |
| tree | 6094ec5b5366e4f6fb0690380eaa6b55f3735473 /clippy_lints/src/casts/mod.rs | |
| parent | 9fd3c2d7886a61a10c37f922f6e866e221565add (diff) | |
| parent | 39329d1d404d38437270acf3cb1337a231367efc (diff) | |
| download | rust-a31dcb77be146ec04a653cd1e7dee21600b3f29e.tar.gz rust-a31dcb77be146ec04a653cd1e7dee21600b3f29e.zip | |
Auto merge of #8562 - Jarcho:enum_tuple_variant_as_int, r=Manishearth
Add lint `cast_enum_constructor` fixes: #1116 changelog: Add lint `cast_enum_constructor`
Diffstat (limited to 'clippy_lints/src/casts/mod.rs')
| -rw-r--r-- | clippy_lints/src/casts/mod.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index 6a0eabd089d..be59145afa0 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -1,3 +1,4 @@ +mod cast_enum_constructor; mod cast_lossless; mod cast_possible_truncation; mod cast_possible_wrap; @@ -454,6 +455,24 @@ declare_clippy_lint! { "casting using `as` between raw pointers to slices of types with different sizes" } +declare_clippy_lint! { + /// ### What it does + /// Checks for casts from an enum tuple constructor to an integer. + /// + /// ### Why is this bad? + /// The cast is easily confused with casting a c-like enum value to an integer. + /// + /// ### Example + /// ```rust + /// enum E { X(i32) }; + /// let _ = E::X as usize; + /// ``` + #[clippy::version = "1.61.0"] + pub CAST_ENUM_CONSTRUCTOR, + suspicious, + "casts from an enum tuple constructor to an integer" +} + pub struct Casts { msrv: Option<RustcVersion>, } @@ -481,6 +500,7 @@ impl_lint_pass!(Casts => [ CHAR_LIT_AS_U8, PTR_AS_PTR, CAST_ENUM_TRUNCATION, + CAST_ENUM_CONSTRUCTOR ]); impl<'tcx> LateLintPass<'tcx> for Casts { @@ -518,6 +538,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts { cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to); } cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv); + cast_enum_constructor::check(cx, expr, cast_expr, cast_from); } } |
