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

Implicit string and byte concatenations should not be confusing

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Message:
      Hide
      When the strings are on multiple lines:
      * "Add a "+" operator to make the string concatenation explicit; or did you forget a comma?"
      When the strings are on the same line:
      * "Merge these implicitly concatenated strings; or did you forget a comma?"
      Show
      When the strings are on multiple lines: * "Add a "+" operator to make the string concatenation explicit; or did you forget a comma?" When the strings are on the same line: * "Merge these implicitly concatenated strings; or did you forget a comma?"
    • Highlighting:
      Hide

      Primary: The end quote of the first concatenated string/bytes literal
      Secondary:

      • location: the starting quote of the next string/bytes/literal
      • no message
      Show
      Primary: The end quote of the first concatenated string/bytes literal Secondary: location: the starting quote of the next string/bytes/literal no message
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Python
    • Irrelevant for Languages:
      ABAP, APEX, C#, C, C++, Cobol, CSS, Flex, Go, HTML, Java, JavaScript, Kotlin, Objective-C, PHP, PL/I, PL/SQL, RPG, Ruby, Rust, Scala, Solidity, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Python concatenates adjacent string or byte literals at compile time. It means that "a" "b" is equivalent to "ab". This is sometimes used to split a long string on multiple lines. However an implicit string concatenation can also be very confusing. In the following contexts it might indicate that a comma was forgotten:

      • when the two strings are on the same line it looks like a badly formatted tuple. Parenthesises are not mandatory to create a tuple, only the comma is.
      • when the strings are in a list, set or tuple.

      Noncompliant Code Example

      def func():
          return "item1" "item2"  # Noncompliant
      
      ["1",
       "2"  # Noncompliant
       "3",
       "a very very very"  # Noncompliant
       "very very long string",
       "4"]
      

      Compliant Solution

      def func():
          return "item1", "item2"
      
      ["1",
       "2",
       "3",
       "a very very very" +
       "very very long string",
       "4"]
      

      Exceptions

      No issue will be raised when there is a visible reason for the string concatenation:

      • when the quotes used for both strings are different. This can be used to avoid escaping quotes
      • when the strings or bytes have different prefixes, i.e. "f" for f-strings, "r" for raw, "u" for unicode and no prefix for normal strings.
      • when strings are visibly split to avoid long lines of code. (Example: the first string ends with a space, punctuation or \n).

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              nicolas.harraudeau Nicolas Harraudeau (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: