Skip to content

Commit

Permalink
ast: Update strict error check for def func
Browse files Browse the repository at this point in the history
Currently compiler strict error check will fail for default
functions that contain a plain variable argument as the
check will report an unused variable. This change attempts to
provide a hint on how to pass that check.

Signed-off-by: Ashutosh Narkar <[email protected]>
  • Loading branch information
ashutosh-narkar committed Jul 28, 2023
1 parent 3423cb1 commit a929115
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ast/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,11 @@ func (c *Compiler) rewriteLocalVars() {
// Report an error for each unused function argument
for arg := range unusedArgs {
if !arg.IsWildcard() {
c.err(NewError(CompileErr, rule.Head.Location, "unused argument %v", arg))
if rule.Default {
c.err(NewError(CompileErr, rule.Head.Location, "unused default function argument %v. (hint: use _ (wildcard variable) instead)", arg))
} else {
c.err(NewError(CompileErr, rule.Head.Location, "unused argument %v", arg))
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5518,6 +5518,18 @@ func TestCheckUnusedFunctionArgVars(t *testing.T) {
},
},
},
{
note: "unused default function argvar",
module: `package test
default func(x) := 0`,
expectedErrors: Errors{
&Error{
Code: CompileErr,
Location: NewLocation([]byte("func(x) := 0"), "", 2, 12),
Message: "unused default function argument x. (hint: use _ (wildcard variable) instead)",
},
},
},
}

t.Helper()
Expand Down

0 comments on commit a929115

Please sign in to comment.