summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/get_last_with_len.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/get_last_with_len.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/get_last_with_len.rs')
-rw-r--r--src/tools/clippy/tests/ui/get_last_with_len.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/get_last_with_len.rs b/src/tools/clippy/tests/ui/get_last_with_len.rs
new file mode 100644
index 000000000..d63a731bd
--- /dev/null
+++ b/src/tools/clippy/tests/ui/get_last_with_len.rs
@@ -0,0 +1,49 @@
+// run-rustfix
+
+#![warn(clippy::get_last_with_len)]
+#![allow(unused)]
+
+use std::collections::VecDeque;
+
+fn dont_use_last() {
+ let x = vec![2, 3, 5];
+ let _ = x.get(x.len() - 1);
+}
+
+fn indexing_two_from_end() {
+ let x = vec![2, 3, 5];
+ let _ = x.get(x.len() - 2);
+}
+
+fn index_into_last() {
+ let x = vec![2, 3, 5];
+ let _ = x[x.len() - 1];
+}
+
+fn use_last_with_different_vec_length() {
+ let x = vec![2, 3, 5];
+ let y = vec!['a', 'b', 'c'];
+ let _ = x.get(y.len() - 1);
+}
+
+struct S {
+ field: Vec<usize>,
+}
+
+fn in_field(s: &S) {
+ let _ = s.field.get(s.field.len() - 1);
+}
+
+fn main() {
+ let slice = &[1, 2, 3];
+ let _ = slice.get(slice.len() - 1);
+
+ let array = [4, 5, 6];
+ let _ = array.get(array.len() - 1);
+
+ let deq = VecDeque::from([7, 8, 9]);
+ let _ = deq.get(deq.len() - 1);
+
+ let nested = [[1]];
+ let _ = nested[0].get(nested[0].len() - 1);
+}