Basic¶
条件位运算BitwiseOperatorInConditional¶
Since: 0.6
位运算是不利于后期维护和理解的,虽然有时候这样计算速度比较快
定义类: oclint-rules/rules/basic/BitwiseOperatorInConditionalRule.cpp
Example:
void example(int a, int b)
{
if (a | b)
{
}
if (a & b)
{
}
}
错误Null检查BrokenNullCheck¶
Since: 0.7
错误的空检查会让程序崩溃
定义类: oclint-rules/rules/basic/BrokenNullCheckRule.cpp
Example:
void m(A *a, B *b)
{
if (a != NULL || a->bar(b))
{
}
if (a == NULL && a->bar(b))
{
}
}
错误Nil检查BrokenNilCheck¶
Since: 0.7
在某些情况下,在Objective-C中破零检查返回结果刚好相反。
定义类: oclint-rules/rules/basic/BrokenNullCheckRule.cpp
Example:
+ (void)compare:(A *)obj1 withOther:(A *)obj2
{
if (obj1 || [obj1 isEqualTo:obj2])
{
}
if (!obj1 && ![obj1 isEqualTo:obj2])
{
}
}
BrokenOddnessCheck¶
Since: 0.6
检查 x % 2 == 1
如果是负数,就会出现异常. 应该使用 x & 1 == 1
, or x % 2 != 0
替换.
定义类: oclint-rules/rules/basic/BrokenOddnessCheckRule.cpp
Example:
void example()
{
if (x % 2 == 1) // violation
{
}
if (foo() % 2 == 1) // violation
{
}
}
可以合并的IF语句 CollapsibleIfStatements¶
Since: 0.6
可以合并的连续两个if,应该合并,提高代码的可读性.
定义类: oclint-rules/rules/basic/CollapsibleIfStatementsRule.cpp
Example:
void example(bool x, bool y)
{
if (x) // these two if statements can be
{
if (y) // combined to if (x && y)
{
foo();
}
}
}
恒定条件运算 ConstantConditionalOperator¶
Since: 0.6
条件运算符
结果已经确定的条件运算.
定义类: oclint-rules/rules/basic/ConstantConditionalOperatorRule.cpp
Example:
void example()
{
int a = 1 == 1 ? 1 : 0; // 1 == 1 is actually always true
}
IF表达式为确定ConstantIfExpression¶
Since: 0.2
if
表达式的条件是已经确定的true
或者else
定义类: oclint-rules/rules/basic/ConstantIfExpressionRule.cpp
Example:
void example()
{
if (true) // always true
{
foo();
}
if (1 == 0) // always false
{
bar();
}
}
无效代码DeadCode¶
Since: 0.4
在 return
, break
, continue
, and throw
之后的代码都是无效的
定义类: oclint-rules/rules/basic/DeadCodeRule.cpp
Example:
void example(id collection)
{
for (id it in collection)
{
continue;
int i1; // dead code
}
return;
int i2; // dead code
}
双重否定 DoubleNegative¶
Since: 0.6
使用双重否定没有意义
定义类: oclint-rules/rules/basic/DoubleNegativeRule.cpp
Example:
void example()
{
int b1 = !!1;
int b2 = ~~1;
}
For应该转换为While ForLoopShouldBeWhileLoop¶
Since: 0.6
一些情况下For循环应该转换为While,使代码更简洁
定义类: oclint-rules/rules/basic/ForLoopShouldBeWhileLoopRule.cpp
Example:
void example(int a)
{
for (; a < 100;)
{
foo(a);
}
}
Goto语句 GotoStatement¶
Since: 0.6
定义类: oclint-rules/rules/basic/GotoStatementRule.cpp
Example:
void example()
{
A:
a();
goto A; // Considered Harmful
}
References:
Edsger Dijkstra (March 1968). “Go To Statement Considered Harmful”. Communications of the ACM (PDF) 11 (3): 147–148. doi:10.1145/362929.362947.
混乱的增量 JumbledIncrementer¶
Since: 0.7
混乱的增量计算,使代码很难阅读
定义类: oclint-rules/rules/basic/JumbledIncrementerRule.cpp
Example:
void aMethod(int a) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; i++) { // references both 'i' and 'j'
}
}
}
错位的Null检查 MisplacedNullCheck¶
Since: 0.7
(空检查应该在其他运算之前)The null check is misplaced. In C and C++, sending a message to a null pointer could crash the app. When null is misplaced, either the check is useless or it’s incorrect.
This rule is defined by the following class: oclint-rules/rules/basic/MisplacedNullCheckRule.cpp
Example:
void m(A *a, B *b)
{
if (a->bar(b) && a != NULL) // violation
{
}
if (a->bar(b) || !a) // violation
{
}
}
错位的Nil检查 MisplacedNilCheck¶
Since: 0.7
(nil检查应该在其他运算之前)The nil check is misplaced. In Objective-C, sending a message to a nil pointer simply does nothing. But code readers may be confused about the misplaced nil check.
This rule is defined by the following class: oclint-rules/rules/basic/MisplacedNilCheckRule.cpp
Example:
+ (void)compare:(A *)obj1 withOther:(A *)obj2
{
if ([obj1 isEqualTo:obj2] && obj1)
{
}
if (![obj1 isEqualTo:obj2] || obj1 == nil)
{
}
}
多余运算符 MultipleUnaryOperator¶
Since: 0.6
多余的运算符应该简化,便于阅读
This rule is defined by the following class: oclint-rules/rules/basic/MultipleUnaryOperatorRule.cpp
Example:
void example()
{
int b = -(+(!(~1)));
}
从Finally 返回 ReturnFromFinallyBlock¶
Since: 0.6
不建议从Finally 返回
定义类: oclint-rules/rules/basic/ReturnFromFinallyBlockRule.cpp
Example:
void example()
{
@try
{
foo();
}
@catch(id ex)
{
bar();
}
@finally
{
return; // this can discard exceptions.
}
}
Finally抛出异常 ThrowExceptionFromFinallyBlock¶
Since: 0.6
从Finally块抛出异常,可能掩盖其他的错误
定义类: oclint-rules/rules/basic/ThrowExceptionFromFinallyBlockRule.cpp
Example:
void example()
{
@try {;}
@catch(id ex) {;}
@finally {
id ex1;
@throw ex1; // this throws an exception
NSException *ex2 = [NSException new];
[ex2 raise]; // this throws an exception, too
}
}