From 1353b6007479ea9095f51493ede16fb846713da0 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov <ar@gortc.io> Date: Mon, 27 Apr 2020 19:19:12 +0300 Subject: [PATCH 1/3] test: add unused testdata for fix --- test/testdata/fix/in/unused.go | 8 ++++++++ test/testdata/fix/out/unused.go | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 test/testdata/fix/in/unused.go create mode 100644 test/testdata/fix/out/unused.go diff --git a/test/testdata/fix/in/unused.go b/test/testdata/fix/in/unused.go new file mode 100644 index 00000000..fa8979c2 --- /dev/null +++ b/test/testdata/fix/in/unused.go @@ -0,0 +1,8 @@ +//args: -Eunused +package p + +type ( + unused struct{} +) + +func X() {} diff --git a/test/testdata/fix/out/unused.go b/test/testdata/fix/out/unused.go new file mode 100644 index 00000000..fa8979c2 --- /dev/null +++ b/test/testdata/fix/out/unused.go @@ -0,0 +1,8 @@ +//args: -Eunused +package p + +type ( + unused struct{} +) + +func X() {} From 3deb9d80ab7b29a5ebf69f52a6f214f10fcb7ddd Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov <ar@gortc.io> Date: Mon, 27 Apr 2020 19:56:45 +0300 Subject: [PATCH 2/3] unused: check line range before suggesting fix Fix #1048 --- pkg/golinters/unused.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/golinters/unused.go b/pkg/golinters/unused.go index 5f6d8371..ac5a90d4 100644 --- a/pkg/golinters/unused.go +++ b/pkg/golinters/unused.go @@ -33,7 +33,7 @@ func NewUnused() *goanalysis.Linter { for _, ur := range u.Result() { p := u.ProblemObject(lintCtx.Packages[0].Fset, ur) pkg := typesToPkg[ur.Pkg()] - issues = append(issues, goanalysis.NewIssue(&result.Issue{ //nolint:scopelint + i := &result.Issue{ FromLinter: name, Text: p.Message, Pos: p.Pos, @@ -42,11 +42,16 @@ func NewUnused() *goanalysis.Linter { From: p.Pos.Line, To: p.End.Line, }, - Replacement: &result.Replacement{ + } + // See https://github.com/golangci/golangci-lint/issues/1048 + // If range is invalid, this will break `--fix` mode. + if i.LineRange.To >= i.LineRange.From { + i.Replacement = &result.Replacement{ // Suggest deleting unused stuff. NeedOnlyDelete: true, - }, - }, nil)) + } + } + issues = append(issues, goanalysis.NewIssue(i, nil)) } return issues }).WithContextSetter(func(lintCtx *linter.Context) { From 56f2b7d59d6149a52559c23845f37f6f4739dd51 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov <ar@gortc.io> Date: Mon, 27 Apr 2020 20:07:09 +0300 Subject: [PATCH 3/3] fixer: add warning about possible line range issue --- pkg/result/processors/fixer.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index 3f7abbc8..29313d7f 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -223,6 +223,12 @@ func (f Fixer) writeFixedFile(origFileLines [][]byte, issues []result.Issue, tmp } else { nextIssueIndex++ rng := nextIssue.GetLineRange() + if rng.From > rng.To { + // Maybe better decision is to skip such issues, re-evaluate if regressed. + f.log.Warnf("[fixer]: issue line range is probably invalid, fix can be incorrect (from=%d, to=%d, linter=%s)", + rng.From, rng.To, nextIssue.FromLinter, + ) + } i += rng.To - rng.From if nextIssue.Replacement.NeedOnlyDelete { continue