GoLand 2020.3 Help

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 Settings/Preferences Ctrl+Alt+S.

Probable bugs

InspectionDescriptionDefault Severity
Defer/go statement calls recover or panic directly

Reports defer and go statements that call recover() or panic() directly.

See Handling panics and Go statements.

Weak Warning Weak warning
Division by zero

Reports division by zero.

Division by zero will lead to a runtime panic.

Warning Warning
Exceeded shift expression

Checks shift expressions that equal or exceed the width of the integer.

Warning 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 Warning
Incorrect strings.Replace count argument

Reports strings.Replace calls with the replace count 0 instead of -1 .

The count argument for how many times a string should be replaced should not be 0 as it will not replace anything and make the function call redundant. Use -1 instead.

Warning Warning
Incorrect usage of Println/Printf-like functions

Reports incorrect usage of Println -like and Printf -like functions.

Weak Warning 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.

See: https://golang.org/pkg/errors/#As

Warning Warning
Incorrect usage of the sync/atomic package

Reports common mistaken usages of the sync/atomic package.

Warning Warning
Invalid conversions of uintptr to unsafe.Pointer

Checks for invalid conversions of uintptr to unsafe/Pointer .

Reports likely incorrect uses of unsafe/Pointer to convert integers to pointers. A conversion from uintptr to unsafe/Pointer is invalid if it implies that there is a uintptr -typed word in memory that holds a pointer value, because that word will be invisible to stack copying and to the garbage collector.

Warning 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 sync/Locker , such as sync/Mutex or sync/WaitGroup ), may cause both copies to work incorrectly. Generally such values should be referred to through a pointer.

Warning 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.

See: Go Closures and Goroutines FAQ

Warning Warning
Malformed build tag

Reports malformed build tags and build tags in the incorrect location.

See go build command: build constraints .

Weak Warning 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 key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' ') , quote (U+0022 '"') , and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

Also, checks that fields with tags are exported.

Example of a good tag is:

type Example struct { Field int `json:"field" xml:"demo"` }
Warning Warning
Nilness analyzer

Reports problems caused by incorrect use of nil value.

Analyses the data flow to determine if variables could have nil or not nil values. Based on this, reports potential bugs in the code. For example:

  • Method calls with nil receiver could lead to 'nil pointer dereference'.

  • nil slice indexing could cause panics.

  • Comparisons like v == nil could be meaningless if v is known to be always nil or not nil .

  • Variable has a corresponding error and it is not checked to be not nil . An error corresponds to a variable when they are defined or assigned together in statements like v, err := foo() .

Warning 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 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 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 Warning
Unmarshal is called with incorrect argument

Analyzes calls to functions such as json.Unmarshal . Reports a problem if the argument that is passed to store the result is not a pointer or an interface. Such calls are guaranteed to fail and return an error.

See: https://golang.org/pkg/encoding/json/#Unmarshal

Warning Warning
Unused function or method call result

Reports calls to certain functions/methods that do not handle a call result.

Warning Warning

Control flow issues

InspectionDescriptionDefault 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 Weak warning
Defer in loop

Reports defer statements inside loops.

Using defer in loops can lead to resource leaks or unpredictable execution order of the statements.

Weak Warning Weak warning
Infinite for loop

Reports empty for loops.

Running this code will make the CPU usage stay at maximum and will make the machine nearly unusable.

Error Error
Missing return at end of function

Reports missing return at end of function.

Error 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 Warning
Used as value in condition

Reports assignments which are used as values.

Error Error

Code style issues

InspectionDescriptionDefault 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 Weak warning
Comment of exported element starts with incorrect name

Reports comments that do not start with the name of the exported element.

See Code review comments: comment sentences .

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 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 Weak warning
Name starts with package name

Reports names starts with package name.

See Code review comments: package names for details.

Weak Warning Weak warning
Receiver has generic name

Reports receiver names like me , this , self or different from other receiver names for this type.

See Code review comments: receiver names for details.

Weak Warning 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 Weak warning
Unit-specific suffix for time.Duration

Reports unit-specific suffixes in constant and variable names of type time.Duration .

Weak Warning Weak warning
Unsorted imports

Reports unsorted imports.

Weak Warning Weak warning
Usage of Snake_Case

Reports usage of snake case instead of camelcase for naming variables, constants and functions.

See Effective Go: MixedCaps .

Weak Warning Weak warning

General

