Skip to content

Commit 166f5cc

Browse files
lambda47autofix-ci[bot]camc314
authored
fix(linter): fix no-fallthrough rule, when the default condition is not last (#12927)
In the previous implementation, if the default condition was not the last one, the order of cfg_ids was incorrect fixes #12793 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Cameron Clark <cameron.clark@hey.com>
1 parent 3ce27e9 commit 166f5cc

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

crates/oxc_linter/src/rules/eslint/no_fallthrough.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,12 @@ fn get_switch_semantic_cases(
437437
let cfg = ctx.cfg();
438438
let graph = cfg.graph();
439439
let has_default = switch.cases.iter().any(SwitchCase::is_default_case);
440-
let (tests, exit) = graph
440+
let (mut cfg_ids, tests, exit) = graph
441441
.edges_directed(node.cfg_id(), Direction::Outgoing)
442-
.fold((Vec::new(), None), |(mut conds, exit), it| {
442+
.fold((Vec::new(), Vec::new(), None), |(mut cfg_ids, mut conds, exit), it| {
443443
let target = it.target();
444444
if !matches!(it.weight(), EdgeType::Normal) {
445-
(conds, exit)
445+
(cfg_ids, conds, exit)
446446
} else if cfg
447447
.basic_block(target)
448448
.instructions()
@@ -466,22 +466,19 @@ fn get_switch_semantic_cases(
466466
})
467467
})
468468
.is_some_and(|it| it.consequent.is_empty() || it.consequent.iter().exactly_one().is_ok_and(|it| matches!(it, Statement::BlockStatement(b) if b.body.is_empty())));
469+
cfg_ids.push(target);
469470
conds.push((target, is_empty));
470-
(conds, exit)
471+
(cfg_ids, conds, exit)
471472
} else {
472-
(conds, Some(target))
473+
if has_default {
474+
cfg_ids.push(target);
475+
}
476+
(cfg_ids, conds, Some(target))
473477
}
474478
});
475479

476-
let mut cfg_ids: Vec<_> = tests.iter().rev().map(|it| it.0).collect();
477-
let (default, exit) = if has_default {
478-
if let Some(exit) = exit {
479-
cfg_ids.push(exit);
480-
}
481-
(exit, None)
482-
} else {
483-
(None, exit)
484-
};
480+
let (default, exit) = if has_default { (exit, None) } else { (None, exit) };
481+
cfg_ids.reverse();
485482
(cfg_ids, FxHashMap::from_iter(tests), default, exit)
486483
}
487484

@@ -535,6 +532,7 @@ fn test() {
535532
"switch (foo) { case 0: a(); \n// eslint-disable-next-line no-fallthrough\n case 1: }",
536533
None,
537534
),
535+
("switch(foo) { case 0: default: a(); break; case 1: b(); }", None),
538536
(
539537
"switch(foo) { case 0: a(); /* no break */ case 1: b(); }",
540538
Some(serde_json::json!([{

0 commit comments

Comments
 (0)