Skip to content

Commit 494bc8a

Browse files
committed
Fix FN that types lints don't work with const or static
1 parent dad39b6 commit 494bc8a

File tree

8 files changed

+86
-28
lines changed

8 files changed

+86
-28
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ impl<'tcx> LateLintPass<'tcx> for Types {
249249
self.check_fn_decl(cx, decl);
250250
}
251251

252+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
253+
match item.kind {
254+
ItemKind::Static(ref ty, _, _) | ItemKind::Const(ref ty, _) => self.check_ty(cx, ty, false),
255+
// functions, enums, structs, impls and traits are covered
256+
_ => (),
257+
}
258+
}
259+
252260
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
253261
self.check_ty(cx, &field.ty, false);
254262
}

tests/ui/dlist.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
extern crate alloc;
66
use alloc::collections::linked_list::LinkedList;
77

8+
const C: LinkedList<i32> = LinkedList::new();
9+
static S: LinkedList<i32> = LinkedList::new();
10+
811
trait Foo {
912
type Baz = LinkedList<u8>;
1013
fn foo(_: LinkedList<u8>);

tests/ui/dlist.stderr

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,67 @@
11
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
2-
--> $DIR/dlist.rs:9:16
2+
--> $DIR/dlist.rs:8:10
3+
|
4+
LL | const C: LinkedList<i32> = LinkedList::new();
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::linkedlist` implied by `-D warnings`
8+
= help: a `VecDeque` might work
9+
10+
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
11+
--> $DIR/dlist.rs:9:11
12+
|
13+
LL | static S: LinkedList<i32> = LinkedList::new();
14+
| ^^^^^^^^^^^^^^^
15+
|
16+
= help: a `VecDeque` might work
17+
18+
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
19+
--> $DIR/dlist.rs:12:16
320
|
421
LL | type Baz = LinkedList<u8>;
522
| ^^^^^^^^^^^^^^
623
|
7-
= note: `-D clippy::linkedlist` implied by `-D warnings`
824
= help: a `VecDeque` might work
925

1026
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
11-
--> $DIR/dlist.rs:10:15
27+
--> $DIR/dlist.rs:13:15
1228
|
1329
LL | fn foo(_: LinkedList<u8>);
1430
| ^^^^^^^^^^^^^^
1531
|
1632
= help: a `VecDeque` might work
1733

1834
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
19-
--> $DIR/dlist.rs:11:23
35+
--> $DIR/dlist.rs:14:23
2036
|
2137
LL | const BAR: Option<LinkedList<u8>>;
2238
| ^^^^^^^^^^^^^^
2339
|
2440
= help: a `VecDeque` might work
2541

2642
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
27-
--> $DIR/dlist.rs:22:15
43+
--> $DIR/dlist.rs:25:15
2844
|
2945
LL | fn foo(_: LinkedList<u8>) {}
3046
| ^^^^^^^^^^^^^^
3147
|
3248
= help: a `VecDeque` might work
3349

3450
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
35-
--> $DIR/dlist.rs:25:39
51+
--> $DIR/dlist.rs:28:39
3652
|
3753
LL | pub fn test(my_favourite_linked_list: LinkedList<u8>) {
3854
| ^^^^^^^^^^^^^^
3955
|
4056
= help: a `VecDeque` might work
4157

4258
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
43-
--> $DIR/dlist.rs:29:29
59+
--> $DIR/dlist.rs:32:29
4460
|
4561
LL | pub fn test_ret() -> Option<LinkedList<u8>> {
4662
| ^^^^^^^^^^^^^^
4763
|
4864
= help: a `VecDeque` might work
4965

50-
error: aborting due to 6 previous errors
66+
error: aborting due to 8 previous errors
5167

tests/ui/option_option.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![deny(clippy::option_option)]
22
#![allow(clippy::unnecessary_wraps)]
33

4+
const C: Option<Option<i32>> = None;
5+
static S: Option<Option<i32>> = None;
6+
47
fn input(_: Option<Option<u8>>) {}
58

69
fn output() -> Option<Option<u8>> {

tests/ui/option_option.stderr

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
2-
--> $DIR/option_option.rs:4:13
2+
--> $DIR/option_option.rs:4:10
33
|
4-
LL | fn input(_: Option<Option<u8>>) {}
5-
| ^^^^^^^^^^^^^^^^^^
4+
LL | const C: Option<Option<i32>> = None;
5+
| ^^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/option_option.rs:1:9
@@ -11,58 +11,70 @@ LL | #![deny(clippy::option_option)]
1111
| ^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
14-
--> $DIR/option_option.rs:6:16
14+
--> $DIR/option_option.rs:5:11
15+
|
16+
LL | static S: Option<Option<i32>> = None;
17+
| ^^^^^^^^^^^^^^^^^^^
18+
19+
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
20+
--> $DIR/option_option.rs:7:13
21+
|
22+
LL | fn input(_: Option<Option<u8>>) {}
23+
| ^^^^^^^^^^^^^^^^^^
24+
25+
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
26+
--> $DIR/option_option.rs:9:16
1527
|
1628
LL | fn output() -> Option<Option<u8>> {
1729
| ^^^^^^^^^^^^^^^^^^
1830

1931
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
20-
--> $DIR/option_option.rs:10:27
32+
--> $DIR/option_option.rs:13:27
2133
|
2234
LL | fn output_nested() -> Vec<Option<Option<u8>>> {
2335
| ^^^^^^^^^^^^^^^^^^
2436

2537
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
26-
--> $DIR/option_option.rs:15:30
38+
--> $DIR/option_option.rs:18:30
2739
|
2840
LL | fn output_nested_nested() -> Option<Option<Option<u8>>> {
2941
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3042

3143
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
32-
--> $DIR/option_option.rs:20:8
44+
--> $DIR/option_option.rs:23:8
3345
|
3446
LL | x: Option<Option<u8>>,
3547
| ^^^^^^^^^^^^^^^^^^
3648

3749
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
38-
--> $DIR/option_option.rs:24:23
50+
--> $DIR/option_option.rs:27:23
3951
|
4052
LL | fn struct_fn() -> Option<Option<u8>> {
4153
| ^^^^^^^^^^^^^^^^^^
4254

4355
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
44-
--> $DIR/option_option.rs:30:22
56+
--> $DIR/option_option.rs:33:22
4557
|
4658
LL | fn trait_fn() -> Option<Option<u8>>;
4759
| ^^^^^^^^^^^^^^^^^^
4860

4961
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
50-
--> $DIR/option_option.rs:34:11
62+
--> $DIR/option_option.rs:37:11
5163
|
5264
LL | Tuple(Option<Option<u8>>),
5365
| ^^^^^^^^^^^^^^^^^^
5466

5567
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
56-
--> $DIR/option_option.rs:35:17
68+
--> $DIR/option_option.rs:38:17
5769
|
5870
LL | Struct { x: Option<Option<u8>> },
5971
| ^^^^^^^^^^^^^^^^^^
6072

6173
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
62-
--> $DIR/option_option.rs:76:14
74+
--> $DIR/option_option.rs:79:14
6375
|
6476
LL | foo: Option<Option<Cow<'a, str>>>,
6577
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6678

67-
error: aborting due to 10 previous errors
79+
error: aborting due to 12 previous errors
6880

tests/ui/vec_box_sized.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ struct BigStruct([i32; 10000]);
99
/// The following should trigger the lint
1010
mod should_trigger {
1111
use super::SizedStruct;
12+
const C: Vec<i32> = Vec::new();
13+
static S: Vec<i32> = Vec::new();
1214

1315
struct StructWithVecBox {
1416
sized_type: Vec<SizedStruct>,

tests/ui/vec_box_sized.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ struct BigStruct([i32; 10000]);
99
/// The following should trigger the lint
1010
mod should_trigger {
1111
use super::SizedStruct;
12+
const C: Vec<Box<i32>> = Vec::new();
13+
static S: Vec<Box<i32>> = Vec::new();
1214

1315
struct StructWithVecBox {
1416
sized_type: Vec<Box<SizedStruct>>,

tests/ui/vec_box_sized.stderr

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
error: `Vec<T>` is already on the heap, the boxing is unnecessary
2-
--> $DIR/vec_box_sized.rs:14:21
2+
--> $DIR/vec_box_sized.rs:12:14
33
|
4-
LL | sized_type: Vec<Box<SizedStruct>>,
5-
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
4+
LL | const C: Vec<Box<i32>> = Vec::new();
5+
| ^^^^^^^^^^^^^ help: try: `Vec<i32>`
66
|
77
= note: `-D clippy::vec-box` implied by `-D warnings`
88

99
error: `Vec<T>` is already on the heap, the boxing is unnecessary
10-
--> $DIR/vec_box_sized.rs:17:14
10+
--> $DIR/vec_box_sized.rs:13:15
11+
|
12+
LL | static S: Vec<Box<i32>> = Vec::new();
13+
| ^^^^^^^^^^^^^ help: try: `Vec<i32>`
14+
15+
error: `Vec<T>` is already on the heap, the boxing is unnecessary
16+
--> $DIR/vec_box_sized.rs:16:21
17+
|
18+
LL | sized_type: Vec<Box<SizedStruct>>,
19+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
20+
21+
error: `Vec<T>` is already on the heap, the boxing is unnecessary
22+
--> $DIR/vec_box_sized.rs:19:14
1123
|
1224
LL | struct A(Vec<Box<SizedStruct>>);
1325
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
1426

1527
error: `Vec<T>` is already on the heap, the boxing is unnecessary
16-
--> $DIR/vec_box_sized.rs:18:18
28+
--> $DIR/vec_box_sized.rs:20:18
1729
|
1830
LL | struct B(Vec<Vec<Box<(u32)>>>);
1931
| ^^^^^^^^^^^^^^^ help: try: `Vec<u32>`
2032

2133
error: `Vec<T>` is already on the heap, the boxing is unnecessary
22-
--> $DIR/vec_box_sized.rs:46:23
34+
--> $DIR/vec_box_sized.rs:48:23
2335
|
2436
LL | pub fn f() -> Vec<Box<S>> {
2537
| ^^^^^^^^^^^ help: try: `Vec<S>`
2638

27-
error: aborting due to 4 previous errors
39+
error: aborting due to 6 previous errors
2840

0 commit comments

Comments
 (0)