Skip to content

Commit db6956d

Browse files
committed
Assume single whitespace was intended when multiple leading whitespaces are encountered
1 parent 7c992af commit db6956d

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

pkg/golinters/nolintlint/nolintlint.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,21 @@ func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
181181

182182
// check for, report and eliminate leading spaces so we can check for other issues
183183
if len(leadingSpace) > 0 {
184+
removeWhitespace := &result.Replacement{
185+
Inline: &result.InlineFix{
186+
StartCol: pos.Column + 1,
187+
Length: len(leadingSpace),
188+
NewString: "",
189+
},
190+
}
184191
if (l.needs & NeedsMachineOnly) != 0 {
185192
issue := NotMachine{BaseIssue: base}
186-
issue.BaseIssue.replacement = &result.Replacement{
187-
Inline: &result.InlineFix{
188-
StartCol: pos.Column - 1,
189-
Length: len(leadingSpace) + 2,
190-
NewString: "//",
191-
},
192-
}
193+
issue.BaseIssue.replacement = removeWhitespace
193194
issues = append(issues, issue)
194195
} else if len(leadingSpace) > 1 {
195196
issue := ExtraLeadingSpace{BaseIssue: base}
197+
issue.BaseIssue.replacement = removeWhitespace
198+
issue.BaseIssue.replacement.Inline.NewString = " " // assume a single space was intended
196199
issues = append(issues, issue)
197200
}
198201
}

pkg/golinters/nolintlint/nolintlint_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ func foo() {
102102
"directive `// nolint` should be written without leading space as `//nolint` at testing.go:5:9",
103103
&result.Replacement{
104104
Inline: &result.InlineFix{
105-
StartCol: 8,
106-
Length: 3,
107-
NewString: "//",
105+
StartCol: 10,
106+
Length: 1,
107+
NewString: "",
108108
},
109109
},
110110
},
@@ -124,9 +124,9 @@ func foo() {
124124
"directive `// nolint` should not have more than one leading space at testing.go:5:9",
125125
&result.Replacement{
126126
Inline: &result.InlineFix{
127-
StartCol: 8,
128-
Length: 4,
129-
NewString: "//",
127+
StartCol: 10,
128+
Length: 2,
129+
NewString: " ",
130130
},
131131
},
132132
},

0 commit comments

Comments
 (0)