-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add gofactory
linter
#4196
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
base: master
Are you sure you want to change the base?
Add gofactory
linter
#4196
Changes from all commits
bbe0eb9
70cd3e5
5a86fb8
2a31b53
22e29b7
faecc6b
2fde9ce
bb4065b
419bf43
e40eddf
930e2b2
c1195cf
ba1ed19
2b59b9d
eb43dd5
df588bc
a1dbfe1
985e045
111f638
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/maranqz/gofactory" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewGoFactory(settings *config.GoFactorySettings) *goanalysis.Linter { | ||
analyzer := gofactory.NewAnalyzer() | ||
|
||
cfg := make(map[string]map[string]any) | ||
if settings != nil { | ||
cfg[analyzer.Name] = map[string]any{} | ||
|
||
if len(settings.PackageGlobs) > 0 { | ||
cfg[analyzer.Name]["packageGlobs"] = settings.PackageGlobs | ||
cfg[analyzer.Name]["packageGlobsOnly"] = settings.PackageGlobsOnly | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
analyzer.Name, | ||
analyzer.Doc, | ||
[]*analysis.Analyzer{analyzer}, | ||
cfg, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,7 @@ func TestCgoOk(t *testing.T) { | |
"--timeout=3m", | ||
"--enable-all", | ||
"-D", | ||
"nosnakecase,gci", | ||
"nosnakecase,gci,gofactory", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope that is correct solution to pass tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. your tests are not in the right places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean I should move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, your tests must be inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to rollback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added because of linter error Should I add option to exclude checking of std libraries in gofactory? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nice idea, but I think in case of Zero value is useful 🙂 Look at
|
||
). | ||
WithTargetPath(testdataDir, "cgo"). | ||
Runner(). | ||
|
@@ -355,7 +355,6 @@ func TestLineDirectiveProcessedFiles(t *testing.T) { | |
func TestUnsafeOk(t *testing.T) { | ||
testshared.NewRunnerBuilder(t). | ||
WithNoConfig(). | ||
WithArgs("--enable-all"). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how does this change relate to PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I return, can I add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be reverted. |
||
WithTargetPath(testdataDir, "unsafe"). | ||
Runner(). | ||
Install(). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
linters-settings: | ||
gofactory: | ||
packageGlobs: | ||
- net/url/** | ||
packageGlobsOnly: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//golangcitest:args -Egofactory | ||
package testdata | ||
|
||
import ( | ||
"net/http" | ||
alias_blocked "net/http" | ||
) | ||
|
||
type Struct struct{} | ||
|
||
var ( | ||
defaultGlobalRequest = http.Request{} // want `Use factory for http.Request` | ||
defaultGlobalRequestPtr = &http.Request{} // want `Use factory for http.Request` | ||
) | ||
|
||
func Default() { | ||
_ = http.Request{} // want `Use factory for http.Request` | ||
_ = &http.Request{} // want `Use factory for http.Request` | ||
|
||
_ = []http.Request{{}, http.Request{}} // want `Use factory for http.Request` | ||
_ = []*http.Request{{}, &http.Request{}} // want `Use factory for http.Request` | ||
|
||
call(http.Request{}) // want `Use factory for http.Request` | ||
|
||
_ = []Struct{{}, {}} | ||
} | ||
|
||
func call(_ http.Request) {} | ||
|
||
func alias() { | ||
_ = alias_blocked.Request{} // want `Use factory for http.Request` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cosmetic question: why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idk too) I checked wrapcheck, go-reassign. They both used different printing way for package names. wrapcheck: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, the option that is better for grep and common understanding – to keep the source code and warnings for it consistent: _ = neturl.URL{} // want `Use factory for neturl.URL`
_ = &url.URL{} // want `Use factory for url.URL` |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//golangcitest:args -Egofactory | ||
//golangcitest:config_path configs/go_factory_package_globs_only.yml | ||
package testdata | ||
|
||
import ( | ||
"net/http" | ||
neturl "net/url" | ||
) | ||
|
||
var ( | ||
nestedGlobalRequest = http.Request{} | ||
nestedGlobalRequestPtr = &http.Request{} | ||
|
||
blockedGlobalURL = neturl.URL{} // want `Use factory for url.URL` | ||
blockedGlobalURLPtr = &neturl.URL{} // want `Use factory for url.URL` | ||
) | ||
|
||
func Blocked() { | ||
_ = http.Request{} | ||
_ = &http.Request{} | ||
|
||
_ = neturl.URL{} // want `Use factory for url.URL` | ||
_ = &neturl.URL{} // want `Use factory for url.URL` | ||
} | ||
|
||
type URL struct { | ||
Scheme string | ||
Opaque string | ||
User *neturl.Userinfo | ||
Host string | ||
Path string | ||
RawPath string | ||
OmitHost bool | ||
ForceQuery bool | ||
RawQuery string | ||
Fragment string | ||
RawFragment string | ||
} | ||
|
||
func Casting() { | ||
_ = neturl.URL(URL{}) // want `Use factory for url.URL` | ||
|
||
uPtr, _ := neturl.Parse("") | ||
u := *uPtr | ||
_ = URL(u) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.