Skip to content
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

Using upstream maligned and updating error message #1555

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca
github.com/golangci/misspell v0.0.0-20180809174111-950f5d19e770
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
Expand All @@ -36,6 +35,7 @@ require (
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
github.com/mattn/go-colorable v0.1.8
github.com/mbilski/exhaustivestruct v1.1.0
github.com/mdempsky/maligned v0.0.0-20201101000000-d73c43cb16d0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-ps v1.0.0
github.com/moricho/tparallel v0.2.1
Expand Down
5 changes: 3 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 5 additions & 46 deletions pkg/golinters/maligned.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,17 @@
package golinters

import (
"fmt"
"sync"

malignedAPI "github.com/golangci/maligned"
"github.com/mdempsky/maligned/passes/maligned"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

func NewMaligned() *goanalysis.Linter {
const linterName = "maligned"
var mu sync.Mutex
var res []goanalysis.Issue
analyzer := &analysis.Analyzer{
Name: linterName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
linterName,
"Tool to detect Go structs that would take less memory if their fields were sorted",
[]*analysis.Analyzer{analyzer},
"maligned",
maligned.Analyzer.Doc,
[]*analysis.Analyzer{maligned.Analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
prog := goanalysis.MakeFakeLoaderProgram(pass)

malignedIssues := malignedAPI.Run(prog)
if len(malignedIssues) == 0 {
return nil, nil
}

issues := make([]goanalysis.Issue, 0, len(malignedIssues))
for _, i := range malignedIssues {
text := fmt.Sprintf("struct of size %d bytes could be of size %d bytes", i.OldSize, i.NewSize)
if lintCtx.Settings().Maligned.SuggestNewOrder {
text += fmt.Sprintf(":\n%s", formatCodeBlock(i.NewStructDef, lintCtx.Cfg))
}
issues = append(issues, goanalysis.NewIssue(&result.Issue{
Pos: i.Pos,
Text: text,
FromLinter: linterName,
}, pass))
}

mu.Lock()
res = append(res, issues...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return res
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
8 changes: 0 additions & 8 deletions pkg/golinters/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,3 @@ func formatCode(code string, _ *config.Config) string {

return fmt.Sprintf("`%s`", code)
}

func formatCodeBlock(code string, _ *config.Config) string {
if strings.Contains(code, "`") {
return code // TODO: properly escape or remove
}

return fmt.Sprintf("```\n%s\n```", code)
}
2 changes: 1 addition & 1 deletion test/testdata/maligned.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//args: -Emaligned
package testdata

type BadAlignedStruct struct { // ERROR "struct of size 24 bytes could be of size 16 bytes"
type BadAlignedStruct struct { // ERROR "struct of size 24 could be 16"
B bool
I int
B2 bool
Expand Down