Skip to content

Commit 02f0110

Browse files
committed
Add test case for negative literals
1 parent e8f12d2 commit 02f0110

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

clippy_lints/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16011601
let (cast_from, cast_to) = (cx.typeck_results().expr_ty(ex), cx.typeck_results().expr_ty(expr));
16021602
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
16031603
if let Some(lit) = get_numeric_literal(ex) {
1604-
let literal_str = snippet_opt(cx, lit.span).unwrap_or_default();
1604+
let literal_str = snippet_opt(cx, ex.span).unwrap_or_default();
16051605

16061606
if_chain! {
16071607
if let LitKind::Int(n, _) = lit.node;

tests/ui/unnecessary_cast_fixable.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ fn main() {
2929
0.5_f32;
3030

3131
1.0 as u16;
32+
33+
-1_i32;
34+
-1.0_f32;
3235
}

tests/ui/unnecessary_cast_fixable.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ fn main() {
2929
0.5 as f32;
3030

3131
1.0 as u16;
32+
33+
-1 as i32;
34+
-1.0 as f32;
3235
}

tests/ui/unnecessary_cast_fixable.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,17 @@ error: casting float literal to `f32` is unnecessary
6060
LL | 0.5 as f32;
6161
| ^^^^^^^^^^ help: try: `0.5_f32`
6262

63-
error: aborting due to 10 previous errors
63+
error: casting integer literal to `i32` is unnecessary
64+
--> $DIR/unnecessary_cast_fixable.rs:33:5
65+
|
66+
LL | -1 as i32;
67+
| ^^^^^^^^^ help: try: `-1_i32`
68+
69+
error: casting float literal to `f32` is unnecessary
70+
--> $DIR/unnecessary_cast_fixable.rs:34:5
71+
|
72+
LL | -1.0 as f32;
73+
| ^^^^^^^^^^^ help: try: `-1.0_f32`
74+
75+
error: aborting due to 12 previous errors
6476

0 commit comments

Comments
 (0)