aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorAndrey Konovalov <andreyknvl@google.com>2019-09-03 18:54:35 +0200
committerDmitry Vyukov <dvyukov@google.com>2019-09-04 07:10:15 +0200
commita50398545a325e46db14f4f3cf985938693e1456 (patch)
tree9145b981accc787bc7d592e72c0512a31a51697a /pkg
parent526709ff0442e985edd498e14c736de8302dc867 (diff)
pkg/compiler: detect unused template params
Diffstat (limited to 'pkg')
-rw-r--r--pkg/compiler/check.go10
-rw-r--r--pkg/compiler/testdata/errors.txt4
2 files changed, 12 insertions, 2 deletions
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go
index 24acfe50f..a18ad2227 100644
--- a/pkg/compiler/check.go
+++ b/pkg/compiler/check.go
@@ -861,6 +861,7 @@ func (comp *compiler) checkTypeArgs(t *ast.Type, desc *typeDesc, flags checkFlag
return args
}
+// TODO: add warning when template arg is not used
func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlags) {
typedefName := t.Ident
comp.usedTypedefs[typedefName] = true
@@ -940,6 +941,7 @@ func (comp *compiler) instantiate(templ ast.Node, params []*ast.Ident, args []*a
for i, param := range params {
argMap[param.Name] = args[i]
}
+ argUsed := make(map[string]bool)
err0 := comp.errors
templ.Walk(ast.Recursive(func(n ast.Node) {
templArg, ok := n.(*ast.Type)
@@ -947,6 +949,7 @@ func (comp *compiler) instantiate(templ ast.Node, params []*ast.Ident, args []*a
return
}
if concreteArg := argMap[templArg.Ident]; concreteArg != nil {
+ argUsed[templArg.Ident] = true
origArgs := templArg.Args
if len(origArgs) != 0 && len(concreteArg.Args) != 0 {
comp.error(templArg.Pos, "both template parameter %v and its usage"+
@@ -965,11 +968,18 @@ func (comp *compiler) instantiate(templ ast.Node, params []*ast.Ident, args []*a
if len(templArg.Colon) != 0 {
col := templArg.Colon[0]
if concreteArg := argMap[col.Ident]; concreteArg != nil {
+ argUsed[col.Ident] = true
col.Ident = concreteArg.Ident
col.Pos = concreteArg.Pos
}
}
}))
+ for _, param := range params {
+ if !argUsed[param.Name] {
+ comp.error(argMap[param.Name].Pos,
+ "template argument %v is not used", param.Name)
+ }
+ }
return err0 == comp.errors
}
diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt
index 8c44694bb..bd7182252 100644
--- a/pkg/compiler/testdata/errors.txt
+++ b/pkg/compiler/testdata/errors.txt
@@ -298,7 +298,7 @@ type templ_struct1[STR] {
}
type templ_struct2[A] {
- f B ### unknown type B
+ f B
}
type templ_base0[TYPE] {
@@ -323,7 +323,7 @@ foo$202(a templ0) ### template templ0 needs 2 arguments instead of 0
foo$203(a type0[42]) ### type type0 is not a template
foo$204(a ptr[in, templ_struct0[42, int8]])
foo$205(a ptr[in, templ_struct0[int8, int8]])
-foo$207(a ptr[in, templ_struct2[1]])
+foo$207(a ptr[in, templ_struct2[1]]) ### template argument A is not used
# fmt