Skip to content

feat: add iotamixing linter #5966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ linters:
- ineffassign
- interfacebloat
- intrange
- iotamixing
- ireturn
- lll
- loggercheck
Expand Down Expand Up @@ -184,6 +185,7 @@ linters:
- ineffassign
- interfacebloat
- intrange
- iotamixing
- ireturn
- lll
- loggercheck
Expand Down Expand Up @@ -2032,6 +2034,11 @@ linters-settings:
# Default: 10
max: 5

iotamixing:
# Whether to report individual consts rather than just the const block.
# Default: false
report-individual: true

ireturn:
# List of interfaces to allow.
# Lists of the keywords and regular expressions matched to interface or package names can be used.
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module github.com/golangci/golangci-lint

go 1.23.0
go 1.23.3

toolchain go1.23.11

require (
4d63.com/gocheckcompilerdirectives v1.3.0
4d63.com/gochecknoglobals v0.2.2
github.com/4meepo/tagalign v1.4.2
github.com/Abirdcfly/dupword v0.1.3
github.com/AdminBenni/iota-mixing v0.0.5
github.com/Antonboom/errname v1.0.0
github.com/Antonboom/nilnil v1.0.1
github.com/Antonboom/testifylint v1.5.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@
"ineffassign",
"interfacebloat",
"intrange",
"iotamixing",
"ireturn",
"lll",
"loggercheck",
Expand Down Expand Up @@ -2084,6 +2085,17 @@
}
}
},
"iotamixing": {
"type": "object",
"additionalProperties": false,
"properties": {
"report-individual": {
"description": "Whether to report individual consts rather than just the const block.",
"type": "boolean",
"default": false
}
}
},
"ireturn": {
"type": "object",
"additionalProperties": false,
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type LintersSettings struct {
ImportAs ImportAsSettings
Inamedparam INamedParamSettings
InterfaceBloat InterfaceBloatSettings
IotaMixing IotaMixingSettings
Ireturn IreturnSettings
Lll LllSettings
LoggerCheck LoggerCheckSettings
Expand Down Expand Up @@ -704,6 +705,10 @@ type InterfaceBloatSettings struct {
Max int `mapstructure:"max"`
}

type IotaMixingSettings struct {
ReportIndividual bool `mapstructure:"report-individual"`
}

type IreturnSettings struct {
Allow []string `mapstructure:"allow"`
Reject []string `mapstructure:"reject"`
Expand Down
29 changes: 29 additions & 0 deletions pkg/golinters/iotamixing/iotamixing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package iotamixing

import (
"golang.org/x/tools/go/analysis"

"github.com/AdminBenni/iota-mixing/pkg/analyzer"
"github.com/AdminBenni/iota-mixing/pkg/analyzer/flags"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func New(settings *config.IotaMixingSettings) *goanalysis.Linter {
a := analyzer.GetIotaMixingAnalyzer()

flags.SetupFlags(&a.Flags)

cfg := map[string]map[string]any{}
if settings != nil {
cfg[a.Name] = map[string]any{flags.ReportIndividualFlagName: settings.ReportIndividual}
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
11 changes: 11 additions & 0 deletions pkg/golinters/iotamixing/iotamixing_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package iotamixing

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
50 changes: 50 additions & 0 deletions pkg/golinters/iotamixing/testdata/iotamixing-report-individual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//golangcitest:args -Eiotamixing
//golangcitest:config_path testdata/iotamixing-report-individual.yml
package testdata

import "fmt"

const (
InvalidPerIndividualIotaDeclAboveAnything = "anything" // want "InvalidPerIndividualIotaDeclAboveAnything is a const with r-val in same const block as iota. keep iotas in separate const blocks"
InvalidPerIndividualIotaDeclAboveNotZero = iota
InvalidPerIndividualIotaDeclAboveNotOne
InvalidPerIndividualIotaDeclAboveNotTwo
)

const (
InvalidPerIndividualIotaDeclBelowZero = iota
InvalidPerIndividualIotaDeclBelowOne
InvalidPerIndividualIotaDeclBelowTwo
InvalidPerIndividualIotaDeclBelowAnything = "anything" // want "InvalidPerIndividualIotaDeclBelowAnything is a const with r-val in same const block as iota. keep iotas in separate const blocks"
)

const (
InvalidPerIndividualIotaDeclBetweenZero = iota
InvalidPerIndividualIotaDeclBetweenOne
InvalidPerIndividualIotaDeclBetweenAnything = "anything" // want "InvalidPerIndividualIotaDeclBetweenAnything is a const with r-val in same const block as iota. keep iotas in separate const blocks"
InvalidPerIndividualIotaDeclBetweenNotTwo
)

const (
InvalidPerIndividualIotaDeclMultipleAbove = "above" // want "InvalidPerIndividualIotaDeclMultipleAbove is a const with r-val in same const block as iota. keep iotas in separate const blocks"
InvalidPerIndividualIotaDeclMultipleNotZero = iota
InvalidPerIndividualIotaDeclMultipleNotOne
InvalidPerIndividualIotaDeclMultipleBetween = "between" // want "InvalidPerIndividualIotaDeclMultipleBetween is a const with r-val in same const block as iota. keep iotas in separate const blocks"
InvalidPerIndividualIotaDeclMultipleNotTwo
InvalidPerIndividualIotaDeclMultipleBelow = "below" // want "InvalidPerIndividualIotaDeclMultipleBelow is a const with r-val in same const block as iota. keep iotas in separate const blocks"
)

const (
ValidPerIndividualIotaZero = iota
ValidPerIndividualIotaOne
ValidPerIndividualIotaTwo
)

const (
ValidPerIndividualRegularSomething = "something"
ValidPerIndividualRegularAnything = "anything"
)

func _() {
fmt.Println("using the std import so goland doesn't nuke it")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
iotamixing:
report-individual: true
55 changes: 55 additions & 0 deletions pkg/golinters/iotamixing/testdata/iotamixing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//golangcitest:args -Eiotamixing
package testdata

import "fmt"

// iota mixing in const block containing an iota and r-val declared above.
const ( // want "iota mixing. keep iotas in separate blocks to consts with r-val"
InvalidPerBlockIotaDeclAboveAnything = "anything"
InvalidPerBlockIotaDeclAboveNotZero = iota
InvalidPerBlockIotaDeclAboveNotOne
InvalidPerBlockIotaDeclAboveNotTwo
)

// iota mixing in const block containing an iota and r-val declared below.
const ( // want "iota mixing. keep iotas in separate blocks to consts with r-val"
InvalidPerBlockIotaDeclBelowZero = iota
InvalidPerBlockIotaDeclBelowOne
InvalidPerBlockIotaDeclBelowTwo
InvalidPerBlockIotaDeclBelowAnything = "anything"
)

// iota mixing in const block containing an iota and r-val declared between consts.
const ( // want "iota mixing. keep iotas in separate blocks to consts with r-val"
InvalidPerBlockIotaDeclBetweenZero = iota
InvalidPerBlockIotaDeclBetweenOne
InvalidPerBlockIotaDeclBetweenAnything = "anything"
InvalidPerBlockIotaDeclBetweenNotTwo
)

// iota mixing in const block containing an iota and r-vals declared above, between, and below consts.
const ( // want "iota mixing. keep iotas in separate blocks to consts with r-val"
InvalidPerBlockIotaDeclMultipleAbove = "above"
InvalidPerBlockIotaDeclMultipleNotZero = iota
InvalidPerBlockIotaDeclMultipleNotOne
InvalidPerBlockIotaDeclMultipleBetween = "between"
InvalidPerBlockIotaDeclMultipleNotTwo
InvalidPerBlockIotaDeclMultipleBelow = "below"
)

// no iota mixing in a const block containing an iota and no r-vals.
const (
ValidPerBlockIotaZero = iota
ValidPerBlockIotaOne
ValidPerBlockIotaTwo
)

// no iota mixing in a const block containing r-vals and no iota.
const (
ValidPerBlockRegularSomething = "something"
ValidPerBlockRegularAnything = "anything"
)

func _() {
fmt.Println("using the std import so goland doesn't nuke it")
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/ineffassign"
"github.com/golangci/golangci-lint/pkg/golinters/interfacebloat"
"github.com/golangci/golangci-lint/pkg/golinters/intrange"
"github.com/golangci/golangci-lint/pkg/golinters/iotamixing"
"github.com/golangci/golangci-lint/pkg/golinters/ireturn"
"github.com/golangci/golangci-lint/pkg/golinters/lll"
"github.com/golangci/golangci-lint/pkg/golinters/loggercheck"
Expand Down Expand Up @@ -536,6 +537,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithURL("https://github.com/ckaznocha/intrange").
WithNoopFallback(cfg, linter.IsGoLowerThanGo122()),

linter.NewConfig(iotamixing.New(&cfg.LintersSettings.IotaMixing)).
WithSince("v2.4.0").
WithPresets(linter.PresetBugs).
WithURL("github.com/AdminBenni/iota-mixing"),

linter.NewConfig(ireturn.New(&cfg.LintersSettings.Ireturn)).
WithSince("v1.43.0").
WithPresets(linter.PresetStyle).
Expand Down