8000 staticcheck feature request: add check when user try to modify global variable on other package · Issue #1152 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

staticcheck feature request: add check when user try to modify global variable on other package #1152

Open

Description

@win-t

user should not be able to modify global variables inside another package because it can result in data race and other undesired behavior

for example, the following code should generate a check error

package main

import (
	"errors"
	"net/http"
)

func main() {
	http.DefaultServeMux = http.NewServeMux() // <-- here
	http.ErrAbortHandler = errors.New("override") // <-- or here
}

in general, a global identifier should be const, but it is not possible in golang because we need to initialize it with a non-zero value

thanks

Activity

  1. ainar-g commented on Dec 28, 2021

    @ainar-g

    I would love to have this check, but it probably will only be included as an aggresive check, not as a standard one. Some people choose to modify “foreign globals” in tests or, in case with net/http, to change the settings of the default clients.

  2. dominikh commented on Jan 7, 2022

    @dominikh
    Owner

    This can at best be an aggressive check, for the reasons Ainar already mentioned. Expanding on that, it's not correct to say that no foreign globals should ever be assigned to, nor that it cannot be done correctly.

    I am, however, not sure how useful this check would be in the aggressive category. It seems unlikely to me that someone would assign to a global by accident.

  3. added
    aggressiveA set of checks that is more prone to false positives but is helpful during code review
    needs-decisionWe have to decide if this check is feasible and desirable
    and removed
    needs-triageNewly filed issue that needs triage
    on Jan 7, 2022
  4. added this to the Staticcheck 2022.2 milestone on Jan 12, 2022
  5. soypat commented on Jul 2, 2023

    @soypat

    Maybe implementing a subset of this for assigning to an error type would be less agressive? And even less agressive would be implementing this only for standard library errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    aggressiveA set of checks that is more prone to false positives but is helpful during code reviewneeds-decisionWe have to decide if this check is feasible and desirablenew-check

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0