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

Regex alternatives should not be redundant

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove or rework this redundant alternative
    • Highlighting:
      Hide

      The redundant alternative

      Show
      The redundant alternative
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      If an alternative in a regular expression only matches things that are already matched by another alternative, that alternative is redundant and serves no purpose.

      In the best case this means that the offending subpattern is merely redundant and should be removed. In the worst case it's a sign that this regex does not match what it was intended to match and should be reworked.

      Noncompliant Code Example

      "[ab]|a"   // The "|a" is redundant because "[ab]" already matches "a"
      ".*|a"     // .* matches everything, so any other alternative is redundant
      

      Compliant Solution

      "[ab]"
      ".*"
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              sebastian.hungerecker Sebastian Hungerecker
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: