Go Analyzer Updates - March 2021

We have added 6 new Autofix in this release and improved 1 existing Autofix to suggest better fix. :rocket:

Features (with examples):

Add Autofix for “Simplify return statements”

Before:

func fn1() bool {
	x := true
	if x {
		return true
	}
	return false
}

Autofix:

func fn1() bool {
	x := true
	return x
}

Add Autofix for "Omit redundant nil check on slice, map, chan"

Before:

var s []int
var m map[int]int
var ch chan int

if s == nil || len(s) == 0 {
}
if m == nil || len(m) == 0 {
}
if ch == nil || len(ch) == 0 {
}

Autofix:

var s []int
var m map[int]int
var ch chan int

if len(s) == 0 {
}
if len(m) == 0 {
}
if len(ch) == 0 {
}

Add Autofix for “Simplify make call”

Before:

_ = make(chan int, 0)
_ = make(map[int]int, 0)
_ = make([]int, 1, 1)

Autofix:

_ = make(chan int)
_ = make(map[int]int)
_ = make([]int, 1)

Add Autofix for “Omit redundant nil check in type assertion”

Before:

func fn(i interface{}) {
	if _, ok := i.(string); ok && i != nil {
	}
}

Autofix:

func fn(i interface{}) {
	if _, ok := i.(string); ok {
	}
}

Add Autofix for “Omit redundant control flow”

Before:

func fnc2(a int) {
	return
}

Autofix:

func fnc2(a int) {
}

Add Autofix for “Omit redundant nil check around for loop”

Before:

var nilMap map[string]int
if nilMap != nil {
	for key, value := range nilMap {
		nilMap[key] = value + 1
	}
}

Autofix:

var nilMap map[string]int
for key, value := range nilMap {
	nilMap[key] = value + 1
}

Fixes:

  • Improve Autofix CRT-A0003. Our analyzer will suggest better fix than before for CRT-A0003