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
Go

Go static code analysis

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

  • All rules 94
  • Vulnerability21
  • Bug13
  • Security Hotspot14
  • Code Smell46
Filtered: 1 rule found
readability
    Impact
      Clean code attribute
        1. Variables in if short statements should be used beyond just the condition

           Code Smell

        Variables in if short statements should be used beyond just the condition

        intentionality - clear
        maintainability
        Code Smell
        • readability
        • go-idiom

        This rule raises an issue when a variable is declared in an if short statement but is only used in the condition itself, suggesting the declaration might be unnecessary.

        Why is this an issue?

        How can I fix it?

        More Info

        Go’s if short statement allows you to declare and initialize a variable before the condition check. This feature is designed to limit the variable’s scope to the if-else block, which is useful when you need the variable in multiple parts of the conditional logic.

        However, if the declared variable is only used in the condition and nowhere else in the if or else blocks, the short statement declaration adds unnecessary complexity. In such cases, you can often simplify the code by moving the expression directly into the condition or restructuring the logic.

        Using if short statements appropriately helps create cleaner, more readable code by:

        • Limiting variable scope to where it’s actually needed
        • Reducing the number of variables in the broader function scope
        • Making the relationship between variable declaration and usage more explicit

        When a variable is declared but underutilized, it can confuse readers about the intended purpose and scope of the variable.

        What is the potential impact?

        This issue primarily affects code readability and maintainability. While it doesn’t cause runtime problems, it can make code harder to understand and maintain. Unnecessary variable declarations can also slightly impact performance in tight loops, though this is typically negligible.

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

        © 2025 SonarSource Sàrl. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use