oclint规则 Redundant(沉余)

12 篇文章 0 订阅
11 篇文章 0 订阅

Redundant

多余条件运算 RedundantConditionalOperator

Since: 0.6

检测三种多余的条件运算:

  1. true表达式得到结果为true,false对应的表达式结果为false;
  2. true 对应 false ,false 对应 true;
  3. 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值