1. ArrayElementAccess(V) : 確保數(shù)組元素通過數(shù)組操作符[ ]獲取。
2.?AvoidEllipses(V):不要使用省略號??梢允褂媚J(rèn)參數(shù)。
? ? 例如:int foo( int a, ... );// Violation?
? ? ? ? ? ? ? ?int foo( int a, int b = 0 );// OK
3.?AvoidFuncDefinitonInClass: 不要在類定義里定義成員函數(shù). 確保接口和實(shí)現(xiàn)分離。函數(shù)定義在類定義內(nèi)是默認(rèn)inline, 而且不緊湊不利于閱讀。
4.?AvoidGotoStatement:不要使用goto
5.?AvoidInlineConstrDestr:不要把構(gòu)造函數(shù)和析構(gòu)函數(shù)定義為inline。function that invoke other inline function often become?too complex for the compiler, and the compiler cannot make them inline.
6.?AvoidStructWithMemberFunction:不要定義包含成員函數(shù)的struct。 函數(shù)應(yīng)該包含在class內(nèi)。
7.?AvoidTernaryOperator:不要使用三元操作符?: 影響可讀性
8.?AvoidVoidParameter: 如果函數(shù)沒有參數(shù),使用()而非(void),聲明一致性
9.?BreakInForLoop: 不要在for循環(huán)內(nèi)使用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:逗號運(yùn)動算符僅僅用在變量聲明/定義或多語句預(yù)處理器宏定義。好處是提高可讀性和防止額外的使用逗號操作符
12.?ConditionCurlyBraces:所有if 和else?statements必須跟隨{ },識別與該語句相關(guān)的代碼。
13.?ConditionCurlyBraces2:確保所有條件語句都使用{} 來識別與之相關(guān)的代碼
14.?ConstParam: 盡可能聲明引用參數(shù)為常量引用。如果函數(shù)不打算改變引用的參數(shù), 可以聲明為常量引用來避免函數(shù)返回值意外修改或防止無意中修改調(diào)用者的數(shù)據(jù)。
15.?ConstPointerFunctionCall:如果指針指向不被改變可以傳const指針
16.?DeclareArrayMagnitude:如果數(shù)組作為函數(shù)的參數(shù)時(shí),不要聲明它的大小。單維數(shù)組的大小永遠(yuǎn)不要在參數(shù)中聲明,多維數(shù)組大小不能全部聲明大小。因?yàn)椴煌瘮?shù)調(diào)用可能傳參不同的大小。
17.?DeclDimArray:不要在數(shù)組初始化的時(shí)候聲明它的大小。因?yàn)榫S護(hù)性較差。
????#define SIZE 4
????int tab1[SIZE] = {1,2,3}; // Violation
? ? ?int tab2[]={1,2,3}; // OK
18.?DefaultInSwitch:不要省略switch 的default分支
19.?DoWhile:不要使用do while,應(yīng)該使用while?
20.?EnumKeyword: 不要用enum關(guān)鍵字聲明一個(gè)變量。
? ??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的每個(gè)成員被明確聲明。
????enum my_enum1
????{
????????a, // Violation
????};;
????enum my_enum1
????{
????????a = 0 // OK
????};;
23.?ExplicitLogicalTests:在條件表達(dá)式中使用明確的邏輯比較。if (isvisible) // Violation
24.?ExplicitlyReturnType:明確函數(shù)返回類型,假如沒有,編譯器會認(rèn)為是void或int。
????foo( ); // Violation
????int foo( );// OK
25.?ExprInSizeof:避免傳給表達(dá)式給sizeof(),例如賦值語句傳給它。
? ??iVar1 = sizeof(iVar2 = 1); // Violation
26.?FalseDefinition:如果FALSE沒有定義,那么 Definition FALSE為0
27.?FalseTypedef:如果FALSE沒有定義,那么typedef FALSE為0?
28.?ForLoopVarAssign:從for語句的body中去除循環(huán)控制變量的賦值。循環(huán)控制變量應(yīng)該僅被修改在for循環(huán)語句的初始化和條件表達(dá)式。在for循環(huán)內(nèi)修改它們使得循環(huán)條件很難理解和可能出現(xiàn)邏輯缺陷。
29.?FuncModifyGlobalVar:避免函數(shù)修改global變量
30.?GlobalVarFound:避免聲明global變量
31.?HardCodeValue:使用“#define” 或enum常量代替hard coded values(魔幻數(shù)字)
32.?HardCodeValueWithZero:
33.?IfElseWhileFollowBlock:循環(huán)語句即使為空也要加大括號(with block)
34.?MacroWithinInclude: 不要在include聲明語句中使用宏定義()
? ??#define HEADER_FILE(nr) "myHeader" #nr ".h"
????#include HEADER_FILE(12) //Violation
35.?ModifyInCondition: 不要在if while switch條件表達(dá)式中使用++ -- 操作符
36.?NullDefinition: 定義NULL “(void*)0”
37.?OnlyOneReturn:函數(shù)最多有一個(gè)return 語句
38.?PartialStatementDefinition:不要定義生命的一部分
? ??#define PARTIAL(a)? ? ((a) * // Violation
39.?PassByValue:
40.?RedefControlStatements:不要重新定義循環(huán)控制語句if while try
41.?RedefPrimitiveTypes: 不要redefine原始數(shù)據(jù)類型char?
????#define T30 char //Violation
????#define T31 unsigned char // OK
42.?RedefTypes:
43.?SepareLogicalTests:使用明確的邏輯表達(dá)式
44.?SingularSwitchStatement:避免switch只有一條case語句,如果這樣應(yīng)該改寫成if else。
45.SourceFileSize:避免source file超過500行
46.?StructKeyword:不要使用struct關(guān)鍵字來聲明一個(gè)變量
????Example:
????struct Position_t {};
????struct Position_t Pos; // Violation
????Repair:
????struct Position_t {};
????Position_t Pos; // OK
47.?TrueDefinition:如果TRUE需要被定義,那么應(yīng)該定義為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函數(shù)內(nèi)去掉不必要的==true
????bool foo()
????{
????????return isPositive(5) == true; // Violation
????????return isPositive(5); // OK
????}
51.?UseParenthesisInMacros: 在宏定義表達(dá)式中, 應(yīng)該在乘除號之前和之后加上括號。
? ??#define MULTI_1(x)? ? (x * (x)) //Violation
? ??#define MULTI_3(x)? ? ?((x) * (x)) //OK
52.?UsePositiveLogic:使用正邏輯而非賦邏輯
53.?UseTypedefForFuncPointers: 函數(shù)指針應(yīng)該typedef
54.?StructureTypedef:所有結(jié)構(gòu)體應(yīng)該有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:這個(gè)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。 操作符重載應(yīng)該聲明為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 變量/參數(shù) 和class/parent class/parent struct 成員變量應(yīng)該是不同的名字。
62.?LocalAndMemberNamesDifferByOneCharOnly: