Fix @unused for last argument of function (#1518)
Regressed in d2a20dc9d54727597d66fd091cb16711375cac2b
---------
Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
diff --git a/warn/warn_control_flow.go b/warn/warn_control_flow.go
index 2c0a17a..e1ca26a 100644
--- a/warn/warn_control_flow.go
+++ b/warn/warn_control_flow.go
@@ -473,13 +473,16 @@
}
}
// Collect its (normal) parameters as defined in the current scope.
- for _, param := range expr.Params {
+ finalParameterMarkedUnused := expr.ColonPos != nil && edit.ContainsComments(expr.ColonPos, "@unused")
+ for i, param := range expr.Params {
// Function parameters are defined in the current scope.
if ident, _ := build.GetParamIdent(param); ident != nil {
definedSymbols[ident.Name] = ident
- if ident.Name == "name" || strings.HasPrefix(ident.Name, "_") || edit.ContainsComments(param, "@unused") {
+ if ident.Name == "name" || strings.HasPrefix(ident.Name, "_") || edit.ContainsComments(param, "@unused") ||
+ i == len(expr.Params)-1 && finalParameterMarkedUnused {
// Don't warn about function arguments if they start with "_"
- // or explicitly marked with @unused.
+ // or explicitly marked with @unused. An @unused comment at the
+ // end of the function header applies to its final argument.
// Also don't warn about unused "name" arguments, it could be a
// macro where such argument is encouraged (by `unnamed-macro`)
// even if not used.
diff --git a/warn/warn_control_flow_test.go b/warn/warn_control_flow_test.go
index adfe679..a721ced 100644
--- a/warn/warn_control_flow_test.go
+++ b/warn/warn_control_flow_test.go
@@ -655,6 +655,40 @@
scopeEverywhere)
checkFindings(t, "unused-variable", `
+def foo(x): # @unused
+ pass
+
+def bar(
+ x,
+ y): # @unused
+ pass
+
+foo()
+bar()
+`,
+ []string{
+ ":5: Variable \"x\" is unused.",
+ },
+ scopeEverywhere)
+
+ checkFindings(t, "unused-variable", `
+def foo(x) -> int: # @unused
+ pass
+
+def bar(
+ x,
+ y) -> int: # @unused
+ pass
+
+foo()
+bar()
+`,
+ []string{
+ ":5: Variable \"x\" is unused.",
+ },
+ scopeEverywhere)
+
+ checkFindings(t, "unused-variable", `
def foo(
name,
x):