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

Map values should not be replaced unconditionally

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Verify this is the [key|index] that was intended; a value has already been saved for it on line n.
    • Highlighting:
      • primary: 2nd storage for key
      • secondary: 1st storage for key
        • message: Original value
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      APEX, Cobol, Flex, Go, Kotlin, Ruby, Rust, Scala, Solidity, VB6
    • Covered Languages:
      C#, C, C++, Java, JavaScript, Objective-C, PHP, Python, Swift, TypeScript, VB.Net
    • Irrelevant for Languages:
      ABAP, CSS, HTML, PL/I, PL/SQL, RPG, T-SQL, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Control-flow Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • ESLint-SonarJS:
      no-duplicated-branches
    • TSLint-SonarTS:
      no-element-overwrite

      Description

      It is highly suspicious when a value is saved for a key or index and then unconditionally overwritten. Such replacements are likely errors.

      Noncompliant Code Example

      letters.put("a", "Apple");
      letters.put("a", "Boy");  // Noncompliant
      
      towns[i] = "London";
      towns[i] = "Chicago";  // Noncompliant
      

        Attachments

          Issue Links

            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: