-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|