Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 13 additions & 5 deletions errorlint/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
var lints []analysis.Diagnostic
extInfo := newTypesInfoExt(pass)
if checkComparison {
l := LintErrorComparisons(extInfo)
l := LintErrorComparisons(pass.Fset, extInfo)
lints = append(lints, l...)
}
if checkAsserts {
Expand All @@ -51,6 +51,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
l := LintFmtErrorfCalls(pass.Fset, *pass.TypesInfo, checkErrorfMulti)
lints = append(lints, l...)
}

// Resolve conflicts between type assertion and error comparison diagnostics
lints = resolveConflicts(lints, extInfo)

sort.Sort(ByPosition(lints))

for _, l := range lints {
Expand Down Expand Up @@ -78,11 +82,15 @@ func newTypesInfoExt(pass *analysis.Pass) *TypesInfoExt {
}
stack := []ast.Node{file}
ast.Inspect(file, func(n ast.Node) bool {
nodeParent[n] = stack[len(stack)-1]
if n == nil {
stack = stack[:len(stack)-1]
} else {
if n != nil && len(stack) > 0 {
// Only set parent if the node is not the same as the current stack top
// This prevents self-references that cause infinite loops
if n != stack[len(stack)-1] {
nodeParent[n] = stack[len(stack)-1]
}
stack = append(stack, n)
} else if n == nil {
stack = stack[:len(stack)-1]
}
return true
})
Expand Down
12 changes: 11 additions & 1 deletion errorlint/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func TestAllowedComparisons(t *testing.T) {

func TestIssueRegressions(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.Run(t, analysistest.TestData(), analyzer, "issues")
analysistest.Run(t, analysistest.TestData(), analyzer, "issues/lint")
}

func TestIssueFixRegressions(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "issues/fix")
}

func TestErrorComparisonFixes(t *testing.T) {
Expand All @@ -53,3 +58,8 @@ func TestErrorTypeAssertionFixes(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "errorassert")
}

func TestASTManipulationFixes(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "astmanipulation")
}
Loading