Skip to content

Commit ad633be

Browse files
authored
Use size_of from the prelude instead of imported (rust-lang#14355)
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80. changelog: none
2 parents 8108bd8 + e8cd3d4 commit ad633be

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

clippy_lints/src/manual_bits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ use rustc_span::{Span, sym};
1414

1515
declare_clippy_lint! {
1616
/// ### What it does
17-
/// Checks for usage of `std::mem::size_of::<T>() * 8` when
17+
/// Checks for usage of `size_of::<T>() * 8` when
1818
/// `T::BITS` is available.
1919
///
2020
/// ### Why is this bad?
2121
/// Can be written as the shorter `T::BITS`.
2222
///
2323
/// ### Example
2424
/// ```no_run
25-
/// std::mem::size_of::<usize>() * 8;
25+
/// size_of::<usize>() * 8;
2626
/// ```
2727
/// Use instead:
2828
/// ```no_run
@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualBits {
6868
cx,
6969
MANUAL_BITS,
7070
expr.span,
71-
"usage of `mem::size_of::<T>()` to obtain the size of `T` in bits",
71+
"usage of `size_of::<T>()` to obtain the size of `T` in bits",
7272
"consider using",
7373
sugg,
7474
app,

clippy_lints/src/manual_slice_size_calculation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ declare_clippy_lint! {
2424
/// ### Example
2525
/// ```no_run
2626
/// # let data : &[i32] = &[1, 2, 3];
27-
/// let newlen = data.len() * std::mem::size_of::<i32>();
27+
/// let newlen = data.len() * size_of::<i32>();
2828
/// ```
2929
/// Use instead:
3030
/// ```no_run
3131
/// # let data : &[i32] = &[1, 2, 3];
32-
/// let newlen = std::mem::size_of_val(data);
32+
/// let newlen = size_of_val(data);
3333
/// ```
3434
#[clippy::version = "1.70.0"]
3535
pub MANUAL_SLICE_SIZE_CALCULATION,

clippy_lints/src/size_of_in_element_count.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ declare_clippy_lint! {
1818
/// ### Example
1919
/// ```rust,no_run
2020
/// # use std::ptr::copy_nonoverlapping;
21-
/// # use std::mem::size_of;
2221
/// const SIZE: usize = 128;
2322
/// let x = [2u8; SIZE];
2423
/// let mut y = [2u8; SIZE];

clippy_lints/src/size_of_ref.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::sym;
88
declare_clippy_lint! {
99
/// ### What it does
1010
///
11-
/// Checks for calls to `std::mem::size_of_val()` where the argument is
11+
/// Checks for calls to `size_of_val()` where the argument is
1212
/// a reference to a reference.
1313
///
1414
/// ### Why is this bad?
@@ -29,7 +29,7 @@ declare_clippy_lint! {
2929
/// // is already a reference, `&self` is a double-reference.
3030
/// // The return value of `size_of_val()` therefore is the
3131
/// // size of the reference-type, not the size of `self`.
32-
/// std::mem::size_of_val(&self)
32+
/// size_of_val(&self)
3333
/// }
3434
/// }
3535
/// ```
@@ -42,14 +42,14 @@ declare_clippy_lint! {
4242
/// impl Foo {
4343
/// fn size(&self) -> usize {
4444
/// // Correct
45-
/// std::mem::size_of_val(self)
45+
/// size_of_val(self)
4646
/// }
4747
/// }
4848
/// ```
4949
#[clippy::version = "1.68.0"]
5050
pub SIZE_OF_REF,
5151
suspicious,
52-
"Argument to `std::mem::size_of_val()` is a double-reference, which is almost certainly unintended"
52+
"Argument to `size_of_val()` is a double-reference, which is almost certainly unintended"
5353
}
5454
declare_lint_pass!(SizeOfRef => [SIZE_OF_REF]);
5555

@@ -65,9 +65,9 @@ impl LateLintPass<'_> for SizeOfRef {
6565
cx,
6666
SIZE_OF_REF,
6767
expr.span,
68-
"argument to `std::mem::size_of_val()` is a reference to a reference",
68+
"argument to `size_of_val()` is a reference to a reference",
6969
None,
70-
"dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type",
70+
"dereference the argument to `size_of_val()` to get the size of the value instead of the size of the reference-type",
7171
);
7272
}
7373
}

tests/ui/manual_bits.stderr

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
1+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
22
--> tests/ui/manual_bits.rs:14:5
33
|
44
LL | size_of::<i8>() * 8;
@@ -7,169 +7,169 @@ LL | size_of::<i8>() * 8;
77
= note: `-D clippy::manual-bits` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::manual_bits)]`
99

10-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
10+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
1111
--> tests/ui/manual_bits.rs:16:5
1212
|
1313
LL | size_of::<i16>() * 8;
1414
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS as usize`
1515

16-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
16+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
1717
--> tests/ui/manual_bits.rs:18:5
1818
|
1919
LL | size_of::<i32>() * 8;
2020
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS as usize`
2121

22-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
22+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
2323
--> tests/ui/manual_bits.rs:20:5
2424
|
2525
LL | size_of::<i64>() * 8;
2626
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS as usize`
2727

28-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
28+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
2929
--> tests/ui/manual_bits.rs:22:5
3030
|
3131
LL | size_of::<i128>() * 8;
3232
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS as usize`
3333

34-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
34+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
3535
--> tests/ui/manual_bits.rs:24:5
3636
|
3737
LL | size_of::<isize>() * 8;
3838
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS as usize`
3939

40-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
40+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
4141
--> tests/ui/manual_bits.rs:27:5
4242
|
4343
LL | size_of::<u8>() * 8;
4444
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS as usize`
4545

46-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
46+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
4747
--> tests/ui/manual_bits.rs:29:5
4848
|
4949
LL | size_of::<u16>() * 8;
5050
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS as usize`
5151

52-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
52+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
5353
--> tests/ui/manual_bits.rs:31:5
5454
|
5555
LL | size_of::<u32>() * 8;
5656
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS as usize`
5757

58-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
58+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
5959
--> tests/ui/manual_bits.rs:33:5
6060
|
6161
LL | size_of::<u64>() * 8;
6262
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS as usize`
6363

64-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
64+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
6565
--> tests/ui/manual_bits.rs:35:5
6666
|
6767
LL | size_of::<u128>() * 8;
6868
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS as usize`
6969

70-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
70+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
7171
--> tests/ui/manual_bits.rs:37:5
7272
|
7373
LL | size_of::<usize>() * 8;
7474
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS as usize`
7575

76-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
76+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
7777
--> tests/ui/manual_bits.rs:40:5
7878
|
7979
LL | 8 * size_of::<i8>();
8080
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `i8::BITS as usize`
8181

82-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
82+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
8383
--> tests/ui/manual_bits.rs:42:5
8484
|
8585
LL | 8 * size_of::<i16>();
8686
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i16::BITS as usize`
8787

88-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
88+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
8989
--> tests/ui/manual_bits.rs:44:5
9090
|
9191
LL | 8 * size_of::<i32>();
9292
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i32::BITS as usize`
9393

94-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
94+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
9595
--> tests/ui/manual_bits.rs:46:5
9696
|
9797
LL | 8 * size_of::<i64>();
9898
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `i64::BITS as usize`
9999

100-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
100+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
101101
--> tests/ui/manual_bits.rs:48:5
102102
|
103103
LL | 8 * size_of::<i128>();
104104
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `i128::BITS as usize`
105105

106-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
106+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
107107
--> tests/ui/manual_bits.rs:50:5
108108
|
109109
LL | 8 * size_of::<isize>();
110110
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `isize::BITS as usize`
111111

112-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
112+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
113113
--> tests/ui/manual_bits.rs:53:5
114114
|
115115
LL | 8 * size_of::<u8>();
116116
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `u8::BITS as usize`
117117

118-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
118+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
119119
--> tests/ui/manual_bits.rs:55:5
120120
|
121121
LL | 8 * size_of::<u16>();
122122
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u16::BITS as usize`
123123

124-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
124+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
125125
--> tests/ui/manual_bits.rs:57:5
126126
|
127127
LL | 8 * size_of::<u32>();
128128
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u32::BITS as usize`
129129

130-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
130+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
131131
--> tests/ui/manual_bits.rs:59:5
132132
|
133133
LL | 8 * size_of::<u64>();
134134
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `u64::BITS as usize`
135135

136-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
136+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
137137
--> tests/ui/manual_bits.rs:61:5
138138
|
139139
LL | 8 * size_of::<u128>();
140140
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS as usize`
141141

142-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
142+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
143143
--> tests/ui/manual_bits.rs:63:5
144144
|
145145
LL | 8 * size_of::<usize>();
146146
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `usize::BITS as usize`
147147

148-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
148+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
149149
--> tests/ui/manual_bits.rs:74:5
150150
|
151151
LL | size_of::<Word>() * 8;
152152
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `Word::BITS as usize`
153153

154-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
154+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
155155
--> tests/ui/manual_bits.rs:79:18
156156
|
157157
LL | let _: u32 = (size_of::<u128>() * 8) as u32;
158158
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS`
159159

160-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
160+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
161161
--> tests/ui/manual_bits.rs:81:18
162162
|
163163
LL | let _: u32 = (size_of::<u128>() * 8).try_into().unwrap();
164164
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `u128::BITS`
165165

166-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
166+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
167167
--> tests/ui/manual_bits.rs:83:13
168168
|
169169
LL | let _ = (size_of::<u128>() * 8).pow(5);
170170
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(u128::BITS as usize)`
171171

172-
error: usage of `mem::size_of::<T>()` to obtain the size of `T` in bits
172+
error: usage of `size_of::<T>()` to obtain the size of `T` in bits
173173
--> tests/ui/manual_bits.rs:85:14
174174
|
175175
LL | let _ = &(size_of::<u128>() * 8);

tests/ui/size_of_ref.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
error: argument to `std::mem::size_of_val()` is a reference to a reference
1+
error: argument to `size_of_val()` is a reference to a reference
22
--> tests/ui/size_of_ref.rs:13:5
33
|
44
LL | size_of_val(&&x);
55
| ^^^^^^^^^^^^^^^^
66
|
7-
= help: dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type
7+
= help: dereference the argument to `size_of_val()` to get the size of the value instead of the size of the reference-type
88
= note: `-D clippy::size-of-ref` implied by `-D warnings`
99
= help: to override `-D warnings` add `#[allow(clippy::size_of_ref)]`
1010

11-
error: argument to `std::mem::size_of_val()` is a reference to a reference
11+
error: argument to `size_of_val()` is a reference to a reference
1212
--> tests/ui/size_of_ref.rs:16:5
1313
|
1414
LL | size_of_val(&y);
1515
| ^^^^^^^^^^^^^^^
1616
|
17-
= help: dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type
17+
= help: dereference the argument to `size_of_val()` to get the size of the value instead of the size of the reference-type
1818

19-
error: argument to `std::mem::size_of_val()` is a reference to a reference
19+
error: argument to `size_of_val()` is a reference to a reference
2020
--> tests/ui/size_of_ref.rs:28:9
2121
|
2222
LL | std::mem::size_of_val(&self) + (std::mem::size_of::<u8>() * self.data.capacity())
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424
|
25-
= help: dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type
25+
= help: dereference the argument to `size_of_val()` to get the size of the value instead of the size of the reference-type
2626

2727
error: aborting due to 3 previous errors
2828

0 commit comments

Comments
 (0)