Add lint for unnecessary casts (cast to same type)

This commit is contained in:
Bood Qian 2017-02-15 22:20:20 +08:00
parent c76517d6c4
commit afce85e96d
6 changed files with 71 additions and 2 deletions

View File

@ -293,6 +293,7 @@ All notable changes to this project will be documented in this file.
[`cast_possible_wrap`]: https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap [`cast_possible_wrap`]: https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap
[`cast_precision_loss`]: https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss [`cast_precision_loss`]: https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss
[`cast_sign_loss`]: https://github.com/Manishearth/rust-clippy/wiki#cast_sign_loss [`cast_sign_loss`]: https://github.com/Manishearth/rust-clippy/wiki#cast_sign_loss
[`cast_unnecessary`]: https://github.com/Manishearth/rust-clippy/wiki#cast_unnecessary
[`char_lit_as_u8`]: https://github.com/Manishearth/rust-clippy/wiki#char_lit_as_u8 [`char_lit_as_u8`]: https://github.com/Manishearth/rust-clippy/wiki#char_lit_as_u8
[`chars_next_cmp`]: https://github.com/Manishearth/rust-clippy/wiki#chars_next_cmp [`chars_next_cmp`]: https://github.com/Manishearth/rust-clippy/wiki#chars_next_cmp
[`clone_double_ref`]: https://github.com/Manishearth/rust-clippy/wiki#clone_double_ref [`clone_double_ref`]: https://github.com/Manishearth/rust-clippy/wiki#clone_double_ref

View File

