Skip to content

Commit

Permalink
unused: check line range before suggesting fix
Browse files Browse the repository at this point in the history
Fix #1048
  • Loading branch information
ernado committed Apr 27, 2020
1 parent 1353b60 commit 3deb9d8
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/golinters/unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 3deb9d8

Please sign in to comment.