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
Ruby

Ruby static code analysis

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

  • All rules 75
  • Bug17
  • Security Hotspot2
  • Code Smell56
Filtered: 3 rules found
readability
    Impact
      Clean code attribute
        1. Array and hash literals should be used instead of constructors when no parameters are needed

           Code Smell
        2. "unless" statements should be used appropriately to avoid confusing logic

           Code Smell
        3. Require statements should be placed at the top of files

           Code Smell

        "unless" statements should be used appropriately to avoid confusing logic

        intentionality - clear
        maintainability
        Code Smell
        • confusing
        • readability

        This rule raises an issue when unless is used with an else clause, or when if ! or if not is used for simple negative conditions without an else clause.

        Why is this an issue?

        How can I fix it?

        More Info

        Ruby’s unless statement is designed to make negative conditions more readable, but it can create confusion when misused.

        When unless is used with an else clause, it creates double negative logic that is hard to understand. The else clause of an unless statement represents the positive case, which is counterintuitive. For example, unless user.admin? …​ else …​ means "if the user is not an admin, do this, otherwise (if they are an admin) do that." This mental gymnastics makes code harder to read and maintain.

        On the other hand, when checking simple negative conditions without an else clause, using if ! or if not instead of unless makes the code less idiomatic and slightly harder to read. Ruby’s unless keyword was specifically created to handle these cases more elegantly.

        The key principle is: use unless for simple negative conditions, but switch to if ! when you need an else clause to maintain clarity.

        What is the potential impact?

        This issue affects code readability and maintainability. Confusing conditional logic can lead to bugs when developers misunderstand the intended behavior, especially during code reviews or when maintaining unfamiliar code. While not a security or performance issue, it can slow down development and increase the likelihood of logical errors.

          Available In:
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use