@ -180,7 +180,7 @@ transparently:
## Lints ## Lints
There are 189 lints included in this crate: There are 190 lints included in this crate:
name | default | triggers on name | default | triggers on
-----------------------------------------------------------------------------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@ -201,6 +201,7 @@ name
[cast_possible_wrap](https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap) | allow | casts that may cause wrapping around the value, e.g `x as i32` where `x: u32` and `x > i32::MAX` [cast_possible_wrap](https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap) | allow | casts that may cause wrapping around the value, e.g `x as i32` where `x: u32` and `x > i32::MAX`
[cast_precision_loss](https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss) | allow | casts that cause loss of precision, e.g `x as f32` where `x: u64` [cast_precision_loss](https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss) | allow | casts that cause loss of precision, e.g `x as f32` where `x: u64`
[cast_sign_loss](https://github.com/Manishearth/rust-clippy/wiki#cast_sign_loss) | allow | casts from signed types to unsigned types, e.g `x as u32` where `x: i32` [cast_sign_loss](https://github.com/Manishearth/rust-clippy/wiki#cast_sign_loss) | allow | casts from signed types to unsigned types, e.g `x as u32` where `x: i32`
[cast_unnecessary](https://github.com/Manishearth/rust-clippy/wiki#cast_unnecessary) | warn | cast to the same type, e.g `x as i32` where `x: i32`
[char_lit_as_u8](https://github.com/Manishearth/rust-clippy/wiki#char_lit_as_u8) | warn | casting a character literal to u8 [char_lit_as_u8](https://github.com/Manishearth/rust-clippy/wiki#char_lit_as_u8) | warn | casting a character literal to u8
[chars_next_cmp](https://github.com/Manishearth/rust-clippy/wiki#chars_next_cmp) | warn | using `.chars().next()` to check if a string starts with a char [chars_next_cmp](https://github.com/Manishearth/rust-clippy/wiki#chars_next_cmp) | warn | using `.chars().next()` to check if a string starts with a char
[clone_double_ref](https://github.com/Manishearth/rust-clippy/wiki#clone_double_ref) | warn | using `clone` on `&&T` [clone_double_ref](https://github.com/Manishearth/rust-clippy/wiki#clone_double_ref) | warn | using `clone` on `&&T`

View File

@ -488,6 +488,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
transmute::WRONG_TRANSMUTE, transmute::WRONG_TRANSMUTE,
types::ABSURD_EXTREME_COMPARISONS, types::ABSURD_EXTREME_COMPARISONS,
types::BOX_VEC, types::BOX_VEC,
types::CAST_UNNECESSARY,
types::CHAR_LIT_AS_U8, types::CHAR_LIT_AS_U8,
types::LET_UNIT_VALUE, types::LET_UNIT_VALUE,
types::LINKEDLIST, types::LINKEDLIST,

View File

@ -379,6 +379,22 @@ declare_lint! {
and `x > i32::MAX`" and `x > i32::MAX`"
} }
/// **What it does:** Checks for casts to the same type
///
/// **Why is this bad?** It's just unnecessary
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// let _ = 2i32 as i32
/// ```
declare_lint! {
pub CAST_UNNECESSARY,
Warn,
"cast to the same type, e.g `x as i32` where `x: i32`"
}
/// Returns the size in bits of an integral type. /// Returns the size in bits of an integral type.
/// Will return 0 if the type is not an int or uint variant /// Will return 0 if the type is not an int or uint variant
fn int_ty_to_nbits(typ: &ty::TyS) -> usize { fn int_ty_to_nbits(typ: &ty::TyS) -> usize {
@ -503,7 +519,8 @@ impl LintPass for CastPass {
lint_array!(CAST_PRECISION_LOSS, lint_array!(CAST_PRECISION_LOSS,
CAST_SIGN_LOSS, CAST_SIGN_LOSS,
CAST_POSSIBLE_TRUNCATION, CAST_POSSIBLE_TRUNCATION,
CAST_POSSIBLE_WRAP) CAST_POSSIBLE_WRAP,
CAST_UNNECESSARY)
} }
} }
@ -511,6 +528,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprCast(ref ex, _) = expr.node { if let ExprCast(ref ex, _) = expr.node {
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr)); let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
if cast_from.sty == cast_to.sty && !in_external_macro(cx, expr.span) {
span_lint(cx,
CAST_UNNECESSARY,
expr.span,
&format!("casting to the same type is unnecessary ({} -> {})", cast_from, cast_to));
}
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx, expr.span) { if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx, expr.span) {
match (cast_from.is_integral(), cast_to.is_integral()) { match (cast_from.is_integral(), cast_to.is_integral()) {
(true, false) => { (true, false) => {

View File

@ -61,4 +61,15 @@ fn main() {
1u32 as usize; // Should not trigger any lint 1u32 as usize; // Should not trigger any lint
1i32 as isize; // Neither should this 1i32 as isize; // Neither should this
1i32 as usize; 1i32 as usize;
// Test cast_unnecessary
1i32 as i32;
1f32 as f32;
false as bool;
&1i32 as &i32;
1i32 as i64; // Should not trigger
let v = vec!(1);
&v as &[i32]; // Should not trigger
} }

View File

@ -274,5 +274,37 @@ error: casting i32 to usize may lose the sign of the value
63 | 1i32 as usize; 63 | 1i32 as usize;
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
warning: casting to the same type is unnecessary (i32 -> i32)
--> $DIR/cast.rs:66:5
|
66 | 1i32 as i32;
| ^^^^^^^^^^^
|
= note: #[warn(cast_unnecessary)] on by default
warning: casting to the same type is unnecessary (f32 -> f32)
--> $DIR/cast.rs:67:5
|
67 | 1f32 as f32;
| ^^^^^^^^^^^
|
= note: #[warn(cast_unnecessary)] on by default
warning: casting to the same type is unnecessary (bool -> bool)
--> $DIR/cast.rs:68:5
|
68 | false as bool;
| ^^^^^^^^^^^^^
|
= note: #[warn(cast_unnecessary)] on by default
warning: casting to the same type is unnecessary (&i32 -> &i32)
--> $DIR/cast.rs:69:5
|
69 | &1i32 as &i32;
| ^^^^^^^^^^^^^
|
= note: #[warn(cast_unnecessary)] on by default
error: aborting due to 42 previous errors error: aborting due to 42 previous errors