From cc80db955d0551c2456692da6176530dd27e08ed Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 1 Oct 2021 16:01:47 +0200 Subject: 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. --- executor/style_test.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'executor/style_test.go') 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 @@ -32,6 +32,22 @@ if (foo) `, ` if (x + y) debug_dump_data(data, len); +`, + }, + }, + { + 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); + } `, }, }, @@ -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]) } -- cgit mrf-deployment