title: Unchecked casts and unnecessary suppressed warnings with lambdas
body: <p>While working with Lambdas and generics, I encountered a special case of unsafe cast warnings.</p>
  
  <p>During reproducing and making an SSCCE, I found that it is probably related to the fact that the lambda is effectively "inside" the return statement.</p>
  
  <p>The question is: <strong>Why do I get a warning in the <code>warningUnnecessarySuppressWarnings</code> method?</strong>. When <em>removing</em> <code>@SuppressWarnings("unchecked")</code>, I get:</p>
  
  <blockquote>
    <p>Type safety: Unchecked cast from List to List</p>
  </blockquote>
  
  <p>As shown in the <code>warningUnsafeCast</code> method. Because of this, the annotation is not unnecessary as the new warning says.</p>
  
  <p>I am using Eclipse Kepler SP2 for Java EE Developers, with Build id: 20140224-0627</p>
  
  <p>Also using <a href="http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler">the recommended</a> update-site for <a href="http://download.eclipse.org/eclipse/updates/4.3-P-builds/">Java 8 support in Eclipse Kepler</a></p>
  
  <pre><code>public static void main(String[] args) {
      System.out.println(warningUnnecessarySuppressWarnings());
      System.out.println(warningUnsafeCast());
      System.out.println(withoutWarning());
  }
  
  private static Integer perform(Function&lt;List&lt;?&gt;, Integer&gt; func) {
      return func.apply(Arrays.asList("a", "b", "c"));
  }
  
  private static Integer warningUnnecessarySuppressWarnings() {
      return perform(list -&gt; {
          @SuppressWarnings("unchecked") // Unnecessary @SuppressWarnings("unchecked")
          List&lt;String&gt; unsafeCast = (List&lt;String&gt;) list;
          return unsafeCast.size();
      });
  }
  
  private static Integer warningUnsafeCast() {
      return perform(list -&gt; {
          List&lt;String&gt; unsafeCast = (List&lt;String&gt;) list; // Type safety: Unchecked cast from List&lt;capture#4-of ?&gt; to List&lt;String&gt;
          return unsafeCast.size();
      });
  }
  
  @SuppressWarnings("unchecked")
  private static Integer withoutWarning() {
      return perform(list -&gt; {
          List&lt;String&gt; unsafeCast = (List&lt;String&gt;) list;
          return unsafeCast.size();
      });
  }
  </code></pre>