Skip to content

Commit cfdef27

Browse files
committed
Address review comments
1 parent bb0ce32 commit cfdef27

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

clippy_lints/src/types.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::borrow::Cow;
44
use std::cmp::Ordering;
55
use std::collections::BTreeMap;
6-
use std::fmt::Display;
76

87
use if_chain::if_chain;
98
use rustc_ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy};
@@ -13,7 +12,7 @@ use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisitor
1312
use rustc_hir::{
1413
BinOpKind, Block, Body, Expr, ExprKind, FnDecl, FnRetTy, FnSig, GenericArg, GenericParamKind, HirId, ImplItem,
1514
ImplItemKind, Item, ItemKind, Lifetime, Local, MatchSource, MutTy, Mutability, Node, QPath, Stmt, StmtKind,
16-
TraitFn, TraitItem, TraitItemKind, TyKind, UnOp,
15+
TraitFn, TraitItem, TraitItemKind, TyKind, UnOp, Lit,
1716
};
1817
use rustc_lint::{LateContext, LateLintPass, LintContext};
1918
use rustc_middle::hir::map::Map;
@@ -1225,7 +1224,8 @@ declare_clippy_lint! {
12251224
}
12261225

12271226
declare_clippy_lint! {
1228-
/// **What it does:** Checks for casts to the same type.
1227+
/// **What it does:** Checks for casts to the same type, casts of int literals to integer types
1228+
/// and casts of float literals to float types.
12291229
///
12301230
/// **Why is this bad?** It's just unnecessary.
12311231
///
@@ -1234,6 +1234,7 @@ declare_clippy_lint! {
12341234
/// **Example:**
12351235
/// ```rust
12361236
/// let _ = 2i32 as i32;
1237+
/// let _ = 0.5 as f32;
12371238
/// ```
12381239
pub UNNECESSARY_CAST,
12391240
complexity,
@@ -1599,7 +1600,9 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
15991600
if let ExprKind::Cast(ref ex, _) = expr.kind {
16001601
let (cast_from, cast_to) = (cx.typeck_results().expr_ty(ex), cx.typeck_results().expr_ty(expr));
16011602
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
1602-
if let ExprKind::Lit(ref lit) = ex.kind {
1603+
if let Some(lit) = get_numeric_literal(ex) {
1604+
let literal_str = snippet_opt(cx, lit.span).unwrap_or_default();
1605+
16031606
if_chain! {
16041607
if let LitKind::Int(n, _) = lit.node;
16051608
if let Some(src) = snippet_opt(cx, lit.span);
@@ -1609,25 +1612,19 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16091612
let to_nbits = fp_ty_mantissa_nbits(cast_to);
16101613
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
16111614
then {
1612-
show_unnecessary_cast(cx, expr, n , cast_from, cast_to);
1615+
show_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
16131616
return;
16141617
}
16151618
}
16161619

16171620
match lit.node {
1618-
LitKind::Int(num, LitIntType::Unsuffixed) if cast_to.is_integral() => {
1619-
show_unnecessary_cast(cx, expr, num, cast_from, cast_to);
1620-
return;
1621+
LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => {
1622+
show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
16211623
},
1622-
LitKind::Float(num, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
1623-
show_unnecessary_cast(cx, expr, num, cast_from, cast_to);
1624-
return;
1624+
LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
1625+
show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
16251626
},
1626-
_ => (),
1627-
};
1628-
1629-
match lit.node {
1630-
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
1627+
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => (),
16311628
_ => {
16321629
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
16331630
span_lint(
@@ -1640,7 +1637,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16401637
),
16411638
);
16421639
}
1643-
},
1640+
}
16441641
}
16451642
}
16461643
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
@@ -1652,10 +1649,24 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16521649
}
16531650
}
16541651

1655-
fn show_unnecessary_cast<Num: Display>(
1652+
fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
1653+
match expr.kind {
1654+
ExprKind::Lit(ref lit) => Some(lit),
1655+
ExprKind::Unary(UnOp::UnNeg, e) => {
1656+
if let ExprKind::Lit(ref lit) = e.kind {
1657+
Some(lit)
1658+
} else {
1659+
None
1660+
}
1661+
},
1662+
_ => None,
1663+
}
1664+
}
1665+
1666+
fn show_unnecessary_cast(
16561667
cx: &LateContext<'_>,
16571668
expr: &Expr<'_>,
1658-
num: Num,
1669+
literal_str: &str,
16591670
cast_from: Ty<'_>,
16601671
cast_to: Ty<'_>,
16611672
) {
@@ -1666,7 +1677,7 @@ fn show_unnecessary_cast<Num: Display>(
16661677
expr.span,
16671678
&format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to),
16681679
"try",
1669-
format!("{}_{}", num, cast_to),
1680+
format!("{}_{}", literal_str, cast_to),
16701681
Applicability::MachineApplicable,
16711682
);
16721683
}

tests/ui/unnecessary_cast_fixable.fixed

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ fn main() {
2020
0b11 as f64;
2121

2222
1_u32;
23-
16_i32;
24-
2_usize;
23+
0x10_i32;
24+
0b10_usize;
25+
0o73_u16;
26+
1_000_000_000_u32;
2527

2628
1.0_f64;
2729
0.5_f32;

tests/ui/unnecessary_cast_fixable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fn main() {
2222
1 as u32;
2323
0x10 as i32;
2424
0b10 as usize;
25+
0o73 as u16;
26+
1_000_000_000 as u32;
2527

2628
1.0 as f64;
2729
0.5 as f32;

tests/ui/unnecessary_cast_fixable.stderr

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,37 @@ error: casting integer literal to `i32` is unnecessary
2828
--> $DIR/unnecessary_cast_fixable.rs:23:5
2929
|
3030
LL | 0x10 as i32;
31-
| ^^^^^^^^^^^ help: try: `16_i32`
31+
| ^^^^^^^^^^^ help: try: `0x10_i32`
3232

3333
error: casting integer literal to `usize` is unnecessary
3434
--> $DIR/unnecessary_cast_fixable.rs:24:5
3535
|
3636
LL | 0b10 as usize;
37-
| ^^^^^^^^^^^^^ help: try: `2_usize`
37+
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
3838

39-
error: casting float literal to `f64` is unnecessary
39+
error: casting integer literal to `u16` is unnecessary
40+
--> $DIR/unnecessary_cast_fixable.rs:25:5
41+
|
42+
LL | 0o73 as u16;
43+
| ^^^^^^^^^^^ help: try: `0o73_u16`
44+
45+
error: casting integer literal to `u32` is unnecessary
4046
--> $DIR/unnecessary_cast_fixable.rs:26:5
4147
|
48+
LL | 1_000_000_000 as u32;
49+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
50+
51+
error: casting float literal to `f64` is unnecessary
52+
--> $DIR/unnecessary_cast_fixable.rs:28:5
53+
|
4254
LL | 1.0 as f64;
4355
| ^^^^^^^^^^ help: try: `1.0_f64`
4456

4557
error: casting float literal to `f32` is unnecessary
46-
--> $DIR/unnecessary_cast_fixable.rs:27:5
58+
--> $DIR/unnecessary_cast_fixable.rs:29:5
4759
|
4860
LL | 0.5 as f32;
4961
| ^^^^^^^^^^ help: try: `0.5_f32`
5062

51-
error: aborting due to 8 previous errors
63+
error: aborting due to 10 previous errors
5264

0 commit comments

Comments
 (0)