Code Inspections in Go
This topic lists all GoLand code inspections available in Go.
You can toggle specific inspections or change their severity level on the Editor | Inspections page of the IDE settings Ctrl+Alt+S.
Probable bugs
Inspection | Description | Default Severity |
---|---|---|
Defer/go statement calls recover or panic directly | Reports See Handling panics and Go statements. | Weak warning |
Division by zero | Reports division by zero. Division by zero will lead to a runtime panic. | Warning |
Exceeded shift expression | Checks shift expressions that equal or exceed the width of the integer. | Warning |
Imported package name as name identifier | Reports declarations of variables, arguments or functions that overlap with the used import. While legal, such declarations will make using the package exported identifiers impossible after the declaration or create confusion when reading the code. | Warning |
Incorrect strings.Replace count argument | Reports The count argument for how many times a string should be replaced should not be | Warning |
Incorrect usage of Println/Printf-like functions | Reports incorrect usage of | Weak warning |
Incorrect usage of the errors.As function | Checks that the second argument to errors.As is a pointer to an interface or to a type implementing error. | Warning |
Incorrect usage of the sync/atomic package | Reports common mistaken usages of the | Warning |
Invalid conversions of uintptr to unsafe.Pointer | Checks for invalid conversions of Reports likely incorrect uses of | Warning |
Locks mistakenly passed by value | Checks for locks mistakenly passed by value. Inadvertently copying a value containing a lock (which is a type implementing | Warning |
Loop variables captured by func literal | Reports references to loop variables from within nested functions. This analyzer checks for references to loop variables from within a function literal inside the loop body. It checks only instances where the function literal is called in a defer or go statement that is the last statement in the loop body. | Warning |
Malformed build tag | Reports malformed build tags and build tags in the incorrect location. | Weak warning |
Malformed struct tag | Reports that the struct tags conforms to Go conventions. According to these conventions, tag strings are a concatenation of optionally space-separated Also, checks that fields with tags are exported. Example of a good tag is: type Example struct {
Field int `json:"field" xml:"demo"`
}
| Warning |
Nilness analyzer | Reports problems caused by incorrect use of Analyses the data flow to determine if variables could have
| Warning |
Non-standard signature for well-known function names | Checks that method whose name matches one of several well-known interface methods from the standard library has the correct signature. | Warning |
Reserved word used as name | Reports declarations of variables, arguments or functions that overlap with the built-in or reserved keyword. If you receive this error then your code might not be as explicit as possible and might confuse other users. | Warning |
Shadowing variable | Reports declarations of variables that overlap with the declarations in the outer scope. | Disabled |
Unhandled error | Reports calls to functions/methods that return an error if the error is not handled. | Warning |
Unmarshal is called with incorrect argument | Analyzes calls to functions such as | Warning |
Unused function or method call result | Reports calls to certain functions/methods that do not handle a call result. | Warning |
Control flow issues
Inspection | Description | Default Severity |
---|---|---|
Assignment to receiver | Reports assignment to method receiver. When assigning a value to the method receiver it won't be reflected outside of the method itself. Values will be reflected in subsequent calls from the same method. | Weak warning |
Defer in loop | Reports Using | Weak warning |
Infinite for loop | Reports empty Running this code will make the CPU usage stay at maximum and will make the machine nearly unusable. | Error |
Missing return at end of function | Reports missing return at end of function. | Error |
Unreachable code | Reports code that can never be executed because there exists no control flow path to the code from the rest of the program. | Warning |
Used as value in condition | Reports assignments which are used as values. | Error |
Code style issues
Inspection | Description | Default Severity |
---|---|---|
Comment has no leading space | Reports comments without a leading space. Comments with leading space can be easier to read since the first word is separated from the comment by a space. | Weak warning |
Comment of exported element starts with incorrect name | Reports comments that do not start with the name of the exported element. | Disabled |
Error string should not be capitalized or end with punctuation | Reports error strings format issues. Error strings should not be capitalized or end with punctuation mark. See Code review comments: error strings for more details. | Weak warning |
Exported element should have comment | Reports exported types, functions, methods, constants and variables without comment. See Code review comments: comment sentences and Code review comments: doc comments. | Disabled |
Exported element should have its own declaration | Reports an exported var or const in a comma separated list of declarations. For example: package p
const C1, C2 = 1, 2
One should consider providing separate specs for such declarations as this makes code more readable: package p
const (
C1 = 1
C2 = 2
) | Weak warning |
Name starts with package name | Reports names starts with package name. See Code review comments: package names for details. | Weak warning |
Receiver has generic name | Reports receiver names like See Code review comments: receiver names for details. | Weak warning |
Struct initialization without field names | Reports structures which are initialized without specifying their field names. When initializing a structure, it's better to explicitly state the name of the fields in order to ensure that in case of changes in the order of the fields or the name of the fields they will correctly continue to be addressed. | Weak warning |
Unit-specific suffix for time.Duration | Reports unit-specific suffixes in constant and variable names of type | Weak warning |
Unsorted imports | Reports unsorted imports. | Weak warning |
Usage of Snake_Case | Reports usage of snake case instead of camelcase for naming variables, constants and functions. | Weak warning |
General
Inspection | Description | Default Severity |
---|---|---|
Assignment nil without explicit type | Reports assignment of
Examples of incorrect code: Examples of correct code: | Error |
Binary expressions types check | Reports incompatible types in binary and unary expressions. | Error |
Deprecated element | Reports usages of deprecated elements. | Warning |
Duplicate case | Checks switch statements for case duplicates. | Error |
Duplicate names | Reports names redeclared in this block. | Error |
Expected '=', got ':=' | Reports var assignments in var or const declaration. | Error |
Explicit dereference required | Reports method calls and field accesses on expressions with a double pointer type. Such expressions require explicit dereference, for example, | Error |
Extended method expression syntax before Go 1.10 | Reports method expressions with extended receiver type syntax available since Go 1.10. In Go versions prior 1.10 a receiver type of a method expression is restricted to be a type name or a pointer to type name. Since Go 1.10 a receiver type can be any type. | Error |
Function call | This inspection checks function calls. | Error |
Impossible type assertion | Reports impossible type assertions. | Error |
Invalid composite literals | Reports composite literals with incompatible types and values. | Error |
Invalid expression conversion | Reports invalid expression conversion. Conversion expression may be incorrect due to incompatible types or impermissible constant value truncation. | Error |
Invalid index or slice expression | Reports invalid index and slice expressions. | Error |
Invalid operand of address operator | Reports address operators applied to wrong operands. Address operation can only be applied to addressable operand and pointer indirection can only be applied to operand of a pointer type. | Error |
Invalid package import | Reports invalid imports. | Error |
Invalid receiver type | Reports invalid receiver types. Receiver type must be of the form
| Error |
Invalid recursive type | Reports recursive types declarations. | Error |
Jump over declaration | Reports goto statements which jumps over declaration. For example: goto label
var a int
label: | Error |
Mismatched types: byte and string | Reports comparisons of string index with a single-byte string instead of byte. | Error |
Missing trailing comma before newline in composite literal | Reports missing trailing comma before newline in composite literal. | Error |
Multiple packages in directory declaration | Reports multiple packages in a single directory. | Error |
Non-function call | Reports non-function calls. For example: | Error |
Redundant parentheses | Reports redundant parentheses in expressions and types. | Weak warning |
Self import | Reports self-imports. | Error |
Types compatibility check | Reports incompatible types. | Error |
Underscore used as value | Reports underscores that are used as values. | Error |
Unexpected '~' | Reports unexpected tildes. | Error |
Unexported return type of exported function | Reports exported functions with unexported return types. Unexported types can be difficult to use when viewing documentation under | Warning |
Unnecessarily exported identifier | Reports exported identifiers that are not used by other packages than the ones they are defined in. | Disabled |
Unresolved reference | Reports unresolved references. | Error |
Usage of cgo in tests is not supported | Reports cgo import in test files. Test files are not allowed to import the | Error |
Wrong test declaration | Reports invalid test functions signatures. | Error |
Declaration redundancy
Inspection | Description | Default Severity |
---|---|---|
Bool condition | Reports redundant and suspect parts of boolean expressions. | Warning |
Empty declaration | Reports empty declarations. | Warning |
Empty slice declared via literal | Reports slice declaration with empty literal initializer instead of nil. An empty slice can be represented by nil or an empty slice literal. The first approach is preferred as it does not lead to memory allocation. | Weak warning |
Redundant blank argument in range | Reports expressions of range clause which are redundant and can be deleted. For example following for statements can be simplified: for _ = range v
for _, _ = range v
for a, _ = range v | Warning |
Redundant comma | Reports redundant comma in the end of argument lists and composite literals. | Weak warning |
Redundant import alias | Reports import aliases that can be omitted. | Weak warning |
Redundant second index in slices | Reports redundant second index in slices inspection. Example: | Warning |
Redundant semicolon | Reports redundant semicolons. | Weak warning |
Redundant type conversion | Reports redundant type conversion that can be omitted. Example: | Weak warning |
Redundant types in composite literals | Reports redundant types in composite literals. For example: can be simplified to: | Warning |
Self assignment | Reports self assignment. | Weak warning |
Type can be omitted | Reports variable and constant types that can be omitted since it can be inferred by the compiler. | Weak warning |
Unused constant | Reports unused constants. | Warning |
Unused exported function | Reports unused exported functions. | Warning |
Unused exported type | Reports unused exported types in main package and in tests. | Warning |
Unused function | Reports unused unexported functions. | Warning |
Unused global variable | Reports for unused global variables. | Warning |
Unused import | Reports unused import statements. | Error |
Unused label | Reports unused label definitions. | Error |
Unused parameter | Reports unused function parameters. | Warning |
Unused type | Reports unused types. | Warning |
Unused variable | Reports unused variables. | Error |