Skip to content

Commit 6417d89

Browse files
apply revive linter suggestion about defer
1 parent fcc5bbd commit 6417d89

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

arduino/builder/internal/preprocessor/internal/ctags/ctags_has_issues.go

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -206,78 +206,76 @@ func removeComments(text string, multilinecomment bool) (string, bool) {
206206
// FindCLinkageLines scans the source files searching for "extern C" context
207207
// It save the line numbers in a map filename -> {lines...}
208208
func (p *Parser) FindCLinkageLines(tags []*Tag) map[string][]int {
209-
210209
lines := make(map[string][]int)
211210

212211
for _, tag := range tags {
213-
214212
if lines[tag.Filename] != nil {
215213
break
216214
}
217215

218216
file, err := os.Open(tag.Filename)
219-
if err == nil {
220-
defer file.Close()
217+
if err != nil {
218+
continue
219+
}
220+
lines[tag.Filename] = append(lines[tag.Filename], -1)
221221

222-
lines[tag.Filename] = append(lines[tag.Filename], -1)
222+
scanner := bufio.NewScanner(file)
223223

224-
scanner := bufio.NewScanner(file)
224+
// we can't remove the comments otherwise the line number will be wrong
225+
// there are three cases:
226+
// 1 - extern "C" void foo()
227+
// 2 - extern "C" {
228+
// void foo();
229+
// void bar();
230+
// }
231+
// 3 - extern "C"
232+
// {
233+
// void foo();
234+
// void bar();
235+
// }
236+
// case 1 and 2 can be simply recognized with string matching and indent level count
237+
// case 3 needs specia attention: if the line ONLY contains `extern "C"` string, don't bail out on indent level = 0
238+
239+
inScope := false
240+
enteringScope := false
241+
indentLevels := 0
242+
line := 0
225243

226-
// we can't remove the comments otherwise the line number will be wrong
227-
// there are three cases:
228-
// 1 - extern "C" void foo()
229-
// 2 - extern "C" {
230-
// void foo();
231-
// void bar();
232-
// }
233-
// 3 - extern "C"
234-
// {
235-
// void foo();
236-
// void bar();
237-
// }
238-
// case 1 and 2 can be simply recognized with string matching and indent level count
239-
// case 3 needs specia attention: if the line ONLY contains `extern "C"` string, don't bail out on indent level = 0
240-
241-
inScope := false
242-
enteringScope := false
243-
indentLevels := 0
244-
line := 0
245-
246-
externCDecl := removeSpacesAndTabs(keywordExternC)
247-
248-
for scanner.Scan() {
249-
line++
250-
str := removeSpacesAndTabs(scanner.Text())
244+
externCDecl := removeSpacesAndTabs(keywordExternC)
251245

252-
if len(str) == 0 {
253-
continue
254-
}
246+
for scanner.Scan() {
247+
line++
248+
str := removeSpacesAndTabs(scanner.Text())
255249

256-
// check if we are on the first non empty line after externCDecl in case 3
257-
if enteringScope {
258-
enteringScope = false
259-
}
250+
if len(str) == 0 {
251+
continue
252+
}
260253

261-
// check if the line contains externCDecl
262-
if strings.Contains(str, externCDecl) {
263-
inScope = true
264-
if len(str) == len(externCDecl) {
265-
// case 3
266-
enteringScope = true
267-
}
268-
}
269-
if inScope {
270-
lines[tag.Filename] = append(lines[tag.Filename], line)
271-
}
272-
indentLevels += strings.Count(str, "{") - strings.Count(str, "}")
254+
// check if we are on the first non empty line after externCDecl in case 3
255+
if enteringScope {
256+
enteringScope = false
257+
}
273258

274-
// Bail out if indentLevel is zero and we are not in case 3
275-
if indentLevels == 0 && !enteringScope {
276-
inScope = false
259+
// check if the line contains externCDecl
260+
if strings.Contains(str, externCDecl) {
261+
inScope = true
262+
if len(str) == len(externCDecl) {
263+
// case 3
264+
enteringScope = true
277265
}
278266
}
267+
if inScope {
268+
lines[tag.Filename] = append(lines[tag.Filename], line)
269+
}
270+
indentLevels += strings.Count(str, "{") - strings.Count(str, "}")
271+
272+
// Bail out if indentLevel is zero and we are not in case 3
273+
if indentLevels == 0 && !enteringScope {
274+
inScope = false
275+
}
279276
}
280277

278+
file.Close()
281279
}
282280
return lines
283281
}

0 commit comments

Comments
 (0)