1. ArrayElementAccess(V) : 確保數組元素通過數組操作符[ ]獲取。
2.?AvoidEllipses(V):不要使用省略號。可以使用默認參數。
? ? 例如:int foo( int a, ... );// Violation?
? ? ? ? ? ? ? ?int foo( int a, int b = 0 );// OK
3.?AvoidFuncDefinitonInClass: 不要在類定義里定義成員函數. 確保接口和實現分離。函數定義在類定義內是默認inline, 而且不緊湊不利于閱讀。
4.?AvoidGotoStatement:不要使用goto
5.?AvoidInlineConstrDestr:不要把構造函數和析構函數定義為inline。function that invoke other inline function often become?too complex for the compiler, and the compiler cannot make them inline.
6.?AvoidStructWithMemberFunction:不要定義包含成員函數的struct。 函數應該包含在class內。
7.?AvoidTernaryOperator:不要使用三元操作符?: 影響可讀性
8.?AvoidVoidParameter: 如果函數沒有參數,使用()而非(void),聲明一致性
9.?BreakInForLoop: 不要在for循環內使用break
10.?CastFuncPtrToPrimPtr:?Do not type cast pointers to primitive types for variables that have
been declared as pointers to functions。
????This rule detects if you type case pointers to primitive types for variables that have been declared as
pointers to functions.
????Benefits:Following this rule helps you make code less error-prone; casting pointers to functions to pointers of
primitive types can result in errors.
Example:
void Foo(char *ptrC)
{
????*ptrC = 0;
????return;
}
void f()
{
????void *ptrV = 0;
????void (*funPtr) (char*) = 0;
????funPtr = &Foo;
????ptrV = (void*)funPtr; // Violation
????return;
}
Output:Pointer to a function is cast to a pointer of primitive type
11. CommaOperator:逗號運動算符僅僅用在變量聲明/定義或多語句預處理器宏定義。好處是提高可讀性和防止額外的使用逗號操作符
12.?ConditionCurlyBraces:所有if 和else?statements必須跟隨{ },識別與該語句相關的代碼。
13.?ConditionCurlyBraces2:確保所有條件語句都使用{} 來識別與之相關的代碼
14.?ConstParam: 盡可能聲明引用參數為常量引用。如果函數不打算改變引用的參數, 可以聲明為常量引用來避免函數返回值意外修改或防止無意中修改調用者的數據。
15.?ConstPointerFunctionCall:如果指針指向不被改變可以傳const指針
16.?DeclareArrayMagnitude:如果數組作為函數的參數時,不要聲明它的大小。單維數組的大小永遠不要在參數中聲明,多維數組大小不能全部聲明大小。因為不同函數調用可能傳參不同的大小。
17.?DeclDimArray:不要在數組初始化的時候聲明它的大小。因為維護性較差。
????#define SIZE 4
????int tab1[SIZE] = {1,2,3}; // Violation
? ? ?int tab2[]={1,2,3}; // OK
18.?DefaultInSwitch:不要省略switch 的default分支
19.?DoWhile:不要使用do while,應該使用while?
20.?EnumKeyword: 不要用enum關鍵字聲明一個變量。
? ??enum Colors { RED, BLUE, GREEN };
????enum Colors c; // Violation
????enum Colors { RED, BLUE, GREEN };
????Colors c; // OK
21.?EOSUsage:使用EOS define代替直接使用“\0”作為字符串的終止符。
? ??#define EOS '\0';
????char str[30] = "Sample text.";
????str[7] = '\0'; // Violation
? ??str[7] = EOS; // OK
22.?ExplicitEnumValues:確保enum的每個成員被明確聲明。
????enum my_enum1
????{
????????a, // Violation
????};;
????enum my_enum1
????{
????????a = 0 // OK
????};;
23.?ExplicitLogicalTests:在條件表達式中使用明確的邏輯比較。if (isvisible) // Violation
24.?ExplicitlyReturnType:明確函數返回類型,假如沒有,編譯器會認為是void或int。
????foo( ); // Violation
????int foo( );// OK
25.?ExprInSizeof:避免傳給表達式給sizeof(),例如賦值語句傳給它。
? ??iVar1 = sizeof(iVar2 = 1); // Violation
26.?FalseDefinition:如果FALSE沒有定義,那么 Definition FALSE為0
27.?FalseTypedef:如果FALSE沒有定義,那么typedef FALSE為0?
28.?ForLoopVarAssign:從for語句的body中去除循環控制變量的賦值。循環控制變量應該僅被修改在for循環語句的初始化和條件表達式。在for循環內修改它們使得循環條件很難理解和可能出現邏輯缺陷。
29.?FuncModifyGlobalVar:避免函數修改global變量
30.?GlobalVarFound:避免聲明global變量
31.?HardCodeValue:使用“#define” 或enum常量代替hard coded values(魔幻數字)
32.?HardCodeValueWithZero:
33.?IfElseWhileFollowBlock:循環語句即使為空也要加大括號(with block)
34.?MacroWithinInclude: 不要在include聲明語句中使用宏定義()
? ??#define HEADER_FILE(nr) "myHeader" #nr ".h"
????#include HEADER_FILE(12) //Violation
35.?ModifyInCondition: 不要在if while switch條件表達式中使用++ -- 操作符
36.?NullDefinition: 定義NULL “(void*)0”
37.?OnlyOneReturn:函數最多有一個return 語句
38.?PartialStatementDefinition:不要定義生命的一部分
? ??#define PARTIAL(a)? ? ((a) * // Violation
39.?PassByValue:
40.?RedefControlStatements:不要重新定義循環控制語句if while try
41.?RedefPrimitiveTypes: 不要redefine原始數據類型char?
????#define T30 char //Violation
????#define T31 unsigned char // OK
42.?RedefTypes:
43.?SepareLogicalTests:使用明確的邏輯表達式
44.?SingularSwitchStatement:避免switch只有一條case語句,如果這樣應該改寫成if else。
45.SourceFileSize:避免source file超過500行
46.?StructKeyword:不要使用struct關鍵字來聲明一個變量
????Example:
????struct Position_t {};
????struct Position_t Pos; // Violation
????Repair:
????struct Position_t {};
????Position_t Pos; // OK
47.?TrueDefinition:如果TRUE需要被定義,那么應該定義為1
48.?TrueTypedef:
49.?TypeModifiersAfterTypes:?Ensure that storage type modifiers are associated with the type, not the variable
? ??int static i; //Violation
? ??static char j; //OK
50.?UnnecessaryEqual:在bool函數內去掉不必要的==true
????bool foo()
????{
????????return isPositive(5) == true; // Violation
????????return isPositive(5); // OK
????}
51.?UseParenthesisInMacros: 在宏定義表達式中, 應該在乘除號之前和之后加上括號。
? ??#define MULTI_1(x)? ? (x * (x)) //Violation
? ??#define MULTI_3(x)? ? ?((x) * (x)) //OK
52.?UsePositiveLogic:使用正邏輯而非賦邏輯
53.?UseTypedefForFuncPointers: 函數指針應該typedef
54.?StructureTypedef:所有結構體應該有typedefs
? ??Example
????struct A// Violation - no typedef
????{
????????int i;
????};
????Repair
????typedef struct A // OK
????{
????????int i;
????} A_t;
????or
????struct A // OK
????{
????????int i;
????};
????typedef struct A A_t;
????note:這個rule僅僅是用于c。
55.?UsingVoid:void should be used when a function is passed or returns no values。
56.?CFilesIncludingCFiles:?.c files may not include other .c files。Many build environments are set up to automatically compile every .c file. If in addition a .c file is included in another .c file, this will result in link errors.
57.?SelectorOperatorsShouldBeConst:Conversion operator, operator->, operator(), operator[] should be const。 操作符重載應該聲明為const。
class A {
public:
????A& operator()( int x ); // Violation
????A& operator[]( int x ); // Violation
????A& operator->( int x ); // Violation
????operator int( ); // Violation
};
Repair
class A {
public:
????A& operator()( int x ) const; // OK
????A& operator[]( int x ) const; // OK
????A& operator->( int x ) const; // OK
????operator int( ) const; // OK
};
60.?OperatorsShouldBeConst:Bitwise operators, comparison operators, logical operators, comma?operator should be const。?presumes that they do not change the internals of the object they are called on. Therefore, it is a good practice to declare them const.
61.?LocalAndMemberNamesDifferByCaseOnly: local 變量/參數 和class/parent class/parent struct 成員變量應該是不同的名字。
62.?LocalAndMemberNamesDifferByOneCharOnly: