mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Fix FN that types lints don't work with const or static
This commit is contained in:
parent
dad39b6613
commit
494bc8a30c
8 changed files with 87 additions and 29 deletions
|
@ -249,6 +249,14 @@ impl<'tcx> LateLintPass<'tcx> for Types {
|
|||
self.check_fn_decl(cx, decl);
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
||||
match item.kind {
|
||||
ItemKind::Static(ref ty, _, _) | ItemKind::Const(ref ty, _) => self.check_ty(cx, ty, false),
|
||||
// functions, enums, structs, impls and traits are covered
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
|
||||
self.check_ty(cx, &field.ty, false);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
extern crate alloc;
|
||||
use alloc::collections::linked_list::LinkedList;
|
||||
|
||||
const C: LinkedList<i32> = LinkedList::new();
|
||||
static S: LinkedList<i32> = LinkedList::new();
|
||||
|
||||
trait Foo {
|
||||
type Baz = LinkedList<u8>;
|
||||
fn foo(_: LinkedList<u8>);
|
||||
|
|
|
@ -1,14 +1,30 @@
|
|||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:9:16
|
||||
--> $DIR/dlist.rs:8:10
|
||||
|
|
||||
LL | type Baz = LinkedList<u8>;
|
||||
| ^^^^^^^^^^^^^^
|
||||
LL | const C: LinkedList<i32> = LinkedList::new();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::linkedlist` implied by `-D warnings`
|
||||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:10:15
|
||||
--> $DIR/dlist.rs:9:11
|
||||
|
|
||||
LL | static S: LinkedList<i32> = LinkedList::new();
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:12:16
|
||||
|
|
||||
LL | type Baz = LinkedList<u8>;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:13:15
|
||||
|
|
||||
LL | fn foo(_: LinkedList<u8>);
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
@ -16,7 +32,7 @@ LL | fn foo(_: LinkedList<u8>);
|
|||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:11:23
|
||||
--> $DIR/dlist.rs:14:23
|
||||
|
|
||||
LL | const BAR: Option<LinkedList<u8>>;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
@ -24,7 +40,7 @@ LL | const BAR: Option<LinkedList<u8>>;
|
|||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:22:15
|
||||
--> $DIR/dlist.rs:25:15
|
||||
|
|
||||
LL | fn foo(_: LinkedList<u8>) {}
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
@ -32,7 +48,7 @@ LL | fn foo(_: LinkedList<u8>) {}
|
|||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:25:39
|
||||
--> $DIR/dlist.rs:28:39
|
||||
|
|
||||
LL | pub fn test(my_favourite_linked_list: LinkedList<u8>) {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
@ -40,12 +56,12 @@ LL | pub fn test(my_favourite_linked_list: LinkedList<u8>) {
|
|||
= help: a `VecDeque` might work
|
||||
|
||||
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
|
||||
--> $DIR/dlist.rs:29:29
|
||||
--> $DIR/dlist.rs:32:29
|
||||
|
|
||||
LL | pub fn test_ret() -> Option<LinkedList<u8>> {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: a `VecDeque` might work
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: aborting due to 8 previous errors
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
#![deny(clippy::option_option)]
|
||||
#![allow(clippy::unnecessary_wraps)]
|
||||
|
||||
const C: Option<Option<i32>> = None;
|
||||
static S: Option<Option<i32>> = None;
|
||||
|
||||
fn input(_: Option<Option<u8>>) {}
|
||||
|
||||
fn output() -> Option<Option<u8>> {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:4:13
|
||||
--> $DIR/option_option.rs:4:10
|
||||
|
|
||||
LL | fn input(_: Option<Option<u8>>) {}
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
LL | const C: Option<Option<i32>> = None;
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/option_option.rs:1:9
|
||||
|
@ -11,58 +11,70 @@ LL | #![deny(clippy::option_option)]
|
|||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:6:16
|
||||
--> $DIR/option_option.rs:5:11
|
||||
|
|
||||
LL | static S: Option<Option<i32>> = None;
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:7:13
|
||||
|
|
||||
LL | fn input(_: Option<Option<u8>>) {}
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:9:16
|
||||
|
|
||||
LL | fn output() -> Option<Option<u8>> {
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:10:27
|
||||
--> $DIR/option_option.rs:13:27
|
||||
|
|
||||
LL | fn output_nested() -> Vec<Option<Option<u8>>> {
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:15:30
|
||||
--> $DIR/option_option.rs:18:30
|
||||
|
|
||||
LL | fn output_nested_nested() -> Option<Option<Option<u8>>> {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:20:8
|
||||
--> $DIR/option_option.rs:23:8
|
||||
|
|
||||
LL | x: Option<Option<u8>>,
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:24:23
|
||||
--> $DIR/option_option.rs:27:23
|
||||
|
|
||||
LL | fn struct_fn() -> Option<Option<u8>> {
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:30:22
|
||||
--> $DIR/option_option.rs:33:22
|
||||
|
|
||||
LL | fn trait_fn() -> Option<Option<u8>>;
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:34:11
|
||||
--> $DIR/option_option.rs:37:11
|
||||
|
|
||||
LL | Tuple(Option<Option<u8>>),
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:35:17
|
||||
--> $DIR/option_option.rs:38:17
|
||||
|
|
||||
LL | Struct { x: Option<Option<u8>> },
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
|
||||
--> $DIR/option_option.rs:76:14
|
||||
--> $DIR/option_option.rs:79:14
|
||||
|
|
||||
LL | foo: Option<Option<Cow<'a, str>>>,
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 10 previous errors
|
||||
error: aborting due to 12 previous errors
|
||||
|
||||
|
|
|
@ -9,6 +9,8 @@ struct BigStruct([i32; 10000]);
|
|||
/// The following should trigger the lint
|
||||
mod should_trigger {
|
||||
use super::SizedStruct;
|
||||
const C: Vec<i32> = Vec::new();
|
||||
static S: Vec<i32> = Vec::new();
|
||||
|
||||
struct StructWithVecBox {
|
||||
sized_type: Vec<SizedStruct>,
|
||||
|
|
|
@ -9,6 +9,8 @@ struct BigStruct([i32; 10000]);
|
|||
/// The following should trigger the lint
|
||||
mod should_trigger {
|
||||
use super::SizedStruct;
|
||||
const C: Vec<Box<i32>> = Vec::new();
|
||||
static S: Vec<Box<i32>> = Vec::new();
|
||||
|
||||
struct StructWithVecBox {
|
||||
sized_type: Vec<Box<SizedStruct>>,
|
||||
|
|
|
@ -1,28 +1,40 @@
|
|||
error: `Vec<T>` is already on the heap, the boxing is unnecessary
|
||||
--> $DIR/vec_box_sized.rs:14:21
|
||||
--> $DIR/vec_box_sized.rs:12:14
|
||||
|
|
||||
LL | sized_type: Vec<Box<SizedStruct>>,
|
||||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
|
||||
LL | const C: Vec<Box<i32>> = Vec::new();
|
||||
| ^^^^^^^^^^^^^ help: try: `Vec<i32>`
|
||||
|
|
||||
= note: `-D clippy::vec-box` implied by `-D warnings`
|
||||
|
||||
error: `Vec<T>` is already on the heap, the boxing is unnecessary
|
||||
--> $DIR/vec_box_sized.rs:17:14
|
||||
--> $DIR/vec_box_sized.rs:13:15
|
||||
|
|
||||
LL | static S: Vec<Box<i32>> = Vec::new();
|
||||
| ^^^^^^^^^^^^^ help: try: `Vec<i32>`
|
||||
|
||||
error: `Vec<T>` is already on the heap, the boxing is unnecessary
|
||||
--> $DIR/vec_box_sized.rs:16:21
|
||||
|
|
||||
LL | sized_type: Vec<Box<SizedStruct>>,
|
||||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
|
||||
|
||||
error: `Vec<T>` is already on the heap, the boxing is unnecessary
|
||||
--> $DIR/vec_box_sized.rs:19:14
|
||||
|
|
||||
LL | struct A(Vec<Box<SizedStruct>>);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
|
||||
|
||||
error: `Vec<T>` is already on the heap, the boxing is unnecessary
|
||||
--> $DIR/vec_box_sized.rs:18:18
|
||||
--> $DIR/vec_box_sized.rs:20:18
|
||||
|
|
||||
LL | struct B(Vec<Vec<Box<(u32)>>>);
|
||||
| ^^^^^^^^^^^^^^^ help: try: `Vec<u32>`
|
||||
|
||||
error: `Vec<T>` is already on the heap, the boxing is unnecessary
|
||||
--> $DIR/vec_box_sized.rs:46:23
|
||||
--> $DIR/vec_box_sized.rs:48:23
|
||||
|
|
||||
LL | pub fn f() -> Vec<Box<S>> {
|
||||
| ^^^^^^^^^^^ help: try: `Vec<S>`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue