Skip to content

Commit 0afba4f

Browse files
authored
refactor: enforce map and slice style (#1131)
1 parent 1b4440c commit 0afba4f

File tree

12 files changed

+22
-18
lines changed

12 files changed

+22
-18
lines changed

formatter/checkstyle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (*Checkstyle) Format(failures <-chan lint.Failure, config lint.Config) (str
4545
}
4646
fn := failure.GetFilename()
4747
if issues[fn] == nil {
48-
issues[fn] = make([]issue, 0)
48+
issues[fn] = []issue{}
4949
}
5050
issues[fn] = append(issues[fn], iss)
5151
}

formatter/stylish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (*Stylish) Format(failures <-chan lint.Failure, config lint.Config) (string
5151
ps = "problem"
5252
}
5353

54-
fileReport := make(map[string][][]string)
54+
fileReport := map[string][][]string{}
5555

5656
for _, row := range result {
5757
if _, ok := fileReport[row[0]]; !ok {

lint/file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ const (
140140
var re = regexp.MustCompile(directiveRE)
141141

142142
func (f *File) disabledIntervals(rules []Rule, mustSpecifyDisableReason bool, failures chan Failure) disabledIntervalsMap {
143-
enabledDisabledRulesMap := make(map[string][]enableDisableConfig)
143+
enabledDisabledRulesMap := map[string][]enableDisableConfig{}
144144

145145
getEnabledDisabledIntervals := func() disabledIntervalsMap {
146-
result := make(disabledIntervalsMap)
146+
result := disabledIntervalsMap{}
147147

148148
for ruleName, disabledArr := range enabledDisabledRulesMap {
149149
ruleResult := []DisabledInterval{}

lint/linter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var (
6363
func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-chan Failure, error) {
6464
failures := make(chan Failure)
6565

66-
perModVersions := make(map[string]*goversion.Version)
66+
perModVersions := map[string]*goversion.Version{}
6767
perPkgVersions := make([]*goversion.Version, len(packages))
6868
for n, files := range packages {
6969
if len(files) == 0 {

lint/package.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ func (p *Package) TypeCheck() error {
9999
Importer: importer.Default(),
100100
}
101101
info := &types.Info{
102-
Types: make(map[ast.Expr]types.TypeAndValue),
103-
Defs: make(map[*ast.Ident]types.Object),
104-
Uses: make(map[*ast.Ident]types.Object),
105-
Scopes: make(map[ast.Node]*types.Scope),
102+
Types: map[ast.Expr]types.TypeAndValue{},
103+
Defs: map[*ast.Ident]types.Object{},
104+
Uses: map[*ast.Ident]types.Object{},
105+
Scopes: map[ast.Node]*types.Scope{},
106106
}
107107
var anyFile *File
108108
var astFiles []*ast.File
@@ -162,7 +162,7 @@ func (w *walker) Visit(n ast.Node) ast.Visitor {
162162
}
163163

164164
func (p *Package) scanSortable() {
165-
p.sortable = make(map[string]bool)
165+
p.sortable = map[string]bool{}
166166

167167
// bitfield for which methods exist on each type.
168168
const (
@@ -171,7 +171,7 @@ func (p *Package) scanSortable() {
171171
bfSwap
172172
)
173173
nmap := map[string]int{"Len": bfLen, "Less": bfLess, "Swap": bfSwap}
174-
has := make(map[string]int)
174+
has := map[string]int{}
175175
for _, f := range p.files {
176176
ast.Walk(&walker{nmap, has}, f.AST)
177177
}

revive.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ warningCode = 1
1414
[rule.dot-imports]
1515
[rule.empty-block]
1616
[rule.empty-lines]
17+
[rule.enforce-map-style]
18+
arguments = ["literal"]
19+
[rule.enforce-slice-style]
20+
arguments = ["literal"]
1721
[rule.error-naming]
1822
[rule.error-return]
1923
[rule.error-strings]

rule/add_constant.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin
5252

5353
w := &lintAddConstantRule{
5454
onFailure: onFailure,
55-
strLits: make(map[string]int),
55+
strLits: map[string]int{},
5656
strLitLimit: r.strLitLimit,
5757
allowList: r.allowList,
5858
ignoreFunctions: r.ignoreFunctions,
59-
structTags: make(map[*ast.BasicLit]struct{}),
59+
structTags: map[*ast.BasicLit]struct{}{},
6060
}
6161

6262
ast.Walk(w, file.AST)

rule/confusing_naming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (ps *packages) methodNames(lp *lint.Package) pkgMethods {
3535
}
3636
}
3737

38-
pkgm := pkgMethods{pkg: lp, methods: make(map[string]map[string]*referenceMethod), mu: &sync.Mutex{}}
38+
pkgm := pkgMethods{pkg: lp, methods: map[string]map[string]*referenceMethod{}, mu: &sync.Mutex{}}
3939
ps.pkgs = append(ps.pkgs, pkgm)
4040

4141
return pkgm

rule/dot_imports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (*DotImportsRule) Name() string {
4242
}
4343

4444
func (r *DotImportsRule) configure(arguments lint.Arguments) {
45-
r.allowedPackages = make(allowPackages)
45+
r.allowedPackages = allowPackages{}
4646
if len(arguments) == 0 {
4747
return
4848
}

rule/empty_block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
1717
failures = append(failures, failure)
1818
}
1919

20-
w := lintEmptyBlock{make(map[*ast.BlockStmt]bool), onFailure}
20+
w := lintEmptyBlock{map[*ast.BlockStmt]bool{}, onFailure}
2121
ast.Walk(w, file.AST)
2222
return failures
2323
}

rule/exported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (r *ExportedRule) Apply(file *lint.File, args lint.Arguments) []lint.Failur
112112
onFailure: func(failure lint.Failure) {
113113
failures = append(failures, failure)
114114
},
115-
genDeclMissingComments: make(map[*ast.GenDecl]bool),
115+
genDeclMissingComments: map[*ast.GenDecl]bool{},
116116
stuttersMsg: r.stuttersMsg,
117117
disabledChecks: r.disabledChecks,
118118
}

test/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func parseInstructions(t *testing.T, filename string, src []byte) []instruction
159159
}
160160
if line == "OK" && ins == nil {
161161
// so our return value will be non-nil
162-
ins = make([]instruction, 0)
162+
ins = []instruction{}
163163
continue
164164
}
165165
switch extractDataMode(line) {

0 commit comments

Comments
 (0)