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

"auto" should be used to avoid repetition of types

    XMLWordPrintable

    Details

    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C++
    • Irrelevant for Languages:
      ABAP, APEX, C#, C, Cobol, CSS, Flex, Go, HTML, Java, JavaScript, Kotlin, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Ruby, Rust, Scala, Solidity, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources
    • CppCoreGuidelines:
      ES.11

      Description

      When used as a type specifier in a declaration, auto allows the compiler to deduce the type of a variable based on the type of the initialization expression.

      When the spelling of the initialization expression already contains the type of the declared variable, it leaves no ambiguity and auto should be used as it makes the code easier to read and reduces duplication. This includes initializations using new, template factory functions for smart pointers and cast expressions.

      Noncompliant Code Example

      #include <memory>
      #include <vector>
      
      class C {};
      class LongAndBoringClassName : public C {};
      
      void f() {
        LongAndBoringClassName *newClass1 = new LongAndBoringClassName(); // Noncompliant
        LongAndBoringClassName *newClass2 = new LongAndBoringClassName(); // Noncompliant
      
        std::unique_ptr<LongAndBoringClassName> newClass3 = std::make_unique<LongAndBoringClassName>(); // Noncompliant
        std::shared_ptr<LongAndBoringClassName> newClass4 = std::make_shared<LongAndBoringClassName>(); // Noncompliant
      
        C* c = new LongAndBoringClassName(); // Compliant
        LongAndBoringClassName *newClass5 = static_cast<LongAndBoringClassName*>(c); // Noncompliant
      }
      

      Compliant Solution

      #include <memory>
      #include <vector>
      
      class C {};
      class LongAndBoringClassName : public C {};
      
      void f() {
        auto newClass1 = new LongAndBoringClassName(); // Compliant
        auto *newClass2 = new LongAndBoringClassName(); // Compliant
      
        auto newClass3 = std::make_unique<LongAndBoringClassName>(); // Compliant
        auto newClass4 = std::make_shared<LongAndBoringClassName>(); // Compliant
      
        C* c = new LongAndBoringClassName(); // Compliant
        auto newClass5 = static_cast<LongAndBoringClassName*>(c); // Compliant
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              geoffray.adde Geoffray Adde
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: