mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Add lint cast_enum_constructor
This commit is contained in:
parent
65e5cd0e95
commit
39329d1d40
8 changed files with 79 additions and 0 deletions
|
@ -3069,6 +3069,7 @@ Released 2018-09-13
|
|||
[`bytes_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_nth
|
||||
[`cargo_common_metadata`]: https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata
|
||||
[`case_sensitive_file_extension_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#case_sensitive_file_extension_comparisons
|
||||
[`cast_enum_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_constructor
|
||||
[`cast_enum_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_truncation
|
||||
[`cast_lossless`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
|
||||
[`cast_possible_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
|
||||
|
|
21
clippy_lints/src/casts/cast_enum_constructor.rs
Normal file
21
clippy_lints/src/casts/cast_enum_constructor.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use clippy_utils::diagnostics::span_lint;
|
||||
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
|
||||
use rustc_hir::{Expr, ExprKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, Ty};
|
||||
|
||||
use super::CAST_ENUM_CONSTRUCTOR;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>) {
|
||||
if matches!(cast_from.kind(), ty::FnDef(..))
|
||||
&& let ExprKind::Path(path) = &cast_expr.kind
|
||||
&& let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), _) = cx.qpath_res(path, cast_expr.hir_id)
|
||||
{
|
||||
span_lint(
|
||||
cx,
|
||||
CAST_ENUM_CONSTRUCTOR,
|
||||
expr.span,
|
||||
"cast of an enum tuple constructor to an integer",
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
|
|||
LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON),
|
||||
LintId::of(booleans::LOGIC_BUG),
|
||||
LintId::of(booleans::NONMINIMAL_BOOL),
|
||||
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
|
||||
LintId::of(casts::CAST_ENUM_TRUNCATION),
|
||||
LintId::of(casts::CAST_REF_TO_MUT),
|
||||
LintId::of(casts::CAST_SLICE_DIFFERENT_SIZES),
|
||||
|
|
|
@ -70,6 +70,7 @@ store.register_lints(&[
|
|||
cargo::REDUNDANT_FEATURE_NAMES,
|
||||
cargo::WILDCARD_DEPENDENCIES,
|
||||
case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
|
||||
casts::CAST_ENUM_CONSTRUCTOR,
|
||||
casts::CAST_ENUM_TRUNCATION,
|
||||
casts::CAST_LOSSLESS,
|
||||
casts::CAST_POSSIBLE_TRUNCATION,
|
||||
|
|
|
@ -7,6 +7,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
|
|||
LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS),
|
||||
LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK),
|
||||
LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
|
||||
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
|
||||
LintId::of(casts::CAST_ENUM_TRUNCATION),
|
||||
LintId::of(eval_order_dependence::EVAL_ORDER_DEPENDENCE),
|
||||
LintId::of(float_equality_without_abs::FLOAT_EQUALITY_WITHOUT_ABS),
|
||||
|
|
17
tests/ui/cast_enum_constructor.rs
Normal file
17
tests/ui/cast_enum_constructor.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
#![warn(clippy::cast_enum_constructor)]
|
||||
#![allow(clippy::fn_to_numeric_cast)]
|
||||
|
||||
fn main() {
|
||||
enum Foo {
|
||||
Y(u32),
|
||||
}
|
||||
|
||||
enum Bar {
|
||||
X,
|
||||
}
|
||||
|
||||
let _ = Foo::Y as usize;
|
||||
let _ = Foo::Y as isize;
|
||||
let _ = Foo::Y as fn(u32) -> Foo;
|
||||
let _ = Bar::X as usize;
|
||||
}
|
16
tests/ui/cast_enum_constructor.stderr
Normal file
16
tests/ui/cast_enum_constructor.stderr
Normal file
|
@ -0,0 +1,16 @@
|
|||
error: cast of an enum tuple constructor to an integer
|
||||
--> $DIR/cast_enum_constructor.rs:13:13
|
||||
|
|
||||
LL | let _ = Foo::Y as usize;
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::cast-enum-constructor` implied by `-D warnings`
|
||||
|
||||
error: cast of an enum tuple constructor to an integer
|
||||
--> $DIR/cast_enum_constructor.rs:14:13
|
||||
|
|
||||
LL | let _ = Foo::Y as isize;
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
Loading…
Reference in a new issue