14

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"))?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
  • Use foreach loop if you can. Its better for performance. – Toochka Apr 17 '14 at 07:51
  • 1
    @Toochka You can't use foreach loop on Android. – tomrozb Apr 17 '14 at 08:02
  • I'm using it successfully – Toochka Apr 18 '14 at 07:17
  • Oh you meant foreach loop, not foreach method from Iterable (http://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-). Yes of course you can use it on Android, but this question doesn't concern foreach loop. – tomrozb Apr 18 '14 at 13:15
  • See http://stackoverflow.com/questions/256859/is-there-a-performance-difference-between-a-for-loop-and-a-for-each-loop. As conclusion: `there is no performance penalty for using the for-each loop` – Grigory Kislin Sep 28 '16 at 10:31

2 Answers2

14

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

screenshot of inspection edit option from quick-fix

Shorn
  • 19,077
  • 15
  • 90
  • 168
  • Thanks! It works. I've also found that one can use " //noinspection Convert2streamapi" to disable inspection for single line or @SuppressWarnings("Convert2streamapi") to disable it for entire code block. – tomrozb Apr 17 '14 at 09:42
  • I can't see this option in any context menu on the Mac, using IJ Ultimate 2020.2. – Stevey Aug 12 '20 at 21:42
8

Use this

@SuppressWarnings("ForLoopReplaceableByForEach")

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

Vlad
  • 7,997
  • 3
  • 56
  • 43