Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add gobreakselectinfor linter#4924

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
rnben wants to merge4 commits intogolangci:master
base:master
Choose a base branch
Loading
fromrnben:master
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
dev: reuse created linter
  • Loading branch information
@rnben
rnben committedAug 17, 2024
commit0d670d67eaa57d7d02f9e60aad4b16d8ccf5d961
1 change: 1 addition & 0 deletionsgo.mod
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2
github.com/polyfloyd/go-errorlint v1.6.0
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/rnben/go-break-select-in-for v0.0.1
github.com/ryancurrah/gomodguard v1.3.3
github.com/ryanrolds/sqlclosecheck v0.5.1
github.com/sanposhiho/wastedassign/v2 v2.0.7
Expand Down
2 changes: 2 additions & 0 deletionsgo.sum
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

85 changes: 6 additions & 79 deletionspkg/golinters/gobreakselectinfor/gobreakselectinfor.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,19 @@
package gobreakselectinfor

import (
"go/ast"
"go/token"
"sync"

"github.com/rnben/go-break-select-in-for/pkg/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const linterName = "gobreakselectinfor"

func New() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: linterName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: func(pass *analysis.Pass) (any, error) {
fileIssues := run(pass)
res := make([]goanalysis.Issue, 0, len(fileIssues))
for i := range fileIssues {
res = append(res, goanalysis.NewIssue(&fileIssues[i], pass))
}
if len(res) == 0 {
return nil, nil
}

mu.Lock()
resIssues = append(resIssues, res...)
mu.Unlock()

return nil, nil
},
}
a := analyzer.Analyzer

return goanalysis.NewLinter(
linterName,
"Checks that break statement inside select statement inside for loop",
[]*analysis.Analyzer{analyzer},
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func run(pass *analysis.Pass) []result.Issue {
var res []result.Issue

inspect := func(node ast.Node) bool {
funcDecl, ok := node.(*ast.FuncDecl)
if !ok {
return true
}

ast.Inspect(funcDecl.Body, func(stmt ast.Node) bool {
if forStmt, ok := stmt.(*ast.ForStmt); ok {
ast.Inspect(forStmt.Body, func(stmt ast.Node) bool {
if selStmt, ok := stmt.(*ast.SelectStmt); ok {
ast.Inspect(selStmt.Body, func(stmt ast.Node) bool {
if brkStmt, ok := stmt.(*ast.BranchStmt); ok && brkStmt.Tok == token.BREAK {
pass.Reportf(stmt.Pos(), "break statement inside select statement inside for loop")
res = append(res, result.Issue{
Pos: pass.Fset.Position(stmt.Pos()),
Text: "break statement inside select statement inside for loop",
FromLinter: linterName,
})
return true
}
return true
})
}
return true
})
}
return true
})

return true
}

for _, f := range pass.Files {
ast.Inspect(f, inspect)
}

return res
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}

[8]ページ先頭

©2009-2025 Movatter.jp