Details

    • Type: Language-Specification
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Impact:
      Unknown 'null' severity
    • Likelihood:
      Unknown 'null' severity

      Description

      Noncompliant Code Example

      public void example() {
        for (Foo obj : (List<Foo>) getFoos()) {  // Noncompliant; cast unnecessary because List<Foo> is what's returned
          //...
        }
      }
      
      public List<Foo> getFoos() {
        return this.foos;
      }
      

      Compliant Solution

      public void example() {
        for (Foo obj : getFoos()) {
          //...
        }
      }
      
      public List<Foo> getFoos() {
        return this.foos;
      }
      

      Exceptions

      Casting may be required to distinguish the method to call in the case of overloading:

      class A {}
      class B extends A{}
      class C {
        void fun(A a){}
        void fun(B b){}
        
        void foo() {
          B b = new B();
          fun(b);
          fun((A) b); //call the first method so cast is not redundant.
        }
      
      }
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: