Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-1125

Boolean literals should not be redundant

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove the unnecessary Boolean literal(s).
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      BooleanLiteralComparisonCheck
    • Targeted languages:
      C++, Cobol, Python, T-SQL
    • Covered Languages:
      C#, Flex, Go, Java, JavaScript, Kotlin, PHP, PL/SQL, Scala, Swift, TypeScript, VB.Net, VB6
    • Irrelevant for Languages:
      ABAP, C, HTML, Objective-C, PL/I, RPG, Ruby, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • ESLint:
      no-unneeded-ternary
    • ESLint-SonarJS:
      no-redundant-boolean
    • PMD:
      SimplifyBooleanExpressions, SimplifiedTernary
    • ReSharper:
      SimplifyConditionalTernaryExpression, RedundantBoolCompare, RedundantLogicalConditionalExpressionOperand, RedundantTernaryExpression, ForStatementConditionIsTrue
    • TSLint-SonarTS:
      no-redundant-boolean

      Description

      Redundant Boolean literals should be removed from expressions to improve readability.

      Noncompliant Code Example

      if (booleanMethod() == true) { /* ... */ }
      if (booleanMethod() == false) { /* ... */ }
      if (booleanMethod() || false) { /* ... */ }
      doSomething(!false);
      doSomething(booleanMethod() == true);
      
      booleanVariable = booleanMethod() ? true : false;
      booleanVariable = booleanMethod() ? true : exp;
      booleanVariable = booleanMethod() ? false : exp;
      booleanVariable = booleanMethod() ? exp : true;
      booleanVariable = booleanMethod() ? exp : false;
      

      Compliant Solution

      if (booleanMethod()) { /* ... */ }        
      if (!booleanMethod()) { /* ... */ }
      if (booleanMethod()) { /* ... */ }
      doSomething(true);
      doSomething(booleanMethod());
      
      booleanVariable = booleanMethod();
      booleanVariable = booleanMethod() || exp;
      booleanVariable = !booleanMethod() && exp;
      booleanVariable = !booleanMethod() || exp;
      booleanVariable = booleanMethod() && exp;
      

        Attachments

          Issue Links

          1.
          PHP RSPEC-1975 Language-Specification Active Unassigned
          2.
          PHP RSPEC-2081 Language-Specification Closed Unassigned
          3.
          JavaScript: Boolean literals should not be used in comparisons RSPEC-2124 Language-Specification Active Unassigned
          4.
          PL/SQL RSPEC-2394 Language-Specification Active Unassigned
          5.
          VB6 RSPEC-3278 Language-Specification Active Unassigned
          6.
          C# RSPEC-3612 Language-Specification Active Unassigned
          7.
          TypeScript RSPEC-4471 Language-Specification Active Unassigned
          8.
          Go RSPEC-4526 Language-Specification Active Unassigned
          9.
          Swift RSPEC-4608 Language-Specification Active Unassigned
          10.
          Kotlin RSPEC-4687 Language-Specification Active Unassigned
          11.
          Scala RSPEC-4936 Language-Specification Active Unassigned
          12.
          Apex RSPEC-5014 Language-Specification Active Unassigned
          13.
          VB.NET RSPEC-5212 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                fabrice.bellingard Fabrice Bellingard
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated: