OCLint的部分规则(Redundant 部分)

40 篇文章 0 订阅
17 篇文章 0 订阅

OCLint的部分规则(Redundant 部分)

对OCLint的部分规则进行简单翻译解释,有部分进行了验证以及进一步分析、测试。OCLint其他相关内容如下:

--
OCLint-iOS-OC项目几种简单使用OCLint的部分规则(Basic 部分)
OCLint的部分规则(Unuseed 部分)OCLint的部分规则(Size 部分)
OCLint的部分规则(Redundant 部分)OCLint的部分规则(Naming 部分)
OCLint的部分规则(Migration 部分)OCLint的部分规则(Empty 部分)
OCLint的部分规则(Design 部分)OCLint的部分规则(Convention 部分)
OCLint的部分规则(CoCoa 部分)



1、redundant conditional operator

      Since:0.6 定义类传送门~点击

This rule detects three types of redundant conditional operators:

  1. true expression and false expression are returning true/false or false/true respectively;
  2. true expression and false expression are the same constant;
  3. true expression and false expression are the same variable expression.

They are usually introduced by mistake, and should be simplified.

简单解释:冗余的条件判断会造成一些错误,应该让它变得简洁。

比如: 1.true对应truefalse对应false

           2 . true对应falsefalse对应true

           3 . truefalse一致。

    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;
    }
2、redundant if statement

      Since:0.4 定义类传送门~点击

This rule detects unnecessary if statements.

简单解释:多余的if判断,可以省略。

    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;
    }
3、redundant local variable

      Since:0.4 定义类传送门~点击

This rule detects cases where a variable declaration is immediately followed by a return of that variable.

简单解释:冗余的局部变量,可以省略,直接return

    int example(int a) {
        int b = a * 2;
        return b;   // variable b is returned immediately after its declaration,
    }
4、redundant nil check

      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.

简单解释:在C或者C++中适用的判空检查在OC中是多余的。因为在OC中向空对象发送消息会返回false值。

    + (void)compare:(A *)obj1 withOther:(A *)obj2  {
        if (obj1 && [obj1 isEqualTo:obj2]) // if ([obj1 isEqualTo:obj2]) is okay   {
        }
    }
5、 unnecessary else statement

      Since:0.6 定义类传送门~点击

When an if statement block ends with a return statement, or all branches in the if statement block end with return statements, then the else statement is unnecessary. The code in the else statement can be run without being in the block.

简单解释:如果if中已经带有return,则不需要写else语句。

    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."
        }                           //
    }
6、unnecessary null check for dealloc

      Since:0.8 定义类传送门~点击

char* p = 0; delete p;isvalid.Thisrulelocatesunnecessaryif (p)checks.

简单解释:在dealloc中不需要判空,就能Delete元素。

    void m(char* c) {
        if (c != nullptr) { // and be simplified to delete c;
            delete c;
        }
    }
7、 useless parentheses

      Since:0.6 定义类传送门~点击

This rule detects useless parentheses.

简单解释:检查无用的括号。

    int example(int a) {
        int y = (a + 1);    // int y = a + 1;
        if ((y > 0))        // if (y > 0)
        {
            return a;
        }
        return (0);         // return 0;
    }

PS:检测了一下在括号里是逻辑判断时不会被检测到,如下(不会被检查到):

    BOOL aaaa = YES;
    BOOL bbbb = NO;
     if((aaaa) && (bbbb)) {
        return YES;
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值