Documentation ¶
Overview ¶
Package stubmethods defines a code action for missing interface methods.
Analyzer stubmethods ¶
stubmethods: detect missing methods and fix with stub implementations
This analyzer detects type-checking errors due to missing methods in assignments from concrete types to interface types, and offers a suggested fix that will create a set of stub methods so that the concrete type satisfies the interface.
For example, this function will not compile because the value NegativeErr{} does not implement the "error" interface:
func sqrt(x float64) (float64, error) { if x < 0 { return 0, NegativeErr{} // error: missing method } ... } type NegativeErr struct{}
This analyzer will suggest a fix to declare this method:
// Error implements error.Error. func (NegativeErr) Error() string { panic("unimplemented") }
(At least, it appears to behave that way, but technically it doesn't use the SuggestedFix mechanism and the stub is created by logic in gopls's golang.stub function.)
Index ¶
Constants ¶
const FixCategory = "stubmethods" // recognized by gopls ApplyFix
Variables ¶
var Analyzer = &analysis.Analyzer{ Name: "stubmethods", Doc: analysisinternal.MustExtractDoc(doc, "stubmethods"), Run: run, RunDespiteErrors: true, URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/stubmethods", }
Functions ¶
func DiagnosticForError ¶
func DiagnosticForError(fset *token.FileSet, file *ast.File, start, end token.Pos, msg string, info *types.Info) (analysis.Diagnostic, bool)
DiagnosticForError computes a diagnostic suggesting to implement an interface to fix the type checking error defined by (start, end, msg).
If no such fix is possible, the second result is false.
func MatchesMessage ¶
MatchesMessage reports whether msg matches the error message sought after by the stubmethods fix.
Types ¶
type StubInfo ¶
type StubInfo struct { // Interface is the interface that the client wants to implement. // When the interface is defined, the underlying object will be a TypeName. // Note that we keep track of types.Object instead of types.Type in order // to keep a reference to the declaring object's package and the ast file // in the case where the concrete type file requires a new import that happens to be renamed // in the interface file. // TODO(marwan-at-work): implement interface literals. Fset *token.FileSet // the FileSet used to type-check the types below Interface *types.TypeName Concrete *types.Named Pointer bool }
StubInfo represents a concrete type that wants to stub out an interface type
func GetStubInfo ¶
GetStubInfo determines whether the "missing method error" can be used to deduced what the concrete and interface types are.
TODO(adonovan): this function (and its following 5 helpers) tries to deduce a pair of (concrete, interface) types that are related by an assignment, either explicitly or through a return statement or function call. This is essentially what the refactor/satisfy does, more generally. Refactor to share logic, after auditing 'satisfy' for safety on ill-typed code.