aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/karamaru-alpha
diff options
context:
space:
mode:
authordependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2024-04-02 14:37:40 +0000
committerTaras Madan <tarasmadan@google.com>2024-04-03 09:59:40 +0000
commit9d2a90af8850a414d2da20b806d7aa8fd9a297ae (patch)
treeb6ce5a1bc2ecaed9f94b06b36eca20b98929970c /vendor/github.com/karamaru-alpha
parentb90978ba49e3321a2d1cd77c07c196b088c97386 (diff)
mod: bump github.com/golangci/golangci-lint from 1.56.2 to 1.57.2
Bumps [github.com/golangci/golangci-lint](https://github.com/golangci/golangci-lint) from 1.56.2 to 1.57.2. - [Release notes](https://github.com/golangci/golangci-lint/releases) - [Changelog](https://github.com/golangci/golangci-lint/blob/master/CHANGELOG.md) - [Commits](https://github.com/golangci/golangci-lint/compare/v1.56.2...v1.57.2) --- updated-dependencies: - dependency-name: github.com/golangci/golangci-lint dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
Diffstat (limited to 'vendor/github.com/karamaru-alpha')
-rw-r--r--vendor/github.com/karamaru-alpha/copyloopvar/.gitignore2
-rw-r--r--vendor/github.com/karamaru-alpha/copyloopvar/LICENSE21
-rw-r--r--vendor/github.com/karamaru-alpha/copyloopvar/README.md27
-rw-r--r--vendor/github.com/karamaru-alpha/copyloopvar/copyloopvar.go137
4 files changed, 187 insertions, 0 deletions
diff --git a/vendor/github.com/karamaru-alpha/copyloopvar/.gitignore b/vendor/github.com/karamaru-alpha/copyloopvar/.gitignore
new file mode 100644
index 000000000..816abbd92
--- /dev/null
+++ b/vendor/github.com/karamaru-alpha/copyloopvar/.gitignore
@@ -0,0 +1,2 @@
+.idea/
+copyloopvar
diff --git a/vendor/github.com/karamaru-alpha/copyloopvar/LICENSE b/vendor/github.com/karamaru-alpha/copyloopvar/LICENSE
new file mode 100644
index 000000000..e2567fd0c
--- /dev/null
+++ b/vendor/github.com/karamaru-alpha/copyloopvar/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 Ryosei Karaki
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/vendor/github.com/karamaru-alpha/copyloopvar/README.md b/vendor/github.com/karamaru-alpha/copyloopvar/README.md
new file mode 100644
index 000000000..d31d1abd9
--- /dev/null
+++ b/vendor/github.com/karamaru-alpha/copyloopvar/README.md
@@ -0,0 +1,27 @@
+# copyloopvar
+
+copyloopvar is a linter detects places where loop variables are copied.
+
+cf. [Fixing For Loops in Go 1.22](https://go.dev/blog/loopvar-preview)
+
+## Example
+
+```go
+for i, v := range []int{1, 2, 3} {
+ i := i // The copy of the 'for' variable "i" can be deleted (Go 1.22+)
+ v := v // The copy of the 'for' variable "v" can be deleted (Go 1.22+)
+ _, _ = i, v
+}
+
+for i := 1; i <= 3; i++ {
+ i := i // The copy of the 'for' variable "i" can be deleted (Go 1.22+)
+ _ = i
+}
+```
+
+## Install
+
+```bash
+go install github.com/karamaru-alpha/copyloopvar/cmd/copyloopvar@latest
+go vet -vettool=`which copyloopvar` ./...
+```
diff --git a/vendor/github.com/karamaru-alpha/copyloopvar/copyloopvar.go b/vendor/github.com/karamaru-alpha/copyloopvar/copyloopvar.go
new file mode 100644
index 000000000..36bda8b14
--- /dev/null
+++ b/vendor/github.com/karamaru-alpha/copyloopvar/copyloopvar.go
@@ -0,0 +1,137 @@
+package copyloopvar
+
+import (
+ "fmt"
+ "go/ast"
+ "go/token"
+
+ "golang.org/x/tools/go/analysis"
+ "golang.org/x/tools/go/analysis/passes/inspect"
+ "golang.org/x/tools/go/ast/inspector"
+)
+
+var ignoreAlias bool
+
+func NewAnalyzer() *analysis.Analyzer {
+ analyzer := &analysis.Analyzer{
+ Name: "copyloopvar",
+ Doc: "copyloopvar is a linter detects places where loop variables are copied",
+ Run: run,
+ Requires: []*analysis.Analyzer{
+ inspect.Analyzer,
+ },
+ }
+ analyzer.Flags.BoolVar(&ignoreAlias, "ignore-alias", false, "ignore aliasing of loop variables")
+ return analyzer
+}
+
+func run(pass *analysis.Pass) (any, error) {
+ inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
+
+ nodeFilter := []ast.Node{
+ (*ast.RangeStmt)(nil),
+ (*ast.ForStmt)(nil),
+ }
+
+ inspect.Preorder(nodeFilter, func(n ast.Node) {
+ switch node := n.(type) {
+ case *ast.RangeStmt:
+ checkRangeStmt(pass, node)
+ case *ast.ForStmt:
+ checkForStmt(pass, node)
+ }
+ })
+
+ return nil, nil
+}
+
+func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
+ key, ok := rangeStmt.Key.(*ast.Ident)
+ if !ok {
+ return
+ }
+ var value *ast.Ident
+ if rangeStmt.Value != nil {
+ if value, ok = rangeStmt.Value.(*ast.Ident); !ok {
+ return
+ }
+ }
+ for _, stmt := range rangeStmt.Body.List {
+ assignStmt, ok := stmt.(*ast.AssignStmt)
+ if !ok {
+ continue
+ }
+ if assignStmt.Tok != token.DEFINE {
+ continue
+ }
+ for i, rh := range assignStmt.Rhs {
+ right, ok := rh.(*ast.Ident)
+ if !ok {
+ continue
+ }
+ if right.Name != key.Name && (value == nil || right.Name != value.Name) {
+ continue
+ }
+ if ignoreAlias {
+ left, ok := assignStmt.Lhs[i].(*ast.Ident)
+ if !ok {
+ continue
+ }
+ if left.Name != right.Name {
+ continue
+ }
+ }
+ pass.Report(analysis.Diagnostic{
+ Pos: assignStmt.Pos(),
+ Message: fmt.Sprintf(`The copy of the 'for' variable "%s" can be deleted (Go 1.22+)`, right.Name),
+ })
+ }
+ }
+}
+
+func checkForStmt(pass *analysis.Pass, forStmt *ast.ForStmt) {
+ if forStmt.Init == nil {
+ return
+ }
+ initAssignStmt, ok := forStmt.Init.(*ast.AssignStmt)
+ if !ok {
+ return
+ }
+ initVarNameMap := make(map[string]interface{}, len(initAssignStmt.Lhs))
+ for _, lh := range initAssignStmt.Lhs {
+ if initVar, ok := lh.(*ast.Ident); ok {
+ initVarNameMap[initVar.Name] = struct{}{}
+ }
+ }
+ for _, stmt := range forStmt.Body.List {
+ assignStmt, ok := stmt.(*ast.AssignStmt)
+ if !ok {
+ continue
+ }
+ if assignStmt.Tok != token.DEFINE {
+ continue
+ }
+ for i, rh := range assignStmt.Rhs {
+ right, ok := rh.(*ast.Ident)
+ if !ok {
+ continue
+ }
+ if _, ok := initVarNameMap[right.Name]; !ok {
+ continue
+ }
+ if ignoreAlias {
+ left, ok := assignStmt.Lhs[i].(*ast.Ident)
+ if !ok {
+ continue
+ }
+ if left.Name != right.Name {
+ continue
+ }
+ }
+ pass.Report(analysis.Diagnostic{
+ Pos: assignStmt.Pos(),
+ Message: fmt.Sprintf(`The copy of the 'for' variable "%s" can be deleted (Go 1.22+)`, right.Name),
+ })
+ }
+ }
+}