Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3741

Don't use a type parameter when a wildcard is enough

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this type parameter and use a wildcard instead
    • Highlighting:
      Hide

      Type parameter declaration

      Show
      Type parameter declaration
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      There is no need to declare a type parameter when naming a type constraint is not required. Using wildcards makes it easier to read.

      Noncompliant Code Example

      <T extends MyClass> void foo(List<T> list) {  // Noncompliant, T is used only once
        for (MyClass myObj : list) {
          doSomething(myObj);
        }
      }
      

      Compliant Solution

      void foo(List<? extends MyClass> list) {
        for (MyClass myObj : list) {
          doSomething(myObj);
        }
      }
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            nicolas.peru Nicolas Peru
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated: