Rollup merge of #82215 - TaKO8Ki:replace-if-let-while-let, r=varkor

Replace if-let and while-let with `if let` and `while let`

This pull request replaces if-let and while-let with `if let` and `while let`.

closes https://github.com/rust-lang/rust/issues/82205
This commit is contained in:
Dylan DPC 2021-02-18 16:57:37 +01:00 committed by GitHub
commit b3d3251271
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 45 additions and 45 deletions

View File

@ -1,6 +1,6 @@
#### Note: this error code is no longer emitted by the compiler. #### Note: this error code is no longer emitted by the compiler.
An if-let pattern attempts to match the pattern, and enters the body if the An `if let` pattern attempts to match the pattern, and enters the body if the
match was successful. If the match is irrefutable (when it cannot fail to match was successful. If the match is irrefutable (when it cannot fail to
match), use a regular `let`-binding instead. For instance: match), use a regular `let`-binding instead. For instance:

View File

@ -1,6 +1,6 @@
#### Note: this error code is no longer emitted by the compiler. #### Note: this error code is no longer emitted by the compiler.
A while-let pattern attempts to match the pattern, and enters the body if the A `while let` pattern attempts to match the pattern, and enters the body if the
match was successful. If the match is irrefutable (when it cannot fail to match was successful. If the match is irrefutable (when it cannot fail to
match), use a regular `let`-binding inside a `loop` instead. For instance: match), use a regular `let`-binding inside a `loop` instead. For instance:

View File

