Uploaded image for project: 'SonarPython'
  1. SonarPython
  2. SONARPY-904

S1066 (CollapsibleIfStatements): Reduce noise when breaking line length limit, when using walrus operator and when a comment is present

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      In order to reduce noise on RSPEC-1066 we should avoid reporting an issue in the following cases:

      • Merging the two if in a single line would break line length limit (we could have 80 as default value)
        if is_first_condition and is_second_condition:
            if re.search(specific_pattern, text_variable, flags=re.IGNORECASE): # merging two ifs in a single line would break line length
               ...
        
      • Using walrus operator
        if match:
            if match2 := re.search(pattern, match.group()):
                ...
        

        In this case we cannot directly merge the two conditions, i.e. we would need to introduce parenthesis around the assignment expression.

      • When a comment is present between the two if
        if cond1:
            # comment specific to cond2
            if cond2:
               ...
        

      Community thread: https://community.sonarsource.com/t/python-merging-if-statement-logic-leads-to-many-false-positives/50595

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            andrea.guarino Andrea Guarino
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: