about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-14 12:59:23 +0000
committerbors <bors@rust-lang.org>2022-04-14 12:59:23 +0000
commitecb3c3fc7edd43d6848f57f559bbddca1fae2e86 (patch)
tree16ec577f4b9c7d111f36eaa800be8a70484d9688 /tests
parent38ba05508ced18d46bea88109807e7b6da66baad (diff)
parent3bd0ac7a8d21b5b2c4e64dc39abf99c75af12916 (diff)
Auto merge of #8677 - xFrednet:8213-manual-bits-suggestion, r=giraffate
Add `usize` cast to `clippy::manual_bits` suggestion

A fix for the suggestion from https://github.com/rust-lang/rust-clippy/pull/8213

changelog: [`manual_bits`]: The suggestion now includes a cast for proper type conversion
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/manual_bits.fixed69
-rw-r--r--tests/ui/manual_bits.rs13
-rw-r--r--tests/ui/manual_bits.stderr126
3 files changed, 127 insertions, 81 deletions
diff --git a/tests/ui/manual_bits.fixed b/tests/ui/manual_bits.fixed
index 4f1b19b75b8..386360dbdcd 100644
--- a/tests/ui/manual_bits.fixed
+++ b/tests/ui/manual_bits.fixed
@@ -1,38 +1,44 @@
 // run-rustfix
 
 #![warn(clippy::manual_bits)]
-#![allow(clippy::no_effect, path_statements, unused_must_use, clippy::unnecessary_operation)]
+#![allow(
+    clippy::no_effect,
+    clippy::useless_conversion,
+    path_statements,
+    unused_must_use,
+    clippy::unnecessary_operation
+)]
 
 use std::mem::{size_of, size_of_val};
 
 fn main() {
-    i8::BITS;
-    i16::BITS;
-    i32::BITS;
-    i64::BITS;
-    i128::BITS;
-    isize::BITS;
-
-    u8::BITS;
-    u16::BITS;
-    u32::BITS;
-    u64::BITS;
-    u128::BITS;
-    usize::BITS;
-
-    i8::BITS;
-    i16::BITS;
-    i32::BITS;
-    i64::BITS;
-    i128::BITS;
-    isize::BITS;
-
-    u8::BITS;
-    u16::BITS;
-    u32::BITS;
-    u64::BITS;
-    u128::BITS;
-    usize::BITS;
+    i8::BITS as usize;
+    i16::BITS as usize;
+    i32::BITS as usize;
+    i64::BITS as usize;
+    i128::BITS as usize;
+    isize::BITS as usize;
+
+    u8::BITS as usize;
+    u16::BITS as usize;
+    u32::BITS as usize;
+    u64::BITS as usize;
+    u128::BITS as usize;
+    usize::BITS as usize;
+
+    i8::BITS as usize;
+    i16::BITS as usize;
+    i32::BITS as usize;
+    i64::BITS as usize;
+    i128::BITS as usize;
+    isize::BITS as usize;
+
+    u8::BITS as usize;
+    u16::BITS as usize;
+    u32::BITS as usize;
+    u64::BITS as usize;
+    u128::BITS as usize;
+    usize::BITS as usize;
 
     size_of::<usize>() * 4;
     4 * size_of::<usize>();
@@ -42,7 +48,12 @@ fn main() {
     size_of_val(&0u32) * 8;
 
     type Word = u32;
-    Word::BITS;
+    Word::BITS as usize;
     type Bool = bool;
     size_of::<Bool>() * 8;
+
+    let _: u32 = u128::BITS as u32;
+    let _: u32 = u128::BITS.try_into().unwrap();
+    let _ = (u128::BITS as usize).pow(5);
+    let _ = &(u128::BITS as usize);
 }
diff --git a/tests/ui/manual_bits.rs b/tests/ui/manual_bits.rs
index f8a01313e6a..62638f047eb 100644
--- a/tests/ui/manual_bits.rs
+++ b/tests/ui/manual_bits.rs
@@ -1,7 +1,13 @@
 // run-rustfix
 
 #![warn(clippy::manual_bits)]
-#![allow(clippy::no_effect, path_statements, unused_must_use, clippy::unnecessary_operation)]
+#![allow(
+    clippy::no_effect,
+    clippy::useless_conversion,
+    path_statements,
+    unused_must_use,
+    clippy::unnecessary_operation
+)]
 
 use std::mem::{size_of, size_of_val};
 
