diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2025-08-30 18:42:07 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2025-08-30 18:42:07 +0000 |
| commit | 3af81cf0b7bd394dac89cbacec303b5937b5519a (patch) | |
| tree | 64073091d9da807dcabb29da61f859009deca893 /compiler/rustc_parse/src | |
| parent | d216ca0506d6a70ca70fc29974ed9155beaabf7a (diff) | |
| download | rust-3af81cf0b7bd394dac89cbacec303b5937b5519a.tar.gz rust-3af81cf0b7bd394dac89cbacec303b5937b5519a.zip | |
review comment: move `Visitor`
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/stmt.rs | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 732c653e4bc..ad5ab6e6b77 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -806,6 +806,35 @@ impl<'a> Parser<'a> { } true }); + + struct IdentFinder { + idents: Vec<Ident>, + /// If a block references one of the bindings introduced by the let pattern, + /// we likely meant to use `if let`. + /// This is pre-expansion, so if we encounter + /// `let Some(x) = foo() { println!("{x}") }` we won't find it. + references_ident: bool = false, + /// If a block has a `return`, then we know with high certainty that it was + /// meant to be let-else. + has_return: bool = false, + } + + impl<'a> Visitor<'a> for IdentFinder { + fn visit_ident(&mut self, ident: &Ident) { + for i in &self.idents { + if ident.name == i.name { + self.references_ident = true; + } + } + } + fn visit_expr(&mut self, node: &'a Expr) { + if let ExprKind::Ret(..) = node.kind { + self.has_return = true; + } + walk_expr(self, node); + } + } + // Collect all bindings in pattern and see if they appear in the block. Likely meant // to write `if let`. See if the block has a return. Likely meant to write // `let else`. @@ -1132,30 +1161,3 @@ impl<'a> Parser<'a> { self.mk_block(thin_vec![self.mk_stmt_err(span, guar)], BlockCheckMode::Default, span) } } - -struct IdentFinder { - idents: Vec<Ident>, - /// If a block references one of the bindings introduced by the let pattern, we likely meant to - /// use `if let`. - /// This is pre-expansion, so if we encounter `let Some(x) = foo() { println!("{x}") }` we won't - /// find it. - references_ident: bool = false, - /// If a block has a `return`, then we know with high certainty that the - has_return: bool = false, -} - -impl<'a> Visitor<'a> for IdentFinder { - fn visit_ident(&mut self, ident: &Ident) { - for i in &self.idents { - if ident.name == i.name { - self.references_ident = true; - } - } - } - fn visit_expr(&mut self, node: &'a Expr) { - if let ExprKind::Ret(..) = node.kind { - self.has_return = true; - } - walk_expr(self, node); - } -} |
