-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,65 @@ | ||
package golinters | ||
|
||
import ( | ||
"sort" | ||
"sync" | ||
|
||
"github.com/nakabonne/nestif" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const nestifName = "nestif" | ||
|
||
func NewNestif() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: goanalysis.TheOnlyAnalyzerName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
nestifName, | ||
"Reports deeply nested if statements", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
checker := &nestif.Checker{ | ||
MinComplexity: lintCtx.Settings().Nestif.MinComplexity, | ||
} | ||
var issues []nestif.Issue | ||
for _, f := range pass.Files { | ||
issues = append(issues, checker.Check(f, pass.Fset)...) | ||
} | ||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
|
||
sort.SliceStable(issues, func(i, j int) bool { | ||
return issues[i].Complexity > issues[j].Complexity | ||
}) | ||
|
||
res := make([]goanalysis.Issue, 0, len(issues)) | ||
for _, i := range issues { | ||
res = append(res, goanalysis.NewIssue(&result.Issue{ //nolint:scopelint | ||
Pos: i.Pos, | ||
Text: i.Message, | ||
FromLinter: nestifName, | ||
}, pass)) | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, res...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains 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 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,47 @@ | ||
//args: -Enestif | ||
//config: linters-settings.nestif.min-complexity=1 | ||
package testdata | ||
|
||
func _() { | ||
var b1, b2, b3, b4 bool | ||
|
||
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 1\)" | ||
if b2 { // +1 | ||
} | ||
} | ||
|
||
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 3\)" | ||
if b2 { // +1 | ||
if b3 { // +2 | ||
} | ||
} | ||
} | ||
|
||
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 5\)" | ||
if b2 { // +1 | ||
} else if b3 { // +1 | ||
if b4 { // +2 | ||
} | ||
} else { // +1 | ||
} | ||
} | ||
|
||
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 9\)" | ||
if b2 { // +1 | ||
if b3 { // +2 | ||
} | ||
} | ||
|
||
if b2 { // +1 | ||
if b3 { // +2 | ||
if b4 { // +3 | ||
} | ||
} | ||
} | ||
} | ||
|
||
if b1 == b2 == b3 { // ERROR "`if b1 == b2 == b3` is deeply nested \(complexity: 1\)" | ||
if b4 { // +1 | ||
} | ||
} | ||
} |