Skip to content

Commit e9e8f38

Browse files
committed
Allow a single issue to fulfill multiple ignore ranges
1 parent 908f431 commit e9e8f38

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pkg/result/processors/nolint.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type ignoredRange struct {
2121
linters []string
2222
matchedIssueFromLinter map[string]bool
2323
result.Range
24-
col int
24+
col int
25+
originalRange *ignoredRange // pre-expanded range (used to match nolintlint issues)
2526
}
2627

2728
func (i *ignoredRange) doesMatch(issue *result.Issue) bool {
@@ -163,7 +164,11 @@ func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) {
163164

164165
for _, ir := range fd.ignoredRanges {
165166
if ir.doesMatch(i) {
167+
nolintDebugf("found ignored range for issue %v: %v", i, ir)
166168
ir.matchedIssueFromLinter[i.FromLinter] = true
169+
if ir.originalRange != nil {
170+
ir.originalRange.matchedIssueFromLinter[i.FromLinter] = true
171+
}
167172
return false, nil
168173
}
169174
}
@@ -199,9 +204,14 @@ func (e *rangeExpander) Visit(node ast.Node) ast.Visitor {
199204
}
200205

201206
expandedRange := *foundRange
207+
// store the original unexpanded range for matching nolintlint issues
208+
if expandedRange.originalRange == nil {
209+
expandedRange.originalRange = foundRange
210+
}
202211
if expandedRange.To < nodeEndLine {
203212
expandedRange.To = nodeEndLine
204213
}
214+
205215
nolintDebugf("found range is %v for node %#v [%d;%d], expanded range is %v",
206216
*foundRange, node, nodeStartLine, nodeEndLine, expandedRange)
207217
e.expandedRanges = append(e.expandedRanges, expandedRange)

test/testdata/nolintlint.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//args: -Enolintlint
1+
//args: -Enolintlint -Emisspell
22
//config: linters-settings.nolintlint.require-explanation=true
33
//config: linters-settings.nolintlint.require-specific=true
44
//config: linters-settings.nolintlint.allow-leading-space=false
@@ -10,4 +10,11 @@ func Foo() {
1010
fmt.Println("not specific") //nolint // ERROR "directive `.*` should mention specific linter such as `//nolint:my-linter`"
1111
fmt.Println("not machine readable") // nolint // ERROR "directive `.*` should be written as `//nolint`"
1212
fmt.Println("extra spaces") // nolint:deadcode // because // ERROR "directive `.*` should not have more than one leading space"
13+
14+
// test expanded range
15+
//nolint:misspell // deliberate misspelling to trigger nolintlint
16+
func() {
17+
mispell := true
18+
fmt.Println(mispell)
19+
}()
1320
}

0 commit comments

Comments
 (0)