add byref checking for the guard's local

This commit is contained in:
mojave2 2023-09-13 11:13:51 +08:00
parent 69fcbfdac0
commit 7f870201d3
No known key found for this signature in database
4 changed files with 176 additions and 21 deletions

View file

@ -143,3 +143,44 @@ fn g(opt_s: Option<S>) {
_ => {},
}
}
mod issue11465 {
enum A {
Foo([u8; 3]),
}
struct B {
b: String,
c: i32,
}
fn issue11465() {
let c = Some(1);
match c {
Some(1) => {},
Some(2) => {},
Some(3) => {},
_ => {},
};
let enum_a = A::Foo([98, 97, 114]);
match enum_a {
A::Foo(ref arr) if arr == b"foo" => {},
A::Foo(ref arr) if let b"bar" = arr => {},
A::Foo(ref arr) if matches!(arr, b"baz") => {},
_ => {},
};
let struct_b = B {
b: "bar".to_string(),
c: 42,
};
match struct_b {
B { ref b, .. } if b == "bar" => {},
B { ref c, .. } if c == &1 => {},
B { ref c, .. } if let &1 = c => {},
B { ref c, .. } if matches!(c, &1) => {},
_ => {},
}
}
}

View file

@ -143,3 +143,44 @@ fn g(opt_s: Option<S>) {
_ => {},
}
}
mod issue11465 {
enum A {
Foo([u8; 3]),
}
struct B {
b: String,
c: i32,
}
fn issue11465() {
let c = Some(1);
match c {
Some(ref x) if x == &1 => {},
Some(ref x) if let &2 = x => {},
Some(ref x) if matches!(x, &3) => {},
_ => {},
};
let enum_a = A::Foo([98, 97, 114]);
match enum_a {
A::Foo(ref arr) if arr == b"foo" => {},
A::Foo(ref arr) if let b"bar" = arr => {},
A::Foo(ref arr) if matches!(arr, b"baz") => {},
_ => {},
};
let struct_b = B {
b: "bar".to_string(),
c: 42,
};
match struct_b {
B { ref b, .. } if b == "bar" => {},
B { ref c, .. } if c == &1 => {},
B { ref c, .. } if let &1 = c => {},
B { ref c, .. } if matches!(c, &1) => {},
_ => {},
}
}
}

View file

@ -94,5 +94,41 @@ LL - x if matches!(x, Some(0)) => ..,
LL + Some(0) => ..,
|
error: aborting due to 8 previous errors
error: redundant guard
--> $DIR/redundant_guards.rs:160:28
|
LL | Some(ref x) if x == &1 => {},
| ^^^^^^^
|
help: try
|
LL - Some(ref x) if x == &1 => {},
LL + Some(1) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:161:28
|
LL | Some(ref x) if let &2 = x => {},
| ^^^^^^^^^^
|
help: try
|
LL - Some(ref x) if let &2 = x => {},
LL + Some(2) => {},
|
error: redundant guard
--> $DIR/redundant_guards.rs:162:28
|
LL | Some(ref x) if matches!(x, &3) => {},
| ^^^^^^^^^^^^^^^
|
help: try
|
LL - Some(ref x) if matches!(x, &3) => {},
LL + Some(3) => {},
|
error: aborting due to 11 previous errors