-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathdoc.go
296 lines (256 loc) · 9.6 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Package stylecheck contains analyzes that enforce style rules.
// Most of the recommendations made are universally agreed upon by the wider Go community.
// Some analyzes, however, implement stricter rules that not everyone will agree with.
// In the context of Staticcheck, these analyzes are not enabled by default.
//
// For the most part it is recommended to follow the advice given by the analyzers that are enabled by default,
// but you may want to disable additional analyzes on a case by case basis.
package stylecheck
import "honnef.co/go/tools/analysis/lint"
var Docs = lint.Markdownify(map[string]*lint.RawDocumentation{
"ST1000": {
Title: `Incorrect or missing package comment`,
Text: `Packages must have a package comment that is formatted according to
the guidelines laid out in
https://github.com/golang/go/wiki/CodeReviewComments#package-comments.`,
Since: "2019.1",
NonDefault: true,
MergeIf: lint.MergeIfAny,
},
"ST1001": {
Title: `Dot imports are discouraged`,
Text: `Dot imports that aren't in external test packages are discouraged.
The \'dot_import_whitelist\' option can be used to whitelist certain
imports.
Quoting Go Code Review Comments:
> The \'import .\' form can be useful in tests that, due to circular
> dependencies, cannot be made part of the package being tested:
>
> package foo_test
>
> import (
> "bar/testutil" // also imports "foo"
> . "foo"
> )
>
> In this case, the test file cannot be in package foo because it
> uses \'bar/testutil\', which imports \'foo\'. So we use the \'import .\'
> form to let the file pretend to be part of package foo even though
> it is not. Except for this one case, do not use \'import .\' in your
> programs. It makes the programs much harder to read because it is
> unclear whether a name like \'Quux\' is a top-level identifier in the
> current package or in an imported package.`,
Since: "2019.1",
Options: []string{"dot_import_whitelist"},
MergeIf: lint.MergeIfAny,
},
"ST1003": {
Title: `Poorly chosen identifier`,
Text: `Identifiers, such as variable and package names, follow certain rules.
See the following links for details:
- https://golang.org/doc/effective_go.html#package-names
- https://golang.org/doc/effective_go.html#mixed-caps
- https://github.com/golang/go/wiki/CodeReviewComments#initialisms
- https://github.com/golang/go/wiki/CodeReviewComments#variable-names`,
Since: "2019.1",
NonDefault: true,
Options: []string{"initialisms"},
MergeIf: lint.MergeIfAny,
},
"ST1005": {
Title: `Incorrectly formatted error string`,
Text: `Error strings follow a set of guidelines to ensure uniformity and good
composability.
Quoting Go Code Review Comments:
> Error strings should not be capitalized (unless beginning with
> proper nouns or acronyms) or end with punctuation, since they are
> usually printed following other context. That is, use
> \'fmt.Errorf("something bad")\' not \'fmt.Errorf("Something bad")\', so
> that \'log.Printf("Reading %s: %v", filename, err)\' formats without a
> spurious capital letter mid-message.`,
Since: "2019.1",
MergeIf: lint.MergeIfAny,
},
"ST1006": {
Title: `Poorly chosen receiver name`,
Text: `Quoting Go Code Review Comments:
> The name of a method's receiver should be a reflection of its
> identity; often a one or two letter abbreviation of its type
> suffices (such as "c" or "cl" for "Client"). Don't use generic
> names such as "me", "this" or "self", identifiers typical of
> object-oriented languages that place more emphasis on methods as
> opposed to functions. The name need not be as descriptive as that
> of a method argument, as its role is obvious and serves no
> documentary purpose. It can be very short as it will appear on
> almost every line of every method of the type; familiarity admits
> brevity. Be consistent, too: if you call the receiver "c" in one
> method, don't call it "cl" in another.`,
Since: "2019.1",
MergeIf: lint.MergeIfAny,
},
"ST1008": {
Title: `A function's error value should be its last return value`,
Text: `A function's error value should be its last return value.`,
Since: `2019.1`,
MergeIf: lint.MergeIfAny,
},
"ST1011": {
Title: `Poorly chosen name for variable of type \'time.Duration\'`,
Text: `\'time.Duration\' values represent an amount of time, which is represented
as a count of nanoseconds. An expression like \'5 * time.Microsecond\'
yields the value \'5000\'. It is therefore not appropriate to suffix a
variable of type \'time.Duration\' with any time unit, such as \'Msec\' or
\'Milli\'.`,
Since: `2019.1`,
MergeIf: lint.MergeIfAny,
},
"ST1012": {
Title: `Poorly chosen name for error variable`,
Text: `Error variables that are part of an API should be called \'errFoo\' or
\'ErrFoo\'.`,
Since: "2019.1",
MergeIf: lint.MergeIfAny,
},
"ST1013": {
Title: `Should use constants for HTTP error codes, not magic numbers`,
Text: `HTTP has a tremendous number of status codes. While some of those are
well known (200, 400, 404, 500), most of them are not. The \'net/http\'
package provides constants for all status codes that are part of the
various specifications. It is recommended to use these constants
instead of hard-coding magic numbers, to vastly improve the
readability of your code.`,
Since: "2019.1",
Options: []string{"http_status_code_whitelist"},
MergeIf: lint.MergeIfAny,
},
"ST1015": {
Title: `A switch's default case should be the first or last case`,
Since: "2019.1",
MergeIf: lint.MergeIfAny,
},
"ST1016": {
Title: `Use consistent method receiver names`,
Since: "2019.1",
NonDefault: true,
MergeIf: lint.MergeIfAny,
},
"ST1017": {
Title: `Don't use Yoda conditions`,
Text: `Yoda conditions are conditions of the kind \"if 42 == x\", where the
literal is on the left side of the comparison. These are a common
idiom in languages in which assignment is an expression, to avoid bugs
of the kind \"if (x = 42)\". In Go, which doesn't allow for this kind of
bug, we prefer the more idiomatic \"if x == 42\".`,
Since: "2019.2",
MergeIf: lint.MergeIfAny,
},
"ST1018": {
Title: `Avoid zero-width and control characters in string literals`,
Since: "2019.2",
MergeIf: lint.MergeIfAny,
},
"ST1019": {
Title: `Importing the same package multiple times`,
Text: `Go allows importing the same package multiple times, as long as
different import aliases are being used. That is, the following
bit of code is valid:
import (
"fmt"
fumpt "fmt"
format "fmt"
_ "fmt"
)
However, this is very rarely done on purpose. Usually, it is a
sign of code that got refactored, accidentally adding duplicate
import statements. It is also a rarely known feature, which may
contribute to confusion.
Do note that sometimes, this feature may be used
intentionally (see for example
https://github.com/golang/go/commit/3409ce39bfd7584523b7a8c150a310cea92d879d)
– if you want to allow this pattern in your code base, you're
advised to disable this check.`,
Since: "2020.1",
MergeIf: lint.MergeIfAny,
},
"ST1020": {
Title: "The documentation of an exported function should start with the function's name",
Text: `Doc comments work best as complete sentences, which
allow a wide variety of automated presentations. The first sentence
should be a one-sentence summary that starts with the name being
declared.
If every doc comment begins with the name of the item it describes,
you can use the \'doc\' subcommand of the \'go\' tool and run the output
through grep.
See https://golang.org/doc/effective_go.html#commentary for more
information on how to write good documentation.`,
Since: "2020.1",
NonDefault: true,
MergeIf: lint.MergeIfAny,
},
"ST1021": {
Title: "The documentation of an exported type should start with type's name",
Text: `Doc comments work best as complete sentences, which
allow a wide variety of automated presentations. The first sentence
should be a one-sentence summary that starts with the name being
declared.
If every doc comment begins with the name of the item it describes,
you can use the \'doc\' subcommand of the \'go\' tool and run the output
through grep.
See https://golang.org/doc/effective_go.html#commentary for more
information on how to write good documentation.`,
Since: "2020.1",
NonDefault: true,
MergeIf: lint.MergeIfAny,
},
"ST1022": {
Title: "The documentation of an exported variable or constant should start with variable's name",
Text: `Doc comments work best as complete sentences, which
allow a wide variety of automated presentations. The first sentence
should be a one-sentence summary that starts with the name being
declared.
If every doc comment begins with the name of the item it describes,
you can use the \'doc\' subcommand of the \'go\' tool and run the output
through grep.
See https://golang.org/doc/effective_go.html#commentary for more
information on how to write good documentation.`,
Since: "2020.1",
NonDefault: true,
MergeIf: lint.MergeIfAny,
},
"ST1023": {
Title: "Redundant type in variable declaration",
Since: "2021.1",
NonDefault: true,
MergeIf: lint.MergeIfAll,
},
"ST1024": {
Title: "Prefer Early loop returns",
Since: "2022.12",
Text: `if a for loop has only one if condition in it and
it is longer than 10 statements then its better to have a simple
if with a break early on instead.
Instead of
for {
if n > 5 {
...
...
...
(more than 10 lines)
}
}
use
for {
if n <= 5 {
break
}
...
...
...
(more than 10 lines)
}
}
This reduces the indent which makes it more readable.`,
NonDefault: true,
MergeIf: lint.MergeIfAll,
},
})