mirror of
https://github.com/golang/go
synced 2024-11-18 12:24:48 -07:00
4d14fc9c00
This change is the first step in handling golang/go#38136. Instead of creating multiple diagnostic reports for type error analyzers, we add suggested fixes to the existing reports. To match the analyzers for FindAnalysisError, we add an ErrorMatch function to source.Analyzer. This is not an ideal solution, but it was the best one I could come up with without modifying the go/analysis API. analysisinternal could be used for this purpose, but it seemed to complicated to be worth it, and this is fairly simple. I think that go/analysis itself might need to be extended for type error analyzers, but these temporary measures will help us understand the kinds of features we need for type error analyzers. A follow-up CL might be to not add reports for type error analyzers until the end of source.Diagnostic, which would remove the need for the look-up. Fixes golang/go#38136 Change-Id: I25bc6396b09d49facecd918bf5591d2d5bdf1b3a Reviewed-on: https://go-review.googlesource.com/c/tools/+/226777 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Package nonewvars defines an Analyzer that applies suggested fixes
|
|
// to errors of the type "no new variables on left side of :=".
|
|
package nonewvars
|
|
|
|
import (
|
|
"bytes"
|
|
"go/ast"
|
|
"go/format"
|
|
"go/token"
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
"golang.org/x/tools/go/analysis/passes/inspect"
|
|
"golang.org/x/tools/go/ast/inspector"
|
|
"golang.org/x/tools/internal/analysisinternal"
|
|
)
|
|
|
|
const Doc = `suggested fixes for "no new vars on left side of :="
|
|
|
|
This checker provides suggested fixes for type errors of the
|
|
type "no new vars on left side of :=". For example:
|
|
z := 1
|
|
z := 2
|
|
will turn into
|
|
z := 1
|
|
z = 2
|
|
`
|
|
|
|
var Analyzer = &analysis.Analyzer{
|
|
Name: string(analysisinternal.NoNewVars),
|
|
Doc: Doc,
|
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
|
Run: run,
|
|
RunDespiteErrors: true,
|
|
}
|
|
|
|
func run(pass *analysis.Pass) (interface{}, error) {
|
|
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
|
|
errors := analysisinternal.GetTypeErrors(pass)
|
|
|
|
nodeFilter := []ast.Node{(*ast.AssignStmt)(nil)}
|
|
inspect.Preorder(nodeFilter, func(n ast.Node) {
|
|
assignStmt, _ := n.(*ast.AssignStmt)
|
|
// We only care about ":=".
|
|
if assignStmt.Tok != token.DEFINE {
|
|
return
|
|
}
|
|
|
|
var file *ast.File
|
|
for _, f := range pass.Files {
|
|
if f.Pos() <= assignStmt.Pos() && assignStmt.Pos() < f.End() {
|
|
file = f
|
|
break
|
|
}
|
|
}
|
|
if file == nil {
|
|
return
|
|
}
|
|
|
|
for _, err := range errors {
|
|
if !FixesError(err.Msg) {
|
|
continue
|
|
}
|
|
if assignStmt.Pos() > err.Pos || err.Pos >= assignStmt.End() {
|
|
continue
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := format.Node(&buf, pass.Fset, file); err != nil {
|
|
continue
|
|
}
|
|
pass.Report(analysis.Diagnostic{
|
|
Pos: err.Pos,
|
|
End: analysisinternal.TypeErrorEndPos(pass.Fset, buf.Bytes(), err.Pos),
|
|
Message: err.Msg,
|
|
SuggestedFixes: []analysis.SuggestedFix{{
|
|
Message: "Change ':=' to '='",
|
|
TextEdits: []analysis.TextEdit{{
|
|
Pos: err.Pos,
|
|
End: err.Pos + 1,
|
|
}},
|
|
}},
|
|
})
|
|
}
|
|
})
|
|
return nil, nil
|
|
}
|
|
|
|
func FixesError(msg string) bool {
|
|
return msg == "no new variables on left side of :="
|
|
}
|