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: 3 rules found
performance
    Impact
      Clean code attribute
        1. Context parameters should be reused instead of creating new background contexts

           Code Smell
        2. Use "bytes.Equal" instead of "bytes.Compare" for equality checks

           Code Smell
        3. Context cancellation functions should be deferred

           Code Smell

        Context parameters should be reused instead of creating new background contexts

        intentionality - logical
        reliability
        maintainability
        Code Smell
        • performance
        • concurrency
        • best-practice

        This rule raises an issue when context.Background() is called in a scope where a context parameter or variable is already available.

        Why is this an issue?

        How can I fix it?

        More Info

        In Go, contexts are designed to carry cancellation signals, deadlines, and request-scoped values across API boundaries. When you call context.Background(), you create a new, empty context that has no parent and will never be cancelled.

        If your function already receives a context parameter, that context likely carries important information from upstream callers. For example, it might have a timeout set by an HTTP server, or a cancellation signal from a user who closed their browser.

        When you ignore the existing context and create a new background context instead, you break this chain of communication. Your operation will continue running even if the original request was cancelled, potentially wasting resources and making your application less responsive.

        This pattern is particularly problematic in web applications, where requests should be cancelled when clients disconnect, and in concurrent programs where operations need to respect cancellation signals.

        What is the potential impact?

        Breaking context propagation can lead to several issues:

        • Operations continue running after they should have been cancelled, wasting CPU and memory
        • Timeouts and deadlines are not respected, potentially causing the application to hang
        • Request-scoped values (like trace IDs or user information) are lost
        • The application becomes less responsive and harder to shut down gracefully
          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