Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
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
- C++ Core Guidelines ES.11 - Use auto to avoid redundant repetition of type names
Attachments
Issue Links
- is implemented by
-
CPP-1437 Rule S5827: "auto" should be used to avoid repetition of types
-
- Closed
-
- is related to
-
CPP-2956 S5827: Using a concept auto for a variable should not trigger the rule
-
- Closed
-
-
CPP-2792 S5827: Fix false positive with macros
-
- Open
-
-
CPP-3086 S5827: Fix false-negative on parenthesized expression in initializer
-
- Closed
-
-
CPP-3253 S5827: False negative for list-initialized objects
-
- Closed
-
-
RSPEC-6234 "auto" should be used to store a result of functions that conventionally return an iterator
- Active
- relates to
-
CPP-2682 Rule S6234: "auto" should be used to store a result of functions that conventionally return an iterator
-
- Closed
-