aboutsummaryrefslogtreecommitdiffstats
path: root/executor/style_test.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2021-10-01 16:01:47 +0200
committerDmitry Vyukov <dvyukov@google.com>2021-10-01 16:38:35 +0200
commitcc80db955d0551c2456692da6176530dd27e08ed (patch)
tree52eb201e3fb5af9168db095fbd5fcebaf2961d9d /executor/style_test.go
parent1d849ab4d892af13a249f75628790b47e42b7c74 (diff)
executor: check for single-line compound statements
Historically the code base does not use single-line compound statements ({} around single-line blocks). But there are few precedents creeped into already. Add a check to keep the code base consistent.
Diffstat (limited to 'executor/style_test.go')
-rw-r--r--executor/style_test.go26
1 files changed, 24 insertions, 2 deletions
diff --git a/executor/style_test.go b/executor/style_test.go
index 9c83f2536..5c393ea3d 100644
--- a/executor/style_test.go
+++ b/executor/style_test.go
@@ -36,6 +36,22 @@ if (foo)
},
},
{
+ pattern: `\) {\n[^\n}]+?\n\t*}\n`,
+ suppression: "debug|__except",
+ message: "Don't use single-line compound statements (remove {})",
+ tests: []string{
+ `
+if (foo) {
+ bar();
+}
+`, `
+ while (x + y) {
+ foo(a, y);
+ }
+`,
+ },
+ },
+ {
// These are also not properly stripped by pkg/csource.
pattern: `/\*[^{]`,
message: "Don't use /* */ block comments. Use // line comments instead",
@@ -146,18 +162,24 @@ if (foo)
re := regexp.MustCompile(check.pattern)
supp := regexp.MustCompile(check.suppression)
for _, match := range re.FindAllIndex(data, -1) {
- // Locate the last line of the match, that's where we assume the error is.
end := match[1] - 1
for end != len(data) && data[end] != '\n' {
end++
}
- start := end - 1
+ // Match suppressions against all lines of the match.
+ start := match[0] - 1
for start != 0 && data[start-1] != '\n' {
start--
}
if check.suppression != "" && supp.Match(data[start:end]) {
continue
}
+ // Locate the last line of the match, that's where we assume the error is.
+ start = end - 1
+ for start != 0 && data[start-1] != '\n' {
+ start--
+ }
+
line := bytes.Count(data[:start], []byte{'\n'}) + 1
t.Errorf("\nexecutor/%v:%v: %v\n%s\n", file, line, check.message, data[start:end])
}