Subject: Re: [PATCH v3 31/35] userdiff golang: don't over-match intented constructs

From: Johannes Sixt <[email-masked]>
Date: Fri, 26 Feb 2021 08:59:00 +0100

Am 24.02.21 um 20:51 schrieb Ævar Arnfjörð Bjarmason:
> Fix a bug introduced when the "golang" driver was added in
> 1dbf0c0ad6c (userdiff: add built-in pattern for golang, 2018-03-01).
> 
> Unlike the default def_ff() driver in xemit.c it would match "type"
> declarations inside functions. Let's make it mandatory that a "func"
> or "type" must be at the beginning of the line with no whitespace to
> get around this.
> 
> Go is such a regularly formatted language that I think this can be
> counted on. I think the whitespace matching was probably copy/pasted
> from an earlier userdiff.c pattern.
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>
> ---
>  t/t4018/golang | 20 ++++++++++++++++++++
>  userdiff.c     |  4 ++--
>  2 files changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/t/t4018/golang b/t/t4018/golang
> index 72a35d66008..252b6049da4 100644
> --- a/t/t4018/golang
> +++ b/t/t4018/golang
> @@ -51,3 +51,23 @@ type some struct { // comment
>  	a Type
>  	b ChangeMe
>  }
> +
> +t4018 description: func combined with type
> +t4018 header: func myfunc() {
> +func myfunc() {
> +	type mystruct struct {
> +		a Foo
> +		b Bar
> +	}
> +	ChangeMe
> +
> +t4018 description: anonymous indented func()
> +t4018 header: func SomeThing() bool {
> +func SomeThing() bool {
> +	func() {

Wait! With the unmodified pattern, this indented "func()" cannot match
because it is not followed by a space. So, this test case does not
demonstrate that the modified pattern makes a difference. In the commit
message of 30/35 you say that "we should always get whitespace after
'func'", but here you show a case where that is not true. Did you forget
to write a name after "func"?

> +		defer func() {
> +			fmt.Println("hello")
> +		}()
> +	}()
> +
> +	ChangeMe
> diff --git a/userdiff.c b/userdiff.c
> index 698eca5ad35..704af241e44 100644
> --- a/userdiff.c
> +++ b/userdiff.c
> @@ -129,9 +129,9 @@ IPATTERN("fountain",
>  	 "[^ \t-]+"),
>  PATTERNS("golang",
>  	 /* Functions */
> -	 "^[ \t]*(func[ \t].*)\n"
> +	 "^(func[ \t].*)\n"
>  	 /* Structs and interfaces */
> -	 "^[ \t]*(type[ \t].*(struct|interface)[ \t].*)",
> +	 "^(type[ \t].*[ \t](struct|interface)[ \t].*)",
>  	 /* -- */
>  	 "[a-zA-Z_][a-zA-Z0-9_]*"
>  	 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
>