golang.org/x/tools/gopls@v0.15.3/internal/analysis/stubmethods/doc.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package stubmethods defines a code action for missing interface methods.
     6  //
     7  // # Analyzer stubmethods
     8  //
     9  // stubmethods: detect missing methods and fix with stub implementations
    10  //
    11  // This analyzer detects type-checking errors due to missing methods
    12  // in assignments from concrete types to interface types, and offers
    13  // a suggested fix that will create a set of stub methods so that
    14  // the concrete type satisfies the interface.
    15  //
    16  // For example, this function will not compile because the value
    17  // NegativeErr{} does not implement the "error" interface:
    18  //
    19  //	func sqrt(x float64) (float64, error) {
    20  //		if x < 0 {
    21  //			return 0, NegativeErr{} // error: missing method
    22  //		}
    23  //		...
    24  //	}
    25  //
    26  //	type NegativeErr struct{}
    27  //
    28  // This analyzer will suggest a fix to declare this method:
    29  //
    30  //	// Error implements error.Error.
    31  //	func (NegativeErr) Error() string {
    32  //		panic("unimplemented")
    33  //	}
    34  //
    35  // (At least, it appears to behave that way, but technically it
    36  // doesn't use the SuggestedFix mechanism and the stub is created by
    37  // logic in gopls's golang.stub function.)
    38  package stubmethods