oclint規則 Redundant(沉餘)

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章