CodeQL 文档

无法访问的语句

ID: go/unreachable-statement
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - maintainability
   - correctness
   - external/cwe/cwe-561
Query suites:
   - go-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

无法访问的语句通常表示代码缺失或存在潜在错误,应仔细检查。

建议

检查周围的代码,以确定为什么该语句无法访问。如果不再需要,请删除该语句。

示例

在以下示例中,for 语句的主体无法正常终止,因此更新语句 i++ 变得无法访问

package main

func mul(xs []int) int {
	res := 1
	for i := 0; i < len(xs); i++ {
		x := xs[i]
		res *= x
		if res == 0 {
		}
		return 0
	}
	return res
}

最有可能的是,return 语句应该移到 if 语句中

package main

func mulGood(xs []int) int {
	res := 1
	for i := 0; i < len(xs); i++ {
		x := xs[i]
		res *= x
		if res == 0 {
			return 0
		}
	}
	return res
}

参考

  • ©GitHub 公司
  • 条款
  • 隐私