summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/needless_borrowed_ref.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/needless_borrowed_ref.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/needless_borrowed_ref.rs')
-rw-r--r--src/tools/clippy/tests/ui/needless_borrowed_ref.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/needless_borrowed_ref.rs b/src/tools/clippy/tests/ui/needless_borrowed_ref.rs
new file mode 100644
index 000000000..500ac448f
--- /dev/null
+++ b/src/tools/clippy/tests/ui/needless_borrowed_ref.rs
@@ -0,0 +1,45 @@
+// run-rustfix
+
+#[warn(clippy::needless_borrowed_reference)]
+#[allow(unused_variables)]
+fn main() {
+ let mut v = Vec::<String>::new();
+ let _ = v.iter_mut().filter(|&ref a| a.is_empty());
+ // ^ should be linted
+
+ let var = 3;
+ let thingy = Some(&var);
+ if let Some(&ref v) = thingy {
+ // ^ should be linted
+ }
+
+ let mut var2 = 5;
+ let thingy2 = Some(&mut var2);
+ if let Some(&mut ref mut v) = thingy2 {
+ // ^ should **not** be linted
+ // v is borrowed as mutable.
+ *v = 10;
+ }
+ if let Some(&mut ref v) = thingy2 {
+ // ^ should **not** be linted
+ // here, v is borrowed as immutable.
+ // can't do that:
+ //*v = 15;
+ }
+}
+
+#[allow(dead_code)]
+enum Animal {
+ Cat(u64),
+ Dog(u64),
+}
+
+#[allow(unused_variables)]
+#[allow(dead_code)]
+fn foo(a: &Animal, b: &Animal) {
+ match (a, b) {
+ (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref'
+ // ^ and ^ should **not** be linted
+ (&Animal::Dog(ref a), &Animal::Dog(_)) => (), // ^ should **not** be linted
+ }
+}