Skip to content

Commit fe0a415

Browse files
committed
Auto merge of #60152 - stepnivlk:visit_subpats-removal, r=varkor
Remove `visit_subpats` parameter from `check_pat` The core idea is to keep track of current ID directly in `EllipsisInclusiveRangePatterns` struct and early return in `check_pat` based on it. Fixes #60043. r? @varkor
2 parents 31f5d69 + 1dc13b5 commit fe0a415

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

src/librustc/lint/context.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1164,12 +1164,10 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
11641164
}
11651165

11661166
fn visit_pat(&mut self, p: &'a ast::Pat) {
1167-
let mut visit_subpats = true;
1168-
run_early_pass!(self, check_pat, p, &mut visit_subpats);
1167+
run_early_pass!(self, check_pat, p);
11691168
self.check_id(p.id);
1170-
if visit_subpats {
1171-
ast_visit::walk_pat(self, p);
1172-
}
1169+
ast_visit::walk_pat(self, p);
1170+
run_early_pass!(self, check_pat_post, p);
11731171
}
11741172

11751173
fn visit_expr(&mut self, e: &'a ast::Expr) {

src/librustc/lint/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ macro_rules! early_lint_methods {
371371
fn check_block_post(a: &ast::Block);
372372
fn check_stmt(a: &ast::Stmt);
373373
fn check_arm(a: &ast::Arm);
374-
fn check_pat(a: &ast::Pat, b: &mut bool); // FIXME: &mut bool looks just broken
374+
fn check_pat(a: &ast::Pat);
375+
fn check_pat_post(a: &ast::Pat);
375376
fn check_expr(a: &ast::Expr);
376377
fn check_expr_post(a: &ast::Expr);
377378
fn check_ty(a: &ast::Ty);

src/librustc_lint/builtin.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -1285,10 +1285,29 @@ declare_lint! {
12851285
"`...` range patterns are deprecated"
12861286
}
12871287

1288-
declare_lint_pass!(EllipsisInclusiveRangePatterns => [ELLIPSIS_INCLUSIVE_RANGE_PATTERNS]);
1288+
pub struct EllipsisInclusiveRangePatterns {
1289+
/// If `Some(_)`, suppress all subsequent pattern
1290+
/// warnings for better diagnostics.
1291+
node_id: Option<ast::NodeId>,
1292+
}
1293+
1294+
impl_lint_pass!(EllipsisInclusiveRangePatterns => [ELLIPSIS_INCLUSIVE_RANGE_PATTERNS]);
1295+
1296+
impl EllipsisInclusiveRangePatterns {
1297+
pub fn new() -> Self {
1298+
Self {
1299+
node_id: None,
1300+
}
1301+
}
1302+
}
12891303

12901304
impl EarlyLintPass for EllipsisInclusiveRangePatterns {
1291-
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &ast::Pat, visit_subpats: &mut bool) {
1305+
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &ast::Pat) {
1306+
if self.node_id.is_some() {
1307+
// Don't recursively warn about patterns inside range endpoints.
1308+
return
1309+
}
1310+
12921311
use self::ast::{PatKind, RangeEnd, RangeSyntax::DotDotDot};
12931312

12941313
/// If `pat` is a `...` pattern, return the start and end of the range, as well as the span
@@ -1311,7 +1330,7 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
13111330
let msg = "`...` range patterns are deprecated";
13121331
let suggestion = "use `..=` for an inclusive range";
13131332
if parenthesise {
1314-
*visit_subpats = false;
1333+
self.node_id = Some(pat.id);
13151334
let mut err = cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, msg);
13161335
err.span_suggestion(
13171336
pat.span,
@@ -1332,6 +1351,14 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
13321351
};
13331352
}
13341353
}
1354+
1355+
fn check_pat_post(&mut self, _cx: &EarlyContext<'_>, pat: &ast::Pat) {
1356+
if let Some(node_id) = self.node_id {
1357+
if pat.id == node_id {
1358+
self.node_id = None
1359+
}
1360+
}
1361+
}
13351362
}
13361363

13371364
declare_lint! {

src/librustc_lint/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ macro_rules! early_lint_passes {
9494
UnusedImportBraces: UnusedImportBraces,
9595
UnsafeCode: UnsafeCode,
9696
AnonymousParameters: AnonymousParameters,
97-
EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns,
97+
EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::new(),
9898
NonCamelCaseTypes: NonCamelCaseTypes,
9999
DeprecatedAttr: DeprecatedAttr::new(),
100100
]);

src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl EarlyLintPass for UnusedParens {
407407
self.check_unused_parens_expr(cx, &value, msg, followed_by_block);
408408
}
409409

410-
fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat, _: &mut bool) {
410+
fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &ast::Pat) {
411411
use ast::PatKind::{Paren, Range};
412412
// The lint visitor will visit each subpattern of `p`. We do not want to lint any range
413413
// pattern no matter where it occurs in the pattern. For something like `&(a..=b)`, there

0 commit comments

Comments
 (0)