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

"auto" should not be used as a storage class specifier

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this "auto" storage class specifier
    • Highlighting:
      Hide

      auto

      Show
      auto
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Before C++11, auto was used as a storage class specifier that indicated automatic duration. Since that's the default, the use of auto in that context was wholly redundant.

      Because the keyword was redundant and therefore rarely used, C++11 repurposes it. auto is now used to specify that the type of the variable or function should be deduced from its context.

      Since it is redundant under older standards and problematic under C++11, auto's use as a storage-class identifier should be removed.

      Noncompliant Code Example

      auto int x; // Noncompliant: redundant before C++11, error as of C++11
      
      auto int y;  // Noncompliant
      

      Compliant Solution

      int x;
      
      auto y = 1 + 2; // C++11: type of 'y' will be inferred
      

        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: