@@ -478,6 +478,42 @@ const (
478478 })
479479}
480480
481+ func TestNoLinterWarningsForUnusedConstants (t * testing.T ) {
482+ tmpDir := t .TempDir ()
483+ err := os .WriteFile (filepath .Join (tmpDir , "linter_test.go" ), []byte (`
484+ package test
485+ type linterTest uint8
486+ const (
487+ linterTestUnknown linterTest = iota
488+ linterTestValue1
489+ linterTestValue2
490+ )
491+ ` ), 0o644 )
492+ require .NoError (t , err )
493+
494+ gen , err := New ("linterTest" , tmpDir )
495+ require .NoError (t , err )
496+
497+ err = gen .Parse (tmpDir )
498+ require .NoError (t , err )
499+
500+ err = gen .Generate ()
501+ require .NoError (t , err )
502+
503+ // read the generated file to check for the linter warning prevention code
504+ content , err := os .ReadFile (filepath .Join (tmpDir , "linter_test_enum.go" ))
505+ require .NoError (t , err )
506+
507+ // check that the unused constants prevention code exists
508+ assert .Contains (t , string (content ), "// These variables are used to prevent the compiler from reporting unused errors" )
509+ assert .Contains (t , string (content ), "var _ = func() bool {" )
510+ assert .Contains (t , string (content ), "var _ linterTest = 0" )
511+ assert .Contains (t , string (content ), "var _ linterTest = linterTestUnknown" )
512+ assert .Contains (t , string (content ), "var _ linterTest = linterTestValue1" )
513+ assert .Contains (t , string (content ), "var _ linterTest = linterTestValue2" )
514+ assert .Contains (t , string (content ), "return true" )
515+ }
516+
481517func TestGeneratorEdgeCases (t * testing.T ) {
482518 t .Run ("invalid template" , func (t * testing.T ) {
483519 // create a generator with a broken template that will fail to execute
0 commit comments