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

String literals should not be concatenated implicitly

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Merge this literal with previous.
    • Highlighting:
      Hide

      The second literal

      Show
      The second literal
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++, Objective-C
    • Irrelevant for Languages:
      ABAP, C#, Cobol, CSS, Flex, HTML, Java, JavaScript, PHP, PL/I, PL/SQL, Python, RPG, Swift, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      While in C, and derived languages, it is legal to concatenate two literals by putting them next to each other, this is only justified in a few cases. For instance if one is a macro or if the layout makes it clearer.

      Noncompliant Code Example

      const char * v1 = "a""b";      // Noncompliant; same as "ab"
      const char * v2 = "a\n" "b\n"; // Noncompliant
      

      Compliant Solution

      const char * v1 = "ab"
      const char * v2 = "a\n"
                        "b\n";
      

      Exceptions

      const char * v3 = "a" /* comment */ "b";
      
      #define _s "b"
      const char * v4 = "a" _s; // concatenation with macro ignored
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jeanchristophe.collet Jean-Christophe Collet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: