-
Notifications
You must be signed in to change notification settings - Fork 2.1k
chore!: Add sliceofpointers
custom linter
#3447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gmlewis
merged 10 commits into
google:master
from
gmlewis:add-no-pointer-to-string-slice
Jan 22, 2025
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ae66746
Add no-pointer-to-string-slice custom linter
gmlewis 4bb1671
Add sliceofpointers custom linter
gmlewis b22d8b8
Follow example at https://github.com/golangci/example-plugin-module-l…
gmlewis e07bebd
Add .custom-gcl.yml and go.mod
gmlewis 5b5afd5
Update .gitignore
gmlewis a6abae7
Merge branch 'master' into add-no-pointer-to-string-slice
gmlewis 7fa7c02
go mod tidy
gmlewis 6bb5c1e
Update .custom-gcl.yml
gmlewis 1a32bcd
Clean linter warnings
gmlewis 15174df
Update copyright notices
gmlewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add sliceofpointers custom linter
Signed-off-by: Glenn Lewis <[email protected]>
- Loading branch information
commit 4bb1671244db345ffc87be32f98d0a1e07163373
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1.57.0 | ||
plugins: | ||
- module: "github.com/google/go-github/v68/tools/sliceofpointers" | ||
path: ./tools/sliceofpointers/sliceofpointers.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// sliceofpointers is a custom linter to be used by | ||
// golangci-lint to find instances of `[]*string` and | ||
// slices of structs without pointers and report them. | ||
// See: https://github.com/google/go-github/issues/180 | ||
package main | ||
|
||
import ( | ||
"go/ast" | ||
"go/token" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
var analyzer = &analysis.Analyzer{ | ||
Name: "sliceofpointers", | ||
Doc: "reports usage of []*string and slices of structs without pointers", | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
for _, file := range pass.Files { | ||
ast.Inspect(file, func(n ast.Node) bool { | ||
if n == nil { | ||
return false | ||
} | ||
|
||
switch t := n.(type) { | ||
case *ast.ArrayType: | ||
checkArrayType(t, t.Pos(), pass) | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func checkArrayType(arrType *ast.ArrayType, tokenPos token.Pos, pass *analysis.Pass) { | ||
if starExpr, ok := arrType.Elt.(*ast.StarExpr); ok { | ||
if ident, ok := starExpr.X.(*ast.Ident); ok && ident.Name == "string" { | ||
const msg = "use []string instead of []*string" | ||
pass.Reportf(tokenPos, msg) | ||
} | ||
} else if ident, ok := arrType.Elt.(*ast.Ident); ok && ident.Obj != nil { | ||
if _, ok := ident.Obj.Decl.(*ast.TypeSpec).Type.(*ast.StructType); ok { | ||
pass.Reportf(tokenPos, "use []*%v instead of []%[1]v", ident.Name) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
singlechecker.Main(analyzer) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
type Example struct { | ||
Strings []*string `json:"strings,omitempty"` // want `use \[\]string instead of \[\]\*string` | ||
Things []Thing `json:"things,omitempty"` // want `use \[\]\*Thing instead of \[\]Thing` | ||
} | ||
|
||
type Thing struct { | ||
} | ||
|
||
func main() { | ||
slice1 := []*string{} // want `use \[\]string instead of \[\]\*string` | ||
_ = slice1 | ||
slice2 := []Thing{} // want `use \[\]\*Thing instead of \[\]Thing` | ||
_ = slice2 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
type Example struct { | ||
Strings []string `json:"strings,omitempty"` // Should not be flagged | ||
Things []*Thing `json:"things,omitempty"` // Should not be flagged | ||
} | ||
|
||
type Thing struct { | ||
} | ||
|
||
func main() { | ||
slice1 := []string{} // Should not be flagged | ||
_ = slice1 | ||
slice2 := []*Thing{} // Should not be flagged | ||
_ = slice2 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.