Skip to content

Commit 706c608

Browse files
committed
S1019: make(map[T]T) and make(map[T]T, 0) are not the same
Closes gh-1070 (cherry picked from commit db46980)
1 parent 2659add commit 706c608

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

simple/doc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ After:
248248
"S1019": {
249249
Title: "Simplify `make` call by omitting redundant arguments",
250250
Text: `The \'make\' function has default values for the length and capacity
251-
arguments. For channels and maps, the length defaults to zero.
252-
Additionally, for slices the capacity defaults to the length.`,
251+
arguments. For channels, the length defaults to zero, and for slices, the capacity defaults to the length.`,
253252
Since: "2017.1",
254253
},
255254

simple/lint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,8 @@ func CheckMakeLenCap(pass *analysis.Pass) (interface{}, error) {
11741174
if m, ok := code.Match(pass, checkMakeLenCapQ1, node); ok {
11751175
T := m.State["typ"].(ast.Expr)
11761176
size := m.State["size"].(ast.Node)
1177-
if _, ok := pass.TypesInfo.TypeOf(T).Underlying().(*types.Slice); ok {
1177+
switch pass.TypesInfo.TypeOf(T).Underlying().(type) {
1178+
case *types.Slice, *types.Map:
11781179
return
11791180
}
11801181
report.Report(pass, size, fmt.Sprintf("should use make(%s) instead", report.Render(pass, T)), report.FilterGenerated())

simple/testdata/src/CheckMakeLenCap/LintMakeLenCap.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ func fn() {
66
type s []int
77
type ch chan int
88
_ = make([]int, 1)
9-
_ = make([]int, 0) // length is mandatory for slices, don't suggest removal
10-
_ = make(s, 0) // length is mandatory for slices, don't suggest removal
11-
_ = make(chan int, c) // constant of 0 may be due to debugging, math or platform-specific code
12-
_ = make(chan int, 0) // want `should use make\(chan int\) instead`
13-
_ = make(ch, 0) // want `should use make\(ch\) instead`
14-
_ = make(map[int]int, 0) // want `should use make\(map\[int\]int\) instead`
15-
_ = make([]int, 1, 1) // want `should use make\(\[\]int, 1\) instead`
16-
_ = make([]int, x, x) // want `should use make\(\[\]int, x\) instead`
9+
_ = make([]int, 0) // length is mandatory for slices, don't suggest removal
10+
_ = make(s, 0) // length is mandatory for slices, don't suggest removal
11+
_ = make(chan int, c) // constant of 0 may be due to debugging, math or platform-specific code
12+
_ = make(chan int, 0) // want `should use make\(chan int\) instead`
13+
_ = make(ch, 0) // want `should use make\(ch\) instead`
14+
_ = make(map[int]int, 0)
15+
_ = make([]int, 1, 1) // want `should use make\(\[\]int, 1\) instead`
16+
_ = make([]int, x, x) // want `should use make\(\[\]int, x\) instead`
1717
_ = make([]int, 1, 2)
1818
_ = make([]int, x, y)
1919
}

0 commit comments

Comments
 (0)