@ -1815,7 +1815,7 @@ declare_lint! {
declare_lint! { declare_lint! {
/// The `irrefutable_let_patterns` lint detects detects [irrefutable /// The `irrefutable_let_patterns` lint detects detects [irrefutable
/// patterns] in [if-let] and [while-let] statements. /// patterns] in [`if let`] and [`while let`] statements.
/// ///
/// ///
/// ///
@ -1832,7 +1832,7 @@ declare_lint! {
/// ### Explanation /// ### Explanation
/// ///
/// There usually isn't a reason to have an irrefutable pattern in an /// There usually isn't a reason to have an irrefutable pattern in an
/// if-let or while-let statement, because the pattern will always match /// `if let` or `while let` statement, because the pattern will always match
/// successfully. A [`let`] or [`loop`] statement will suffice. However, /// successfully. A [`let`] or [`loop`] statement will suffice. However,
/// when generating code with a macro, forbidding irrefutable patterns /// when generating code with a macro, forbidding irrefutable patterns
/// would require awkward workarounds in situations where the macro /// would require awkward workarounds in situations where the macro
@ -1843,14 +1843,14 @@ declare_lint! {
/// See [RFC 2086] for more details. /// See [RFC 2086] for more details.
/// ///
/// [irrefutable patterns]: https://doc.rust-lang.org/reference/patterns.html#refutability /// [irrefutable patterns]: https://doc.rust-lang.org/reference/patterns.html#refutability
/// [if-let]: https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-expressions /// [`if let`]: https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-expressions
/// [while-let]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-pattern-loops /// [`while let`]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-pattern-loops
/// [`let`]: https://doc.rust-lang.org/reference/statements.html#let-statements /// [`let`]: https://doc.rust-lang.org/reference/statements.html#let-statements
/// [`loop`]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#infinite-loops /// [`loop`]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#infinite-loops
/// [RFC 2086]: https://github.com/rust-lang/rfcs/blob/master/text/2086-allow-if-let-irrefutables.md /// [RFC 2086]: https://github.com/rust-lang/rfcs/blob/master/text/2086-allow-if-let-irrefutables.md
pub IRREFUTABLE_LET_PATTERNS, pub IRREFUTABLE_LET_PATTERNS,
Warn, Warn,
"detects irrefutable patterns in if-let and while-let statements" "detects irrefutable patterns in `if let` and `while let` statements"
} }
declare_lint! { declare_lint! {

View File

@ -368,9 +368,9 @@ fn unreachable_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, catchall: Option<
fn irrefutable_let_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, source: hir::MatchSource) { fn irrefutable_let_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, source: hir::MatchSource) {
tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| { tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| {
let msg = match source { let msg = match source {
hir::MatchSource::IfLetDesugar { .. } => "irrefutable if-let pattern", hir::MatchSource::IfLetDesugar { .. } => "irrefutable `if let` pattern",
hir::MatchSource::WhileLetDesugar => "irrefutable while-let pattern", hir::MatchSource::WhileLetDesugar => "irrefutable `while let` pattern",
hir::MatchSource::IfLetGuardDesugar => "irrefutable if-let guard", hir::MatchSource::IfLetGuardDesugar => "irrefutable `if let` guard",
_ => bug!(), _ => bug!(),
}; };
lint.build(msg).emit() lint.build(msg).emit()

View File

@ -45,7 +45,7 @@ impl NonConstExpr {
return None; return None;
} }
Self::Match(IfLetGuardDesugar) => bug!("if-let guard outside a `match` expression"), Self::Match(IfLetGuardDesugar) => bug!("`if let` guard outside a `match` expression"),
// All other expressions are allowed. // All other expressions are allowed.
Self::Loop(Loop | While | WhileLet) Self::Loop(Loop | While | WhileLet)

View File

@ -6,7 +6,7 @@ pub fn main() {
if let Some(y) = x { if let Some(y) = x {
assert_eq!(y, 3); assert_eq!(y, 3);
} else { } else {
panic!("if-let panicked"); panic!("`if let` panicked");
} }
let mut worked = false; let mut worked = false;
if let Some(_) = x { if let Some(_) = x {
@ -54,7 +54,7 @@ pub fn main() {
if let Foo::Two(b) = a { if let Foo::Two(b) = a {
assert_eq!(b, 42_usize); assert_eq!(b, 42_usize);
} else { } else {
panic!("panic in nested if-let"); panic!("panic in nested `if let`");
} }
} }
} }

View File

@ -16,7 +16,7 @@ fn main() {
// FIXME(project-rfc-2229#24): Change this to be a destructure pattern // FIXME(project-rfc-2229#24): Change this to be a destructure pattern
// once this is fixed, to remove the warning. // once this is fixed, to remove the warning.
if let SingleVariant::Point(ref mut x, _) = point { if let SingleVariant::Point(ref mut x, _) = point {
//~^ WARNING: irrefutable if-let pattern //~^ WARNING: irrefutable `if let` pattern
*x += 1; *x += 1;
} }
}; };

View File

@ -7,7 +7,7 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default = note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information = note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/closure-origin-single-variant-diagnostics.rs:18:9 --> $DIR/closure-origin-single-variant-diagnostics.rs:18:9
| |
LL | / if let SingleVariant::Point(ref mut x, _) = point { LL | / if let SingleVariant::Point(ref mut x, _) = point {

View File

@ -4,8 +4,8 @@ fn macros() {
macro_rules! foo{ macro_rules! foo{
($p:pat, $e:expr, $b:block) => {{ ($p:pat, $e:expr, $b:block) => {{
if let $p = $e $b if let $p = $e $b
//~^ WARN irrefutable if-let //~^ WARN irrefutable `if let`
//~| WARN irrefutable if-let //~| WARN irrefutable `if let`
}} }}
} }
macro_rules! bar{ macro_rules! bar{
@ -23,27 +23,27 @@ fn macros() {
} }
pub fn main() { pub fn main() {
if let a = 1 { //~ WARN irrefutable if-let if let a = 1 { //~ WARN irrefutable `if let`
println!("irrefutable pattern"); println!("irrefutable pattern");
} }
if let a = 1 { //~ WARN irrefutable if-let if let a = 1 { //~ WARN irrefutable `if let`
println!("irrefutable pattern"); println!("irrefutable pattern");
} else if true { } else if true {
println!("else-if in irrefutable if-let"); println!("else-if in irrefutable `if let`");
} else { } else {
println!("else in irrefutable if-let"); println!("else in irrefutable `if let`");
} }
if let 1 = 2 { if let 1 = 2 {
println!("refutable pattern"); println!("refutable pattern");
} else if let a = 1 { //~ WARN irrefutable if-let } else if let a = 1 { //~ WARN irrefutable `if let`
println!("irrefutable pattern"); println!("irrefutable pattern");
} }
if true { if true {
println!("if"); println!("if");
} else if let a = 1 { //~ WARN irrefutable if-let } else if let a = 1 { //~ WARN irrefutable `if let`
println!("irrefutable pattern"); println!("irrefutable pattern");
} }
} }

View File

@ -1,4 +1,4 @@
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:6:13 --> $DIR/if-let.rs:6:13
| |
LL | if let $p = $e $b LL | if let $p = $e $b
@ -12,7 +12,7 @@ LL | | });
= note: `#[warn(irrefutable_let_patterns)]` on by default = note: `#[warn(irrefutable_let_patterns)]` on by default
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:6:13 --> $DIR/if-let.rs:6:13
| |
LL | if let $p = $e $b LL | if let $p = $e $b
@ -25,7 +25,7 @@ LL | | });
| |
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:26:5 --> $DIR/if-let.rs:26:5
| |
LL | / if let a = 1 { LL | / if let a = 1 {
@ -33,19 +33,19 @@ LL | | println!("irrefutable pattern");
LL | | } LL | | }
| |_____^ | |_____^
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:30:5 --> $DIR/if-let.rs:30:5
| |
LL | / if let a = 1 { LL | / if let a = 1 {
LL | | println!("irrefutable pattern"); LL | | println!("irrefutable pattern");
LL | | } else if true { LL | | } else if true {
LL | | println!("else-if in irrefutable if-let"); LL | | println!("else-if in irrefutable `if let`");
LL | | } else { LL | | } else {
LL | | println!("else in irrefutable if-let"); LL | | println!("else in irrefutable `if let`");
LL | | } LL | | }
| |_____^ | |_____^
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:40:12 --> $DIR/if-let.rs:40:12
| |
LL | } else if let a = 1 { LL | } else if let a = 1 {
@ -54,7 +54,7 @@ LL | | println!("irrefutable pattern");
LL | | } LL | | }
| |_____^ | |_____^
warning: irrefutable if-let pattern warning: irrefutable `if let` pattern
--> $DIR/if-let.rs:46:12 --> $DIR/if-let.rs:46:12
| |
LL | } else if let a = 1 { LL | } else if let a = 1 {

View File

@ -1,4 +1,4 @@
// Test if the sugared if-let construct correctly prints "missing an else clause" when an else // Test if the sugared `if let` construct correctly prints "missing an else clause" when an else
// clause does not exist, instead of the unsympathetic "`match` arms have incompatible types" // clause does not exist, instead of the unsympathetic "`match` arms have incompatible types"
fn main() { fn main() {

View File

@ -1,9 +1,9 @@
#![deny(irrefutable_let_patterns)] #![deny(irrefutable_let_patterns)]
fn main() { fn main() {
if let _ = 5 {} //~ ERROR irrefutable if-let pattern if let _ = 5 {} //~ ERROR irrefutable `if let` pattern
while let _ = 5 { //~ ERROR irrefutable while-let pattern while let _ = 5 { //~ ERROR irrefutable `while let` pattern
break; break;
} }
} }

View File

@ -1,4 +1,4 @@
error: irrefutable if-let pattern error: irrefutable `if let` pattern
--> $DIR/deny-irrefutable-let-patterns.rs:4:5 --> $DIR/deny-irrefutable-let-patterns.rs:4:5
| |
LL | if let _ = 5 {} LL | if let _ = 5 {}
@ -10,7 +10,7 @@ note: the lint level is defined here
LL | #![deny(irrefutable_let_patterns)] LL | #![deny(irrefutable_let_patterns)]
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
error: irrefutable while-let pattern error: irrefutable `while let` pattern
--> $DIR/deny-irrefutable-let-patterns.rs:6:5 --> $DIR/deny-irrefutable-let-patterns.rs:6:5
| |
LL | / while let _ = 5 { LL | / while let _ = 5 {

View File

@ -5,7 +5,7 @@
fn irrefutable_let_guard() { fn irrefutable_let_guard() {
match Some(()) { match Some(()) {
Some(x) if let () = x => {} Some(x) if let () = x => {}
//~^ ERROR irrefutable if-let guard //~^ ERROR irrefutable `if let` guard
_ => {} _ => {}
} }
} }

View File

@ -1,4 +1,4 @@
error: irrefutable if-let guard error: irrefutable `if let` guard
--> $DIR/warns.rs:7:24 --> $DIR/warns.rs:7:24
| |
LL | Some(x) if let () = x => {} LL | Some(x) if let () = x => {}

View File

@ -5,8 +5,8 @@ fn macros() {
macro_rules! foo{ macro_rules! foo{
($p:pat, $e:expr, $b:block) => {{ ($p:pat, $e:expr, $b:block) => {{
while let $p = $e $b while let $p = $e $b
//~^ WARN irrefutable while-let //~^ WARN irrefutable `while let`
//~| WARN irrefutable while-let //~| WARN irrefutable `while let`
}} }}
} }
macro_rules! bar{ macro_rules! bar{
@ -24,7 +24,7 @@ fn macros() {
} }
pub fn main() { pub fn main() {
while let _a = 1 { //~ WARN irrefutable while-let while let _a = 1 { //~ WARN irrefutable `while let`
println!("irrefutable pattern"); println!("irrefutable pattern");
break; break;
} }

View File

@ -1,4 +1,4 @@
warning: irrefutable while-let pattern warning: irrefutable `while let` pattern
--> $DIR/while-let.rs:7:13 --> $DIR/while-let.rs:7:13
| |
LL | while let $p = $e $b LL | while let $p = $e $b
@ -12,7 +12,7 @@ LL | | });
= note: `#[warn(irrefutable_let_patterns)]` on by default = note: `#[warn(irrefutable_let_patterns)]` on by default
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: irrefutable while-let pattern warning: irrefutable `while let` pattern
--> $DIR/while-let.rs:7:13 --> $DIR/while-let.rs:7:13
| |
LL | while let $p = $e $b LL | while let $p = $e $b
@ -25,7 +25,7 @@ LL | | });
| |
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: irrefutable while-let pattern warning: irrefutable `while let` pattern
--> $DIR/while-let.rs:27:5 --> $DIR/while-let.rs:27:5
| |
LL | / while let _a = 1 { LL | / while let _a = 1 {

View File

@ -340,7 +340,7 @@ declare_clippy_lint! {
/// ``` /// ```
pub WHILE_LET_ON_ITERATOR, pub WHILE_LET_ON_ITERATOR,
style, style,
"using a while-let loop instead of a for loop on an iterator" "using a `while let` loop instead of a for loop on an iterator"
} }
declare_clippy_lint! { declare_clippy_lint! {