From 829aae6a8d0a2ad86d4a29bd6fad261421fc5300 Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Thu, 2 Nov 2023 19:29:52 +0000 Subject: [PATCH 1/2] add a go language reference link to package doc --- doc.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc.go b/doc.go index 8435e5d..a745247 100644 --- a/doc.go +++ b/doc.go @@ -10,8 +10,8 @@ The Go [language spec] does not have an explicit definition for enums. For the purpose of this analyzer, and by convention, an enum type is any named type that: - - has underlying type float, string, or integer (includes byte and rune); - and + - has an [underlying type] of float, string, or integer (includes byte + and rune); and - has at least one constant of its type defined in the same [block]. In the example below, Biome is an enum type. The three constants are its @@ -209,6 +209,7 @@ To ignore specific types, specify the -ignore-enum-types flag: exhaustive -ignore-enum-types '^time\.Duration$|^example\.org/measure\.Unit$' [language spec]: https://golang.org/ref/spec +[underlying type]: https://golang.org/ref/spec#Underlying_types [block]: https://golang.org/ref/spec#Blocks [BasicKind]: https://pkg.go.dev/go/types#BasicKind */ From 879cdefda87c79b5316d04ebbc9049c38e340e8c Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Wed, 2 Aug 2023 15:51:42 +0000 Subject: [PATCH 2/2] use go1.21 ast.IsGenerated when available --- comment.go | 33 --------------------------------- comment_go121.go | 11 +++++++++++ comment_pre_go121.go | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 comment_go121.go create mode 100644 comment_pre_go121.go diff --git a/comment.go b/comment.go index cc84bea..5da2dd0 100644 --- a/comment.go +++ b/comment.go @@ -3,42 +3,9 @@ package exhaustive import ( "go/ast" "go/token" - "regexp" "strings" ) -// For definition of generated file see: -// http://golang.org/s/generatedcode - -var generatedCodeRe = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`) - -func isGeneratedFile(file *ast.File) bool { - // NOTE: file.Comments includes file.Doc as well, so no need - // to separately check file.Doc. - for _, c := range file.Comments { - for _, cc := range c.List { - // This check handles the "must appear before the first - // non-comment, non-blank text in the file" requirement. - // - // According to https://golang.org/ref/spec#Source_file_organization - // the package clause is the first element in a file, which - // should make it the first non-comment, non-blank text. - if c.Pos() >= file.Package { - return false - } - // According to the docs: - // '\r' has been removed. - // '\n' has been removed for //-style comments - // This has also been manually verified. - if generatedCodeRe.MatchString(cc.Text) { - return true - } - } - } - - return false -} - const ( ignoreComment = "//exhaustive:ignore" enforceComment = "//exhaustive:enforce" diff --git a/comment_go121.go b/comment_go121.go new file mode 100644 index 0000000..a7bbc88 --- /dev/null +++ b/comment_go121.go @@ -0,0 +1,11 @@ +//go:build go1.21 + +package exhaustive + +import ( + "go/ast" +) + +func isGeneratedFile(file *ast.File) bool { + return ast.IsGenerated(file) +} diff --git a/comment_pre_go121.go b/comment_pre_go121.go new file mode 100644 index 0000000..28d2ed4 --- /dev/null +++ b/comment_pre_go121.go @@ -0,0 +1,27 @@ +//go:build !go1.21 + +package exhaustive + +import ( + "go/ast" + "regexp" +) + +// For definition of generated file see: +// http://golang.org/s/generatedcode + +var generatedCodeRe = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`) + +func isGeneratedFile(file *ast.File) bool { + for _, c := range file.Comments { + for _, cc := range c.List { + if cc.Pos() > file.Package { + break + } + if generatedCodeRe.MatchString(cc.Text) { + return true + } + } + } + return false +}