Redundant¶
多余条件运算 RedundantConditionalOperator¶
Since: 0.6
检测三种多余的条件运算:
- true表达式得到结果为true,false对应的表达式结果为false;
- true 对应 false ,false 对应 true;
- true,false 对应的结果是一样的.
这种表达式是多余的,应该简化.
定义类: oclint-rules/rules/redundant/RedundantConditionalOperatorRule.cpp
Example:
void example(int a, int b, int c)
{
bool b1 = a > b ? true : false; // true/false: bool b1 = a > b;
bool b2 = a > b ? false : true; // false/true: bool b2 = !(a > b);
int i1 = a > b ? 1 : 1; // same constant: int i1 = 1;
float f1 = a > b ? 1.0 : 1.00; // equally constant: float f1 = 1.0;
int i2 = a > b ? c : c; // same variable: int i2 = c;
}
多余if语句 RedundantIfStatement¶
Since: 0.4
检测多余的if语句.
定义类: oclint-rules/rules/redundant/RedundantIfStatementRule.cpp
Example:
bool example(int a, int b)
{
if (a == b) // this if statement is redundant
{
return true;
}
else
{
return false;
} // the entire method can be simplified to return a == b;
}
多余本地变量RedundantLocalVariable¶
Since: 0.4
检测声明的本地变量直接返回的情况.
定义类: oclint-rules/rules/redundant/RedundantLocalVariableRule.cpp
Example:
int example(int a)
{
int b = a * 2;
return b; // variable b is returned immediately after its declaration,
} // can be simplified to return a * 2;
多余Nil检查 RedundantNilCheck¶
Since: 0.7
C/C++-style null check in Objective-C like foo != nil && [foo bar]
is redundant, since sending a message to a nil object in this case simply returns a false-y value.
定义类: oclint-rules/rules/redundant/RedundantNilCheckRule.cpp
Example:
+ (void)compare:(A *)obj1 withOther:(A *)obj2
{
if (obj1 && [obj1 isEqualTo:obj2]) // if ([obj1 isEqualTo:obj2]) is okay
{
}
}
多余的Else语句 UnnecessaryElseStatement¶
Since: 0.6
当一个IF语句有返回语句,那么else 语句就不是必要的。剩下的语句可以不再代码块中运行。
定义类: oclint-rules/rules/redundant/UnnecessaryElseStatementRule.cpp
Example:
bool example(int a)
{
if (a == 1) // if (a == 1)
{ // {
cout << "a is 1."; // cout << "a is 1.";
return true; // return true;
} // }
else //
{ //
cout << "a is not 1." // cout << "a is not 1."
} //
}
释放内存时多余的Null检查UnnecessaryNullCheckForDealloc¶
Since: 0.8
char* p = 0; delete p;
是有效的. 这个规则会在 if (p)
代码块中检查.
定义类: oclint-rules/rules/redundant/UnnecessaryNullCheckForCXXDeallocRule.cpp
Example:
void m(char* c) {
if (c != nullptr) { // and be simplified to delete c;
delete c;
}
}
无用的括号 UselessParentheses¶
Since: 0.6
检查无用的括号.
定义类: oclint-rules/rules/redundant/UselessParenthesesRule.cpp
Example:
int example(int a)
{
int y = (a + 1); // int y = a + 1;
if ((y > 0)) // if (y > 0)
{
return a;
}
return (0); // return 0;
}