InspectionDescriptionDefault Severity
Assignment nil without explicit type

Reports assignment of nil without explicit type.

nil value cannot be assigned to a variable that has no type.

Examples of incorrect code: incorrect := nil var incorrect = nil

Examples of correct code: incorrect := nil var correct *MyType = nil

Error Error
Binary expressions types check

Reports incompatible types in binary and unary expressions.

Error Error
Deprecated element

Reports usages of deprecated elements.

Warning Warning
Duplicate case

Checks switch statements for case duplicates.

Error Error
Duplicate names

Reports names redeclared in this block.

Error Error
Expected '=', got ':='

Reports var assignments in var or const declaration.

Error Error
Explicit dereference required

Reports method calls and field accesses on expressions with a double pointer type.

Such expressions require explicit dereference, for example, x.f() must be replaced with (*x).f() .

Error 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 Error
Function call

This inspection checks function calls.

Error Error
Impossible type assertion

Reports impossible type assertions.

Error Error
Invalid composite literals

Reports composite literals with incompatible types and values.

Error Error
Invalid expression conversion

Reports invalid expression conversion.

Conversion expression may be incorrect due to incompatible types or impermissible constant value truncation.

Error Error
Invalid index or slice expression

Reports invalid index and slice expressions.

Error 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.

See Go specifications: address operators .

Error Error
Invalid package import

Reports invalid imports.

Error Error
Invalid receiver type

Reports invalid receiver types.

Receiver type must be of the form T or *T (possibly using parentheses) where T is a type name.

T must not be a pointer or interface type and it must be declared in the same package as the method.

Error Error
Invalid recursive type

Reports recursive types declarations.

Error Error
Jump over declaration

Reports goto statements which jumps over declaration.

For example:

goto label var a int label:

Error Error
Mismatched types: byte and string

Reports comparisons of string index with a single-byte string instead of byte.

Error Error
Missing trailing comma before newline in composite literal

Reports missing trailing comma before newline in composite literal.

Error Error
Multiple packages in directory declaration

Reports multiple packages in a single directory.

Error Error
Non-function call

Reports non-function calls.

For example: f := 2 f() // error

Error Error
Redundant parentheses

Reports redundant parentheses in expressions and types.

Weak Warning Weak warning
Self import

Reports self-imports.

Error Error
Types compatibility check

Reports incompatible types.

Error Error
Underscore used as value

Reports underscores that are used as values.

Error Error
Unexpected '~'

Reports unexpected tildes.

Error 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 go doc .

Warning 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 Error
Usage of cgo in tests is not supported

Reports cgo import in test files.

Test files are not allowed to import the C package.

Error Error
Wrong test declaration

Reports invalid test functions signatures.

Error Error

Declaration redundancy

InspectionDescriptionDefault Severity
Bool condition

Reports redundant and suspect parts of boolean expressions.

Warning Warning
Empty declaration

Reports empty declarations.

Warning 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.

See Code review comments: declaring empty slices .

Weak Warning 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 Warning
Redundant comma

Reports redundant comma in the end of argument lists and composite literals.

Weak Warning Weak warning
Redundant import alias

Reports import aliases that can be omitted.

Weak Warning Weak warning
Redundant second index in slices

Reports redundant second index in slices inspection.

Example: a[0:len(a)] .

Warning Warning
Redundant semicolon

Reports redundant semicolons.

Weak Warning Weak warning
Redundant type conversion

Reports redundant type conversion that can be omitted.

Example: var s = string("hello") .

Weak Warning Weak warning
Redundant types in composite literals

Reports redundant types in composite literals.

For example: [][]int{[]int{1}, []int{2}}

can be simplified to: [][]int {{1}, {2}} .

Warning Warning
Self assignment

Reports self assignment.

Weak Warning 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 Weak warning
Unused constant

Reports unused constants.

Warning Warning
Unused exported function

Reports unused exported functions.

Warning Warning
Unused exported type

Reports unused exported types in main package and in tests.

Warning Warning
Unused function

Reports unused unexported functions.

Warning Warning
Unused global variable

Reports for unused global variables.

Warning Warning
Unused import

Reports unused import statements.

Error Error
Unused label

Reports unused label definitions.

Error Error
Unused parameter

Reports unused function parameters.

Warning Warning
Unused type

Reports unused types.

Warning Warning
Unused variable

Reports unused variables.

Error Error
Last modified: 08 May 2020