linter warning: return value is ignored

huangapple go评论69阅读模式
英文:

linter warning: return value is ignored

问题

go-staticcheck发出一个警告:忽略了返回值。这个问题只出现在循环和特定条件内。让我澄清一下:

for _, key := range []string{"my-foo", "bar", "baz"} {
  if strings.HasPrefix(key, "my") {
    fmt.Println(true)
  }
}

到目前为止,代码检查器没有报错。但是当我移除 fmt 并使用 continue 时,它会报错:

for _, key := range []string{"my-foo", "bar", "baz"} {
  if strings.HasPrefix(key, "my") {
    continue
  }
}

HasPrefix 是一个纯函数,但它的返回值被忽略了。

这个问题在使用 strings.Contains() 时不会出现,只有在循环中使用 strings.HasPrefix() 时才会出现。


我还尝试了这样的写法:

for _, key := range []string{"my-foo", "bar", "baz"} {
  hasMy := strings.HasPrefix(key, "my")
  if hasMy {
    continue
  }
}

现在,它会提示两个问题:

hasMy 的值从未被使用

HasPrefix 是一个纯函数,但它的返回值被忽略了


编辑:

我发现很难忽略这个问题,因此在这里粘贴以供自己参考。如果我们需要忽略类似的代码检查问题,可以这样写:

for _, key := range []string{"my-foo", "bar", "baz"} {
  //lint:ignore SA4017 // 忽略这个问题
  if strings.HasPrefix(key, "my") {
    continue
  }
}

注意://lint 之间没有空格。

英文:

go-staticcheck issueing a warning: return value is ignored. This issue appears only inside a loop and specific condition. Let me clarify this:

for _, key := range []string{"my-foo", "bar", "baz"} {
  if strings.HasPrefix(key, "my") {
    fmt.Println(true)
  }
}

Until now, linter doesn't throw an issue. But when I remove fmt and use continue it throws issue:

for _, key := range []string{"my-foo", "bar", "baz"} {
  if strings.HasPrefix(key, "my") {
    continue
  }
}

> HasPrefix is a pure function but its return value is ignored

The issue doesn't appear with strings.Contains(). It only appears with strings.HasPrefix() and only when use continue in the loop.


I also tried like this:

for _, key := range []string{"my-foo", "bar", "baz"} {
  hasMy := strings.HasPrefix(key, "my")
  if hasMy {
    continue
  }
}

Now, it lints 2 issues:
> this value of hasMy is never used
>
> HasPrefix is a pure function but its return value is ignored

Edit:

I was finding hard to ignore and thus pasting here for self reference. In case we need to ignore similar linter issue.

for _, key := range []string{"my-foo", "bar", "baz"} {
  //lint:ignore SA4017 // ignore this
  if strings.HasPrefix(key, "my") {
    continue
  }
}

Note: No space in between //lint.

答案1

得分: 2

你的continue只是在循环中回到上一次,而且没有其他代码依赖于HasPrefix的结果(一个bool值),所以实际上你只是浪费了那个调用。纯函数是指不产生副作用的函数,因此linter知道在这种情况下,条件语句可以完全删除而不会产生任何影响。

如果在那个if语句之后添加一个else,你会发现lint警告消失了(在我的机器上,错误消息略有不同,但可能是不同版本的staticcheck)。

for _, key := range []string{"my-foo", "bar", "baz"} {
	if strings.HasPrefix(key, "my") {
		continue
	} else {
		fmt.Println("hello")
	}
}

或者,如果你在检查中添加其他内容,就像你的第一个版本中那样:

for _, key := range []string{"my-foo", "bar", "baz"} {
	if strings.HasPrefix(key, "my") {
		fmt.Println("yep")
		continue
	}
}

对于使用变量存储HasPrefix结果的第二个版本,同样适用。

英文:

Your continue is just running back through the loop, and there's no other code depending on the result of the HasPrefix (a bool), so in effect you're just wasting that call. A pure function is one that performs no side effects, so the linter knows that in this case that conditional could be removed entirely and wouldn't make any difference.

If you add an else after that if statement, you can see the lint warning go away (on my machine the error message is slightly different, but that could be a different version of staticcheck).

for _, key := range []string{"my-foo", "bar", "baz"} {
	if strings.HasPrefix(key, "my") {
		continue
	} else {
		fmt.Println("hello")
	}
}

Or if you add something else inside the check as in your first version:

for _, key := range []string{"my-foo", "bar", "baz"} {
	if strings.HasPrefix(key, "my") {
		fmt.Println("yep")
		continue
	}
}

And the same applies to your second version using a variable to store the result of the HasPrefix.

huangapple
  • 本文由 发表于 2023年5月8日 07:10:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76196643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定