-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add reassign linter #3064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hey, thank you for opening your first Pull Request ! |
In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements. Pull Request Description
Linter
The Linter Tests Inside Golangci-lint
|
Hi @ldez - it's been more than a week so just checking in, what sort of schedule can we expect on this sort of code review? Happy to iterate on the code as reviews come in. Thanks! |
I have some doubts about this linter because by default it only detected |
@ldez The default is to detect |
Does your linter only detect reassignment of global variables? For you, in which cases the reassignment is a problem? and why do you think there is a problem? |
Yes the job of the linter is to detect reassignment of global variables. The background for the linter is here https://github.com/curioswitch/go-reassign#background Some people prefer constant errors pattern to |
so the description of your linter must be explicit on that. I think you have to improve the default detection and maybe you have to provide a slice instead of one string for the pattern. Also, I'm not sure about regexp, why did you use it instead of simple string? |
Do you mean PR description? It says "public variables in other packages" which I think is fairly explicit
Could you describe what sort of improvement you're thinking of? Should the pattern accept any variable to detect them all? I can change from string to slice
It's to match against unknown variable names, often defined by a user. When matching against something arbitrary like that, I think regex flexibility can be needed |
"public variables" or "top-level variables" are not really explicit.
I already said that the current default is a bit weak.
Regexps are CPU intensives, so you have to a clear need to use them. The weak default and the use of regex, give me the impression that you don't really know the concrete use cases of your linter, that's why I ask you questions. |
Did you read the background I linked? I'm not sure how much more to add about the concrete use case which is to safely provide exposed It can also be used outside the case of errors to detect more broadly, in which case the regex would likely be set more permissive but that may increase chance of false positives. |
Because golangci has its own suppression mechanisms, perhaps the pattern isn't as useful when invoked via golangci (it'd still be needed for CLI invocation in the binary itself). Would it be better to instead default to checking all reassignments without the pattern option, assuming that false positives could be fixed or suppressed with golangci? |
It's not a good idea: generating false positives is not a good thing at all. As your linter is not specific to error as global variables, I expected something different as default but I understand that you are focused on error, so we will go with the current default. Can you add a slice for the patterns? |
Thanks @ldez! I've changed the config to a slice |
There is something that is unexpected for me. tree
config/config.gopackage config
import "net/http"
var DefaultClient = &http.Client{} main.gopackage main
import (
"io"
"net/http"
"github.com/golangci/sandbox/config"
)
var DefaultClient = &http.Client{}
func reassignPattern() {
io.EOF = nil
config.DefaultClient = nil // <- fail
DefaultClient = nil // <- fail
http.DefaultClient = nil
http.DefaultTransport = nil
} .golangci.ymllinters:
disable-all: true
enable:
- reassign
- typecheck
linters-settings:
reassign:
patterns:
- "DefaultClient"
- "DefaultTransport"
|
I think that the reassignment should be handled even inside the same package. examplespackage a
import (
"errors"
)
var MyError = errors.New("My error)
func reassignPattern() {
MyError = nil
} package a
import (
"net/http"
)
var DefaultClient = &http.Client{}
func reassignPattern() {
DefaultClient = nil
} It can be seen as an improvement. Also, I think the fact that the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@ldez Thanks for all the help on this! 🙏
Yeah this is something that came to mind. I didn't implement it yet mostly because within-package, the author has control of the code so can worry less, having their variables modified outside the package from unknown codebase can cause undefined behavior though. But I think it could be an improvement, possibly opt-in |
reassign is a linter to check when global variables in other packages are reassigned, for example
io.EOF = nil
https://github.com/curioswitch/go-reassign