Skip to content

Ban case blocks with just "break", top level break - #64153

Open
Jake Bailey (jakebailey) wants to merge 8 commits into
microsoft:mainfrom
jakebailey:emptycase-ban-redundant-break
Open

Ban case blocks with just "break", top level break#64153
Jake Bailey (jakebailey) wants to merge 8 commits into
microsoft:mainfrom
jakebailey:emptycase-ban-redundant-break

Conversation

@jakebailey

Copy link
Copy Markdown
Member

Noticed this while reviewing code on another branch. In Go, you don't need to say "break", but some case blocks said just "break" like it's JS without the required commentary. Ban those cases. Also, drop any extra top-level breaks, which are redundant.

The emptycase customlint rule previously only flagged switch/select
cases with an empty body. Extend it to also flag cases whose body is
solely a bare break statement, since that break is redundant in Go
(switch/select cases do not fall through) and provides no more
information than an empty case. As with empty cases, a case is
exempted if it has an explanatory comment.

Also fix the handful of existing bare break-only cases in the
compiler and language server that were newly caught by this rule, by
adding a short comment explaining the intentional no-op.
Rename the emptycase analyzer to casebody now that it enforces both
empty case documentation and redundant top-level break statements.
Report every direct bare break in a switch or select case, even when
the case has other statements or comments.

Replace existing redundant breaks with explanatory comments where the
case becomes empty, and add switch and select coverage for lone,
commented, trailing, and nested breaks.
Copilot AI balanced review requested due to automatic review settings September 3, 2026 19:20
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 3, 2026
@typescript-automation typescript-automation Bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Sep 3, 2026
Include type switches in the casebody analyzer so empty cases and
direct redundant break statements are handled consistently across all
Go switch forms. Add type-switch coverage and document the existing
intentionally ignored fourslash case.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The analyzer incorrectly reports non-final case-level breaks whose removal would change runtime behavior.

Pull request overview

Renames and extends the custom case-body lint rule while removing redundant Go break statements.

Changes:

  • Adds detection for redundant case-level breaks and type-switch cases.
  • Renames emptycase to casebody and expands golden tests.
  • Removes redundant breaks or documents intentionally empty cases.
File summaries
File Description
tsc/internal/transformers/tstransforms/typeserializer.go Documents empty serialization cases.
tsc/internal/transformers/estransforms/namedevaluation.go Removes redundant breaks.
tsc/internal/printer/utilities.go Documents an empty infer-type case.
tsc/internal/printer/printer.go Removes or documents redundant breaks.
tsc/internal/ls/folding.go Removes redundant breaks.
tsc/internal/ls/autoimport/util.go Documents select fallback behavior.
tsc/internal/fourslash/fourslash.go Documents intentionally ignored edit ranges.
tsc/internal/format/span.go Removes redundant breaks.
tsc/internal/format/scanner.go Documents normal scanning behavior.
tools/customlint/testdata/emptycase/emptycase.go.golden Removes the superseded golden file.
tools/customlint/testdata/casebody/casebody.go.golden Adds expanded lint expectations.
tools/customlint/testdata/casebody/casebody.go Adds break and type-switch cases.
tools/customlint/plugin.go Registers the renamed analyzer.
tools/customlint/casebody.go Expands the analyzer to detect breaks and type switches.
Review details

Suppressed comments (1)

tools/customlint/casebody.go:82

  • This reports every direct break, but a break before a later statement is not redundant: Go permits that statement to be unreachable, and removing the break causes it to execute. Restrict this diagnostic to an unlabeled break that is the final statement in the case body.
  • Files reviewed: 14/14 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Add regression coverage confirming that the casebody analyzer permits a
direct labeled break which exits an enclosing loop.
Report the first statement following a direct break in a switch,
type-switch, or select case body. This also ensures labeled breaks are
only accepted when they terminate the case body.
Continue scanning a case body after reporting its first unreachable
statement so later unlabeled breaks are still rejected. Add ordinary,
type-switch, and select coverage for code following a break.
Describe statements after a direct break as structurally disallowed
rather than necessarily unreachable, since a following label may be a
goto target. Add coverage showing that the layout remains banned even
when the labeled statement is otherwise reachable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

2 participants