@@ -45,4 +51,9 @@ fn main() {
     size_of::<Word>() * 8;
     type Bool = bool;
     size_of::<Bool>() * 8;
+
+    let _: u32 = (size_of::<u128>() * 8) as u32;
+    let _: u32 = (size_of::<u128>() * 8).try_into().unwrap();
+    let _ = (size_of::<u128>() * 8).pow(5);
+    let _ = &(size_of::<u128>() * 8);
 }
diff --git a/tests/ui/manual_bits.stderr b/tests/ui/manual_bits.stderr
index c4f5af2dcb0..69c591a203d 100644
--- a/tests/ui/manual_bits.stderr
+++ b/tests/ui/manual_bits.stderr
@@ -1,154 +1,178 @@
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:9:5
+  --> $DIR/manual_bits.rs:15:5
    |
 LL |     size_of::<i8>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS as usize`
    |
    = note: `-D clippy::manual-bits` implied by `-D warnings`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:10:5
+  --> $DIR/manual_bits.rs:16:5
    |
 LL |     size_of::<i16>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:11:5
+  --> $DIR/manual_bits.rs:17:5
    |
 LL |     size_of::<i32>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:12:5
+  --> $DIR/manual_bits.rs:18:5
    |
 LL |     size_of::<i64>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:13:5
+  --> $DIR/manual_bits.rs:19:5
    |
 LL |     size_of::<i128>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:14:5
+  --> $DIR/manual_bits.rs:20:5
    |
 LL |     size_of::<isize>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:16:5
+  --> $DIR/manual_bits.rs:22:5
    |
 LL |     size_of::<u8>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:17:5
+  --> $DIR/manual_bits.rs:23:5
    |
 LL |     size_of::<u16>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:18:5
+  --> $DIR/manual_bits.rs:24:5
    |
 LL |     size_of::<u32>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:19:5
+  --> $DIR/manual_bits.rs:25:5
    |
 LL |     size_of::<u64>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:20:5
+  --> $DIR/manual_bits.rs:26:5
    |
 LL |     size_of::<u128>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:21:5
+  --> $DIR/manual_bits.rs:27:5
    |
 LL |     size_of::<usize>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:23:5
+  --> $DIR/manual_bits.rs:29:5
    |
 LL |     8 * size_of::<i8>();
-   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:24:5
+  --> $DIR/manual_bits.rs:30:5
    |
 LL |     8 * size_of::<i16>();
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:25:5
+  --> $DIR/manual_bits.rs:31:5
    |
 LL |     8 * size_of::<i32>();
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:26:5
+  --> $DIR/manual_bits.rs:32:5
    |
 LL |     8 * size_of::<i64>();
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:27:5
+  --> $DIR/manual_bits.rs:33:5
    |
 LL |     8 * size_of::<i128>();
-   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:28:5
+  --> $DIR/manual_bits.rs:34:5
    |
 LL |     8 * size_of::<isize>();
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:30:5
+  --> $DIR/manual_bits.rs:36:5
    |
 LL |     8 * size_of::<u8>();
-   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:31:5
+  --> $DIR/manual_bits.rs:37:5
    |
 LL |     8 * size_of::<u16>();
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:32:5
+  --> $DIR/manual_bits.rs:38:5
    |
 LL |     8 * size_of::<u32>();
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:33:5
+  --> $DIR/manual_bits.rs:39:5
    |
 LL |     8 * size_of::<u64>();
-   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:34:5
+  --> $DIR/manual_bits.rs:40:5
    |
 LL |     8 * size_of::<u128>();
-   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:35:5
+  --> $DIR/manual_bits.rs:41:5
    |
 LL |     8 * size_of::<usize>();
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS as usize`
 
 error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
-  --> $DIR/manual_bits.rs:45:5
+  --> $DIR/manual_bits.rs:51:5
    |
 LL |     size_of::<Word>() * 8;
-   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `Word::BITS`
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `Word::BITS as usize`
+
+error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
+  --> $DIR/manual_bits.rs:55:18
+   |
+LL |     let _: u32 = (size_of::<u128>() * 8) as u32;
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS`
+
+error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
+  --> $DIR/manual_bits.rs:56:18
+   |
+LL |     let _: u32 = (size_of::<u128>() * 8).try_into().unwrap();
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS`
+
+error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
+  --> $DIR/manual_bits.rs:57:13
+   |
+LL |     let _ = (size_of::<u128>() * 8).pow(5);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(u128::BITS as usize)`
+
+error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
+  --> $DIR/manual_bits.rs:58:14
+   |
+LL |     let _ = &(size_of::<u128>() * 8);
+   |              ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(u128::BITS as usize)`
 
-error: aborting due to 25 previous errors
+error: aborting due to 29 previous errors