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

Collection and array contents should be used

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Either use this collection's contents or remove the collection.
    • Highlighting:
      Hide

      primary: instantiation
      secondary: population/depopulation

      Show
      primary: instantiation secondary: population/depopulation
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      C#, C, C++, Java, Objective-C, PHP, Python, Swift, VB.Net
    • Covered Languages:
      JavaScript, TypeScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources
    • TSLint-SonarTS:
      no-unused-array

      Description

      When a collection is populated but its contents are never used, then it is surely some kind of mistake. Either refactoring has rendered the collection moot, or an access is missing.

      This rule raises an issue when no methods are called on a collection other than those that add or remove values.

      Noncompliant Code Example

      public int getLength(String a, String b, String c) {
        ArrayList<String> strings = new ArrayList<>();  // Noncompliant
        strings.add(a);
        strings.add(b);
        strings.add(c);
      
        return a.length() + b.length() + c.length();
      }
      

      Compliant Solution

      public int getLength(String a, String b, String c) {
        return a.length() + b.length() + c.length();
      }
      

        Attachments

          Issue Links

          1.
          JavaScript RSPEC-4291 Language-Specification Active Unassigned

            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: