SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Apex

Apex static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your APEX code

  • All rules 97
  • Vulnerability2
  • Bug26
  • Security Hotspot3
  • Code Smell66
 
Tags
    Impact
      Clean code attribute
        1. Arguments to "assertEquals" and "assertNotEquals" should be in the correct order

           Code Smell
        2. String conversions should use explicit methods instead of empty string concatenation

           Code Smell
        3. Abstract classes should contain at least one abstract or virtual method

           Code Smell
        4. SOQL queries should not contain hardcoded date literals

           Code Smell
        5. Apex classes should contain at most one "@InvocableMethod" annotation

           Bug
        6. SOQL queries should not be executed inside loops

           Code Smell
        7. Custom exception classes should follow proper naming conventions

           Code Smell
        8. Loops should use braces

           Code Smell
        9. SOSL queries in test methods should use "Test.setFixedSearchResults"

           Code Smell
        10. Constants should use SNAKE_CASE with all uppercase letters

           Code Smell
        11. DML operations in catch blocks can mask original exceptions

           Bug
        12. Builder pattern methods should return the builder instance to enable method chaining

           Code Smell
        13. Future methods must be static and return void

           Bug
        14. Test classes should have only one @testSetup method

           Bug
        15. Test classes and methods should not use "seeAllData=true"

           Code Smell
        16. HTTP requests should have explicit timeout configuration

           Code Smell
        17. Methods with "@AuraEnabled" should be public or global

           Bug
        18. Singleton classes should have private constructors

           Code Smell
        19. Classes implementing "Database.Batchable" should be declared as "global"

           Bug
        20. "getRoleAndSubordinates()" should be replaced with "getRoleAndSubordinatesInternal()"

           Code Smell
        21. Future methods making HTTP callouts should include "callout=true" parameter

           Bug
        22. "String.isEmpty()" should be replaced with "String.isBlank()" for comprehensive validation

           Code Smell
        23. SOQL queries should use SystemModStamp instead of LastModifiedDate for better performance

           Code Smell
        24. Catch blocks should not be empty

           Code Smell
        25. Future methods should not call other future methods

           Bug
        26. Apex classes and methods should have explicit access modifiers

           Code Smell
        27. Test classes should be annotated with "@isTest"

           Code Smell
        28. SOQL queries should include LIMIT clauses to prevent hitting governor limits

           Bug
        29. Private members made public for testing should use "@TestVisible" annotation

           Code Smell
        30. Batch Apex scope parameters should not exceed 2000 records

           Code Smell
        31. Custom event names should not be prefixed with "on"

           Code Smell
        32. Catch specific exception types instead of generic "Exception"

           Code Smell
        33. Apex REST services should implement comprehensive exception handling

           Vulnerability
        34. SOQL queries should be assigned to Lists to avoid QueryException

           Bug
        35. Test methods should include meaningful assertions

           Code Smell
        36. InvocableMethod annotations should include descriptive label and description

           Code Smell
        37. Test methods should not have parameters

           Bug
        38. URLs should not be hardcoded in Apex code

           Bug
        39. "@future" methods should not be called in loops

           Bug
        40. Email messages should be sent in batches

           Bug
        41. Test methods should not be annotated with "@isTest(SeeAllData=true)"

           Code Smell
        42. Tested code should be enclosed between "Test.StartTest()" and "Test.StopTest()"

           Code Smell
        43. Messages should not be hardcoded

           Code Smell
        44. URLs of Salesforce pages should be relative, not absolute.

           Bug
        45. "getRecordTypeInfosByDeveloperName()" should be used instead of "getRecordTypeInfosByName()"

           Bug
        46. System.runAs should be used to test user permissions

           Code Smell
        47. SOQL COUNT should be used instead of the method size()

           Code Smell
        48. Business logic should not be implemented inside Triggers

           Code Smell
        49. DML statements should not be used inside loops

           Bug
        50. Record IDs should not be hardcoded

           Bug
        51. Executing SOQL, SOSL or DML queries without sharing or with inherited sharing is security sensitive

           Security Hotspot
        52. Sharing level should be specified in Apex Classes with SOQL/SOSL Queries or DML Statements

           Vulnerability
        53. Triggers should process records in bulk

           Bug
        54. Multi-line comments should not be empty

           Code Smell
        55. Methods should not have identical implementations

           Code Smell
        56. All branches in a conditional structure should not have exactly the same implementation

           Bug
        57. Cognitive Complexity of functions should not be too high

           Code Smell
        58. Non-existent operators like "=+" should not be used

           Bug
        59. Track parsing failures

           Code Smell
        60. Hard-coded credentials are security-sensitive

           Security Hotspot
        61. Boolean checks should not be inverted

           Code Smell
        62. Two branches in a conditional structure should not have exactly the same implementation

           Code Smell
        63. Related "if"/"else if" statements and "when" in a "switch" should not have the same condition

           Bug
        64. "switch" statements should not be nested

           Code Smell
        65. Identical expressions should not be used on both sides of a binary operator

           Bug
        66. All code should be reachable

           Bug
        67. Variables should not be self-assigned

           Bug
        68. Unused local variables should be removed

           Code Smell
        69. "switch" statements should not have too many "case" clauses

           Code Smell
        70. Track lack of copyright and license headers

           Code Smell
        71. Functions should not have too many lines of code

           Code Smell
        72. Control flow statements "if", "for", "while", "switch" and "try" should not be nested too deeply

           Code Smell
        73. Using hardcoded IP addresses is security-sensitive

           Security Hotspot
        74. "switch" statements should have "when else" clauses

           Code Smell
        75. "if ... else if" constructs should end with "else" clauses

           Code Smell
        76. Sections of code should not be commented out

           Code Smell
        77. Statements should be on separate lines

           Code Smell
        78. String literals should not be duplicated

           Code Smell
        79. Methods should not be empty

           Code Smell
        80. Unused function parameters should be removed

           Code Smell
        81. Local variable and method parameter names should comply with a naming convention

           Code Smell
        82. "when" clauses should not have too many lines of code

           Code Smell
        83. Useless "if(true) {...}" and "if(false){...}" blocks should be removed

           Bug
        84. Unused "private" methods should be removed

           Code Smell
        85. Track uses of "TODO" tags

           Code Smell
        86. Track uses of "FIXME" tags

           Code Smell
        87. Boolean literals should not be redundant

           Code Smell
        88. Redundant pairs of parentheses should be removed

           Code Smell
        89. Nested blocks of code should not be left empty

           Code Smell
        90. Functions should not have too many parameters

           Code Smell
        91. Expressions should not be too complex

           Code Smell
        92. Mergeable "if" statements should be combined

           Code Smell
        93. Tabulation characters should not be used

           Code Smell
        94. Files should not have too many lines of code

           Code Smell
        95. Lines should not be too long

           Code Smell
        96. Class names should comply with a naming convention

           Code Smell
        97. Function names should comply with a naming convention

           Code Smell

        All branches in a conditional structure should not have exactly the same implementation

        intentionality - clear
        reliability
        Bug

          Why is this an issue?

          Having all branches of a switch or if chain with the same implementation indicates a problem.

          In the following code:

          if (b == 0) {  // Noncompliant
            doOneMoreThing();
          } else {
            doOneMoreThing();
          }
          
          switch on i {  // Noncompliant
            when 1 {
              doSomething();
            }
            when 2 {
              doSomething();
            }
            when 3 {
              doSomething();
            }
            when else {
              doSomething();
            }
          }
          

          Either there is a copy-paste error that needs fixing or an unnecessary switch or if chain that should be removed.

          Exceptions

          This rule does not apply to if chains without else, nor to switch without a default clause.

          if(b == 0) {    //no issue, this could have been done on purpose to make the code more readable
            doSomething();
          } else if(b == 1) {
            doSomething();
          }
          
            Available In:
          • SonarQube IdeCatch issues on the fly,
            in your IDE
          • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
          • SonarQube ServerAnalyze code in your
            on-premise CI
            Enterprise
            Edition
            Available Since
            9.1

          © 2008-2025 SonarSource SA. All rights reserved.

          Privacy Policy | Cookie Policy | Terms of Use