Skip to content

empty-block: skip bare for-range loops - #1710

Merged
alexandear merged 3 commits into
revive-lint:masterfrom
alexandear:fix/empty-block-channel-drain
Apr 24, 2026
Merged

alexandear merged 3 commits into
revive-lint:masterfrom
alexandear:fix/empty-block-channel-drain

Conversation

@alexandear

Copy link
Copy Markdown
Member

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.

Pull request overview

Adjusts the empty-block rule to avoid reporting empty bodies for bare for range loops, addressing false positives for common channel-draining patterns (issue #386).

Changes:

  • Update empty-block rule logic to skip empty RangeStmt bodies when both key and value are omitted.
  • Update testdata/empty_block.go fixtures to reflect the new behavior and add a helper returning a closed channel.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
rule/empty_block.go Skips reporting empty blocks for bare for range statements.
testdata/empty_block.go Updates fixtures for the new for range behavior and adds makeChan() helper.

Comment thread rule/empty_block.go Resolved
Comment thread testdata/empty_block.go Resolved

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.

Pull request overview

Updates the empty-block rule to avoid false positives on intentionally empty bare for range loops (commonly used for channel draining), and aligns fixtures/docs with the new behavior.

Changes:

  • Skip reporting for empty for range <expr> {} loops when both key and value are omitted.
  • Update testdata/empty_block.go to add coverage for range variants and to reflect the new β€œmust not match” expectations.
  • Update RULES_DESCRIPTIONS.md to document the newly skipped patterns and the resulting false-negative tradeoff.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
rule/empty_block.go Adds a bare RangeStmt exemption to prevent warnings on common channel-draining patterns.
testdata/empty_block.go Adjusts fixtures to validate the new exemption and keep a flagged non-bare range case.
RULES_DESCRIPTIONS.md Documents the exemption behavior and limitations/false negatives.

Comment thread rule/empty_block.go Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@alexandear
alexandear merged commit 4a89763 into revive-lint:master Apr 24, 2026
10 checks passed
@alexandear
alexandear deleted the fix/empty-block-channel-drain branch April 24, 2026 06:29
@alexandear alexandear mentioned this pull request Aug 21, 2026
merlimat added a commit to oxia-db/oxia that referenced this pull request Oct 9, 2026
### Problem

Since Go 1.27.2 was released, the `Build & Test` job fails at the
`golangci-lint` step on every PR. Then the `Test` step is skipped, so CI
runs no tests on PRs.

`actions/setup-go` reads `go 1.27` from `go.work` and installs the
latest 1.27.x. The Go 1.27.2 compiler writes export data in a new format
version, 5. That's a backport of
[go.dev/issue/81188](https://go.dev/issue/81188); 1.27.1 wrote version
4. golangci-lint v2.13.2 bundles `golang.org/x/tools` v0.49.0, which can
only read up to version 4. So type-checking fails on the first standard
library import of every package.

### Example

1. setup-go installs `go1.27.2 linux/amd64`. The last green run,
[37688466181](https://github.com/oxia-db/oxia/actions/runs/37688466181/job/113022265427)
on 2026-10-07, got `go1.27.1`.
2. `Install Go tools` builds golangci-lint v2.13.2.
3. `make lint` loads each package's dependencies from the export data
that Go 1.27.2 writes, in version 5. x/tools v0.49.0 rejects it, and all
five modules fail typecheck ([run
37805008553](https://github.com/oxia-db/oxia/actions/runs/37805008553/job/113957637137),
PR #1281):

   ```
cmd/common/parse/map.go:18:2: could not import strings
(/opt/hostedtoolcache/go/1.27.2/x64/src/strings/builder.go:8:2: could
not import internal/abi
(/opt/hostedtoolcache/go/1.27.2/x64/src/internal/abi/abi.go:8:2: could
not import internal/goarch (-: could not load export data: internal
error in importing "internal/goarch" (cannot decode "internal/goarch",
export data version 5 is greater than maximum supported version 4);
please report an issue))) (typecheck)
   ```

4. `Check git status if clean` and `Test` are skipped.

The same failure reproduces locally with `GOTOOLCHAIN=go1.27.2`. With
this change, step 2 builds v2.14.0, whose x/tools v0.50.0 reads version
5, and step 3 reports `0 issues.` for every module.

### Change

- `GOLANGCI_LINT_VERSION` goes from v2.13.2 to v2.14.0 in
`ci-build-test.yaml` and `ci-release.yaml`.
- v2.14.0 moves revive from v1.15.0 to v1.17.0. That changes three
things here:
- The new rules `multiline-if-init` and `marshal-receiver` flag 14
places in existing code. They're disabled in `.golangci.yaml`, the same
way #1298 handled the rules added by the previous bump.
- `empty-block` now skips bare `for range` loops (revive-lint/revive#1710),
so the `//nolint:revive` on the channel drain in `notifications.Close`
is no longer used. It's removed. Otherwise `fix: true` would delete it
during `make lint`, and the clean-tree check would fail.
- revive-lint/revive#1718 is fixed (revive-lint/revive#1719): an internal
failure in one rule no longer stops the remaining rules for that file.
#1309 disabled `inefficient-map-lookup` until this fix shipped, so the
second commit turns it back on (0 findings). That commit can be dropped
without affecting the rest.

### Testing

Tested with golangci-lint v2.14.0 built with go1.27.2, a fresh
`GOLANGCI_LINT_CACHE` and a private `TMPDIR` for each run:

- The CI loop over all five go.work modules with `--fix=false` reports 0
issues under go1.27.2 and under go1.27.1.
- Plain `make lint` (config `fix: true`) reports 0 issues and leaves the
tree unchanged.
- Flake check for `inefficient-map-lookup`: 30 runs of the full loop
with the rule enabled had 0 failures. As a control, the same loop on
`main` with only #1309 reverted, linted by v2.13.2 under go1.27.1,
failed 3 out of 10 runs with the old symptom: needed `//nolint:revive`
directives reported as unused.

---------

Signed-off-by: Matteo Merli <mmerli@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

empty-block: incorrectly warns on channel draining

3 participants