How to suppress “Can be replaced with foreach call” warning

和自甴很熟 提交于 2019-12-23 07:06:18

问题


I'm using Java 8 with Android Studio and Retrolambda plugin to compile lambdas to anonymous classes (because Java 8 is not supported on Android). The IDE shows me warnings (or tips) how to change my code to use all the features of Java 8. One of these features is "Can be replaced with foreach call" when you loop over collection. I want to suppress this kind of warning, but I can't figure out how to do this.

The simplest solution to suppress this kind of warning is the @SuppressWarnings("all") annotation. But I want to be warned about different types of warnings so this is not the solution.

Is there any way to disable this kind of warning for entire IDE or just for the code block (something like @SuppressWarnings("foreach"))?


回答1:


One way is to configure your intention settings (I'm assuming this is doable with Android Studio, I'm an IntelliJ user).

For this specific intention:

  • put your cursor on the "for" keyword
  • press "alt+enter" (or maybe "option+enter" or something like that on a mac).
  • press the right arrow, select "edit inspection profile setting" and turn it off or customise to your liking




回答2:


Use this

@SuppressWarnings("ForLoopReplaceableByForEach")

Useful reference: https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html



来源:https://stackoverflow.com/questions/23127445/how-to-suppress-can-be-replaced-with-foreach-call-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!