]>
git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/collection_is_never_read.rs
2 #![warn(clippy::collection_is_never_read)]
4 use std
::collections
::{HashMap, HashSet}
;
8 fn not_a_collection() {
9 // TODO: Expand `collection_is_never_read` beyond collections?
14 fn no_access_at_all() {
15 // Other lints should catch this.
16 let x
= vec
![1, 2, 3]; // Ok
19 fn write_without_read() {
20 // The main use case for `collection_is_never_read`.
21 let mut x
= HashMap
::new(); // WARNING
25 fn read_without_write() {
26 let mut x
= vec
![1, 2, 3]; // Ok
31 let mut x
= vec
![1, 2, 3]; // Ok
36 fn write_after_read() {
37 // TODO: Warn here, but this requires more extensive data flow analysis.
38 let mut x
= vec
![1, 2, 3]; // Ok
40 x
.push(4); // Pointless
43 fn write_before_reassign() {
44 // TODO: Warn here, but this requires more extensive data flow analysis.
45 let mut x
= HashMap
::new(); // Ok
46 x
.insert(1, 2); // Pointless
51 fn read_in_closure() {
52 let mut x
= HashMap
::new(); // Ok
59 fn write_in_closure() {
60 let mut x
= vec
![1, 2, 3]; // WARNING
67 let mut x
= HashMap
::new(); // Ok
73 let x
= HashMap
::<usize, usize>::new(); // Ok
75 let mut x
= HashMap
::new(); // WARNING
80 let mut x
= HashMap
::new(); // WARNING
82 let x
= HashMap
::<usize, usize>::new(); // Ok
86 #[allow(clippy::let_unit_value)]
88 let mut x
= vec
![1, 2, 3]; // WARNING
90 let _
: () = x
.clear();
94 let mut x
= vec
![1, 2, 3]; // WARNING
96 println
!("{:?}", x
.push(5));
100 let mut x
= vec
![1, 2, 3]; // WARNING
101 let y
= vec
![4, 5, 6]; // Ok
105 #[allow(clippy::self_assignment)]
106 fn self_assignment() {
107 let mut x
= vec
![1, 2, 3]; // WARNING
111 fn method_argument_but_not_target() {
114 fn my_method(&self, _argument
: &[usize]) {}
116 let my_struct
= MyStruct
;
118 let mut x
= vec
![1, 2, 3]; // Ok
120 my_struct
.my_method(&x
);
123 fn insert_is_not_a_read() {
124 let mut x
= HashSet
::new(); // WARNING
128 fn insert_is_a_read() {
129 let mut x
= HashSet
::new(); // Ok
131 println
!("5 was inserted");
135 fn not_read_if_return_value_not_used() {
136 // `is_empty` does not modify the set, so it's a query. But since the return value is not used, the
137 // lint does not consider it a read here.
138 let x
= vec
![1, 2, 3]; // WARNING
142 fn extension_traits() {
144 fn method_with_side_effect(&self);
145 fn method_without_side_effect(&self);
148 impl<T
> VecExt
<T
> for Vec
<T
> {
149 fn method_with_side_effect(&self) {
150 println
!("my length: {}", self.len());
152 fn method_without_side_effect(&self) {}
155 let x
= vec
![1, 2, 3]; // Ok
156 x
.method_with_side_effect();
158 let y
= vec
![1, 2, 3]; // Ok (false negative)
159 y
.method_without_side_effect();
162 fn function_argument() {
163 #[allow(clippy::ptr_arg)]
164 fn foo
<T
>(v
: &Vec
<T
>) -> usize {
168 let x
= vec
![1, 2, 3]; // Ok
173 // Do lint (write without read)
174 let mut s
= String
::new();
175 s
.push_str("Hello, World!");
177 // Do not lint (read without write)
178 let mut s
= String
::from("Hello, World!");
181 // Do not lint (write and read)
182 let mut s
= String
::from("Hello, World!");
183 s
.push_str("foo, bar");
186 // Do lint the first line, but not the second
187 let mut s
= String
::from("Hello, World!");
188 let t
= String
::from("foo, bar");