Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Analyzer = SCAnalyzer.Analyzer
View Source
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{ Analyzer: &analysis.Analyzer{ Name: "SA9004", Run: run, Requires: []*analysis.Analyzer{inspect.Analyzer}, }, Doc: &lint.RawDocumentation{ Title: `Only the first constant has an explicit type`, Text: `In a constant declaration such as the following: const ( First byte = 1 Second = 2 ) the constant Second does not have the same type as the constant First. This construct shouldn't be confused with const ( First byte = iota Second ) where \'First\' and \'Second\' do indeed have the same type. The type is only passed on when no explicit value is assigned to the constant. When declaring enumerations with explicit values it is therefore important not to write const ( EnumFirst EnumType = 1 EnumSecond = 2 EnumThird = 3 ) This discrepancy in types can cause various confusing behaviors and bugs. Wrong type in variable declarations The most obvious issue with such incorrect enumerations expresses itself as a compile error: package pkg const ( EnumFirst uint8 = 1 EnumSecond = 2 ) func fn(useFirst bool) { x := EnumSecond if useFirst { x = EnumFirst } } fails to compile with ./const.go:11:5: cannot use EnumFirst (type uint8) as type int in assignment Losing method sets A more subtle issue occurs with types that have methods and optional interfaces. Consider the following: package main import "fmt" type Enum int func (e Enum) String() string { return "an enum" } const ( EnumFirst Enum = 1 EnumSecond = 2 ) func main() { fmt.Println(EnumFirst) fmt.Println(EnumSecond) } This code will output an enum 2 as \'EnumSecond\' has no explicit type, and thus defaults to \'int\'.`, Since: "2019.1", Severity: lint.SeverityWarning, MergeIf: lint.MergeIfAny, }, })
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.