Skip to content

Warn about a forvalues range that runs no times, such as 3/1, and suggest the countdown - #407

Merged
jbearak merged 1 commit into
mainfrom
t3/warn-empty-forvalues-range
Nov 7, 2026
Merged

jbearak merged 1 commit into
mainfrom
t3/warn-empty-forvalues-range

Conversation

@jbearak

@jbearak jbearak commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

forvalues j = 3/1 { ... } runs zero times in Stata. a/b always counts up by 1, so with a > b there is nothing to count, and Stata says nothing. It is almost always a countdown meant as 3(-1)1. Sight didn't flag it, so neither users nor a downstream translation tool reading sight check --format json could see the problem.

This adds EMPTY_FORVALUES_RANGE (warning, severity key emptyForvaluesRange) for a forvalues range whose bounds and step are all numeric literals and which runs no times:

  • a/b with a > b:
    `forvalues` range `3/1` runs no times (`a/b` counts up); did you mean `3(-1)1`?
  • a(s)b whose step points away from b:
    `forvalues` range `1(-1)3` runs no times (step `-1` counts away from `3`)
  • a t to b (or a t : b) whose implied step t - a points away from b:
    `forvalues` range `3 4 to 1` runs no times (the step from `3` to `4` counts away from `1`)

The range covers the range expression after =. Ranges that run (1/3, 3/3, 3(-1)1, 10 8 to 3), ranges with a macro or expression, and a zero step (which Stata rejects with "increment may not be zero") are not reported. sight: ignore and sight: ignore-next suppress it like the other logic warnings. Sight has no code actions, so there is no quick fix; the message carries the suggested countdown. Documented in docs/diagnostics.md and docs/configuration.md.

Proof: StataMP 18 runs 3/1, 1(-1)3, 3(1)1, 3 4 to 1 and 3 4 : 1 zero times and 3/3 once. New unit, provider, and sight check --format json tests pass, the gates pass (9864 tests, 0 fail), and sight check over Stata's 3,780 base ados (127 literal ranges) reports none.

Reviewed with sol-loop: 1 round with gpt-6.1-sol. Sol verdict: approved

Created with Claude Opus 5.5 in Claude Code (T3 Code); reviewed by gpt-6.1-sol in Codex.

Summary by CodeRabbit

  • New Features
    • Added a diagnostic for numeric-literal forvalues ranges that run zero times. It supports slash, stepped, and to/colon forms and defaults to warning severity.
    • Added a setting to choose the diagnostic severity: error, warning, information, hint, or off.
  • Documentation
    • Documented the diagnostic, supported cases, exclusions, and configuration options.

Add EMPTY_FORVALUES_RANGE (warning, severity key emptyForvaluesRange)
for a forvalues range whose bounds and step are all numeric literals
and which runs zero times. Stata gives no message for these loops.

- `a/b` with a > b: `a/b` always counts up by 1, so the message
  suggests the countdown `a(-1)b`.
- `a(s)b` and `a t to b` / `a t : b` whose step points away from b.

Ranges with macros or expressions, and a zero step (which Stata
rejects with "increment may not be zero"), are not reported. The
diagnostic honors sight: ignore / ignore-next. Sight has no code
actions, so there is no quick fix.

Verified against StataMP 18: 3/1, 1(-1)3, 3(1)1, 3 4 to 1 and
3 4 : 1 run 0 times; 3/3 runs once.
@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds an EMPTY_FORVALUES_RANGE diagnostic for numeric-literal forvalues ranges that run zero times. The diagnostic supports configurable severity, is included in provider results outside embedded contexts, and is covered by analyzer, provider, and CLI tests.

Changes: Empty forvalues range diagnostic

Layer / File(s) Summary
Diagnostic code and severity configuration
src/types/index.ts, src/config-file/schema.ts, src/server-handlers.ts, src/utils/config-validator.ts, client/package.json, docs/configuration.md, docs/diagnostics.md, tests/property/config-schema-completeness.prop.test.ts
Adds the EMPTY_FORVALUES_RANGE code and emptyForvaluesRange severity setting. The setting accepts error, warning, information, hint, or off, and defaults to warning.
Parse and report empty ranges
src/providers/command-position.ts, src/providers/forvalues-range-diagnostics.ts, docs/diagnostics.md, tests/unit/forvalues-range-diagnostics.test.ts
Adds parsing for numeric-literal slash, stepped, and to or colon ranges. The analyzer reports ranges whose step points away from the endpoint. Tests cover supported forms, exclusions, severity settings, and suppression.
Provider integration and output validation
src/providers/diagnostics.ts, docs/diagnostics.md, tests/unit/forvalues-range-diagnostics.test.ts
The diagnostics provider adds analyzer results unless their start position is inside an embedded context. Documentation and tests cover suppression, provider diagnostics, and CLI JSON output.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Possibly related PRs

  • jbearak/sight#367: Changes lexer handling of tight /// sequences and statement boundaries, which affects how the analyzer handles ranges split across lines.
Merge Risk: Minimal · up to fdc38

This adds a new warning for forvalues ranges that run zero times, with a configurable severity and suppression support. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title describes the empty forvalues range warning and its countdown suggestion.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 7 functions across 9 files. (3 skipped: 3 unsupported.)

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 8dbfc2b0-beef-40aa-84a4-56e6fbb20dec
Commits

Reviewing files that changed from the base of the PR and between 14305cf and fdc38eb.

Files selected for processing: 12

  • client/package.json
  • docs/configuration.md
  • docs/diagnostics.md
  • src/config-file/schema.ts
  • src/providers/command-position.ts
  • src/providers/diagnostics.ts
  • src/providers/forvalues-range-diagnostics.ts
  • src/server-handlers.ts
  • src/types/index.ts
  • src/utils/config-validator.ts
  • tests/property/config-schema-completeness.prop.test.ts
  • tests/unit/forvalues-range-diagnostics.test.ts

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant