Skip to content

Commit c498b7b

Browse files
committed
format: adjust rxCommentDirective per https://go.dev/doc/comment#syntax
The page shows slightly different regular expressions than the ones we used. There was no good reason for that difference, so align them. One exception does exist: go-sumtype uses dashes, but its directive came before Go added the documented convention in 2022, so it gets a pass. In our defense, we added this before upstream started documenting a convention, as we prompted them to add that documentation via https://go.dev/issue/issues back in 2021.
1 parent 2d501b8 commit c498b7b

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

format/format.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (f *fumpter) lineEnd(line int) token.Pos {
309309
return f.file.LineStart(line+1) - 1
310310
}
311311

312-
// rxCommentDirective covers all common Go comment directives:
312+
// rxCommentDirective covers all common Go comment directives, such as:
313313
//
314314
// //go: | standard Go directives, like go:noinline
315315
// //some-words: | similar to the syntax above, like lint:ignore or go-sumtype:decl
@@ -321,15 +321,16 @@ func (f *fumpter) lineEnd(line int) token.Pos {
321321
// //#nosec | #nosec directive for gosec
322322
// //NOSONAR | NOSONAR directive for SonarQube
323323
// //sys(nb)? | syscall function wrapper prototypes
324-
//
325-
// Note that the "some-words:" matching expects a letter afterward, such as
326-
// "go:generate", to prevent matching false positives like "https://site".
327324
var rxCommentDirective = regexp.MustCompile(
328325
`^(?:` +
329-
`[a-z-]+:[a-z]+` +
330-
`|export\b` +
331-
`|extern\b` +
332-
`|line\b` +
326+
// Patterns directly from https://go.dev/doc/comment#syntax.
327+
// Note that we adjust the first pattern to allow for //go-sumtype:decl,
328+
// which is a tool that existed before the Go convention was documented.
329+
`[a-z0-9-]+:[a-z0-9]` +
330+
`|export ` +
331+
`|extern ` +
332+
`|line ` +
333+
// Third-party patterns; we generally assume they end with a word boundary.
333334
`|no(?:inspection|lint)\b` +
334335
`|#nosec\b` +
335336
`|NOSONAR\b` +

0 commit comments

Comments
 (0)