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: 6 rules found
java14
    Impact
      Clean code attribute
        1. Comma-separated labels should be used in Switch with colon case

           Code Smell
        2. Switch arrow labels should not use redundant keywords

           Code Smell
        3. Escape sequences should not be used in text blocks

           Code Smell
        4. Whitespace for text block indent should be consistent

           Code Smell
        5. Simple string literal should be used for single line strings

           Code Smell
        6. Use Java 14 "switch" expression

           Code Smell

        Use Java 14 "switch" expression

        intentionality - clear
        maintainability
        Code Smell
        • java14

        Why is this an issue?

        Many existing switch statements are essentially simulations of switch expressions, where each arm either assigns to a common target variable or returns a value. Expressing this as a statement is roundabout, repetitive, and error-prone.

        Java 14 added support for switch expressions, which provide more succinct and less error-prone version of switch.

        Noncompliant code example

        void day_of_week(DoW day) {
            int numLetters;
            switch (day) {  // Noncompliant
              case MONDAY:
              case FRIDAY:
              case SUNDAY:
                numLetters = 6;
                break;
              case TUESDAY:
                numLetters = 7;
                break;
              case THURSDAY:
              case SATURDAY:
                numLetters = 8;
                break;
              case WEDNESDAY:
                numLetters = 9;
                break;
              default:
                throw new IllegalStateException("Wat: " + day);
            }
        }
        
        int return_switch(int x) {
            switch (x) { // Noncompliant
              case 1:
                return 1;
              case 2:
                return 2;
              default:
                throw new IllegalStateException();
            }
        }
        

        Compliant solution

        int numLetters = switch (day) {
            case MONDAY, FRIDAY, SUNDAY -> 6;
            case TUESDAY                -> 7;
            case THURSDAY, SATURDAY     -> 8;
            case WEDNESDAY              -> 9;
        };
        
          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
          9.1
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use