]> git.lizzy.rs Git - rust.git/commitdiff
Add test case for negative literals
authorGeoffrey Copin <copin.geoffrey@gmail.com>
Thu, 22 Oct 2020 22:04:27 +0000 (00:04 +0200)
committerGeoffrey Copin <copin.geoffrey@gmail.com>
Thu, 22 Oct 2020 22:04:27 +0000 (00:04 +0200)
clippy_lints/src/types.rs
tests/ui/unnecessary_cast_fixable.fixed
tests/ui/unnecessary_cast_fixable.rs
tests/ui/unnecessary_cast_fixable.stderr

index f4bb648d15a48d30e7077bf66243ce5b986b7dab..3a088709a7e4c0fd8b45464aadc9162bd91413f3 100644 (file)
@@ -1601,7 +1601,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
             let (cast_from, cast_to) = (cx.typeck_results().expr_ty(ex), cx.typeck_results().expr_ty(expr));
             lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
             if let Some(lit) = get_numeric_literal(ex) {
-                let literal_str = snippet_opt(cx, lit.span).unwrap_or_default();
+                let literal_str = snippet_opt(cx, ex.span).unwrap_or_default();
 
                 if_chain! {
                     if let LitKind::Int(n, _) = lit.node;
index 54853f4b8a2631d97c9e7b348b01161229a228d3..2a13469b1465290c1e8b31bdb9b08339191ccb45 100644 (file)
@@ -29,4 +29,7 @@ fn main() {
     0.5_f32;
 
     1.0 as u16;
+
+    -1_i32;
+    -1.0_f32;
 }
index 8da3d9477024b4c338c974be324ceae4eb4d97fa..65ddd3c7fbfb2ac9e04eb204eb5632553e6ba400 100644 (file)
@@ -29,4 +29,7 @@ fn main() {
     0.5 as f32;
 
     1.0 as u16;
+
+    -1 as i32;
+    -1.0 as f32;
 }
index 28fb9540afc06a6fefa9f34bf376e0e35811df8e..26b23e315e3f18ed6436f98c0666e38e78d6b211 100644 (file)
@@ -60,5 +60,17 @@ error: casting float literal to `f32` is unnecessary
 LL |     0.5 as f32;
    |     ^^^^^^^^^^ help: try: `0.5_f32`
 
-error: aborting due to 10 previous errors
+error: casting integer literal to `i32` is unnecessary
+  --> $DIR/unnecessary_cast_fixable.rs:33:5
+   |
+LL |     -1 as i32;
+   |     ^^^^^^^^^ help: try: `-1_i32`
+
+error: casting float literal to `f32` is unnecessary
+  --> $DIR/unnecessary_cast_fixable.rs:34:5
+   |
+LL |     -1.0 as f32;
+   |     ^^^^^^^^^^^ help: try: `-1.0_f32`
+
+error: aborting due to 12 previous errors