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
Java

Java static code analysis

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

  • All rules 733
  • Vulnerability60
  • Bug175
  • Security Hotspot40
  • Code Smell458

  • Quick Fix 65
Filtered: 11 rules found
java21
    Impact
      Clean code attribute
        1. Use when instead of a single if inside a pattern match body

           Code Smell
        2. "String.indexOf" should be used with correct ranges

           Bug
        3. "Math.clamp" should be used with correct ranges

           Bug
        4. Virtual threads should not run tasks that include synchronized code

           Bug
        5. "setDaemon", "setPriority" and "getThreadGroup" should not be invoked on virtual threads

           Bug
        6. Use built-in "Math.clamp" methods

           Code Smell
        7. Virtual threads should be used for tasks that include heavy blocking operations

           Bug
        8. Use switch instead of if-else chain to compare a variable against multiple cases

           Code Smell
        9. Use record pattern instead of explicit field access

           Code Smell
        10. Reverse view should be used instead of reverse copy in read-only cases

           Code Smell
        11. Reverse iteration should utilize reversed view

           Code Smell

        Use when instead of a single if inside a pattern match body

        consistency - conventional
        maintainability
        Code Smell
        Quick FixIDE quick fixes available with SonarQube for IDE
        • java21

        Why is this an issue?

        How can I fix it?

        More Info

        Java 21 has introduced enhancements to switch statements and expressions, allowing them to operate on any type, not just specific ones, as in previous versions. Furthermore, case labels have been upgraded to support patterns, providing an alternative to the previous restriction of only accepting constants.

        // As of Java 21
        String patternMatchSwitch(Object obj) {
            return switch (obj) {
                case String s  -> String.format("String %s", s);
                case Integer i -> String.format("int %d", i);
                default        -> obj.toString();
            };
        }
        

        This allows to use the when keyword to specify a condition for a case label, also called a guarded case label.

        String guardedCaseSwitch(Object obj) {
            return switch (obj) {
                case String s when s.length() > 0 -> String.format("String %s", s);
                case Integer i when i > 0 -> String.format("int %d", i);
                default        -> obj.toString();
            };
        }
        

        This syntax is more readable and less error-prone than using an if statement inside the case block and should be preferred.

        This rule reports an issue when a single if statement is used inside a case block.

          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 Community BuildAnalyze code in your
          on-premise CI
          Available Since
          10.5
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          10.5

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use