File: lint-if-let-rescope.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (109 lines) | stat: -rw-r--r-- 4,167 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//@ run-rustfix

#![deny(if_let_rescope)]
#![feature(stmt_expr_attributes)]
#![allow(irrefutable_let_patterns, unused_parens)]

fn droppy() -> Droppy {
    Droppy
}
struct Droppy;
impl Drop for Droppy {
    fn drop(&mut self) {
        println!("dropped");
    }
}
impl Droppy {
    const fn get(&self) -> Option<u8> {
        None
    }
}

fn main() {
    if let Some(_value) = droppy().get() {
        // Should not lint
    }

    if let Some(_value) = droppy().get() {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
        // do something
    } else {
        //~^ HELP: the value is now dropped here in Edition 2024
        // do something else
    }

    if let Some(_value) = droppy().get() {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
        // do something
    } else if let Some(_value) = droppy().get() {
        //~^ HELP: the value is now dropped here in Edition 2024
        // do something else
    }
    //~^ HELP: the value is now dropped here in Edition 2024

    if droppy().get().is_some() {
        // Should not lint
    } else if let Some(_value) = droppy().get() {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
    } else if droppy().get().is_none() {
        //~^ HELP: the value is now dropped here in Edition 2024
    }

    if let Some(1) = { if let Some(_value) = Droppy.get() { Some(1) } else { None } } {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: the value is now dropped here in Edition 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
    }

    if let () = { if let Some(_value) = Droppy.get() {} } {
        // This should not lint.
        // This `if let` sits is a tail expression of a block.
        // In Edition 2024, the temporaries are dropped before exiting the surrounding block.
    }

    #[rustfmt::skip]
    if (if let Some(_value) = droppy().get() { true } else { false }) {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: the value is now dropped here in Edition 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
        // do something
    } else if (((if let Some(_value) = droppy().get() { true } else { false }))) {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: the value is now dropped here in Edition 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
    }

    while let Some(_value) = droppy().get() {
        // Should not lint
        break;
    }

    while (if let Some(_value) = droppy().get() { false } else { true }) {
        //~^ ERROR: `if let` assigns a shorter lifetime since Edition 2024
        //~| WARN: this changes meaning in Rust 2024
        //~| HELP: the value is now dropped here in Edition 2024
        //~| HELP: a `match` with a single arm can preserve the drop order up to Edition 2021
    }

    // We want to keep the `if let`s below as direct descendents of match arms,
    // so the formatting is suppressed.
    #[rustfmt::skip]
    match droppy().get() {
        _ => if let Some(_value) = droppy().get() {},
        // Should not lint
        // There is implicitly a block surrounding the `if let`.
        // Given that it is a tail expression, the temporaries are dropped duly before
        // the execution is exiting the `match`.
    }

    if let Some(_value) = droppy().get() {}
}