Uploaded image for project: 'NetBeans'
  1. NetBeans
  2. NETBEANS-3755

"Can use functional operations" hint creates uncompilable code with nested loops (Unexpected return value)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 11.2
    • None
    • java - Hints
    • None
    • OpenJDK 13+33

    Description

      Consider this code:

      private static boolean deepContains(
              final String needle,
              final Collection<Collection<String>> haystack) {
          for (final Collection<String> hay : haystack) {
              for (final String check : hay) {
                  if (check.equals(needle)) {
                      return true;
                  }
              }
          }
          return false;
      }
      

      NetBeans suggests to replace the outer loop with with a functional operation, resulting in

      private static boolean deepContains(
              final String needle,
              final Collection<Collection<String>> haystack) {
          haystack.forEach((hay) -> {
              for (final String check : hay) {
                  if (check.equals(needle)) {
                      return true;
                  }
              }
          });
          return false;
      }
      

      This does not compile. Error messages:
      For the outer loop:

      incompatible types: unexpected return value

      For the inner loop:
       

      for-each not applicable to expression type
      required: array or java.lang.Iterable
      found: T
      where T is a type-variable:
      T extends Object declared in interface Iterable

      Attachments

        Activity

          People

            Unassigned Unassigned
            striderapache dennis lucero
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: