Defaults for case statements and selectors should not be needed when checking an Enum
#8
Replies: 2 comments 1 reply
-
|
I run into this a few times as well (and puppet-lint complaining about the missing default). I agree with you and think that there doesn't need to be a default case. |
Beta Was this translation helpful? Give feedback.
-
|
For your specific example, I think you can also write Today puppet-lint enforces the style, but to properly enforce your new suggestion I think puppet-lint needs to track the data type and all branches of the case statement to see if there's complete coverage. If not, it should fail telling the user there is an undefined branch. Or put into concrete terms (coming up with test cases here), let's say we have: class example (
Enum['foo','bar'] $param,
) {
case $param {
'foo': { include example::foo }
'bar': { include example::bar }
}
}This is a good case: all branches are covered and should pass because there is no undefined behavior. Now imagine it's changed: class example (
Enum['foo','bar', 'boo'] $param,
) {
case $param {
'foo': { include example::foo }
'bar': { include example::bar }
}
}This should fail: it either needs to define Until now it's all simple because everything is in the same file, but let's imagine we have type aliases: class example (
Example::CustomType $param,
) {
case $param {
'foo': { include example::foo }
'bar': { include example::bar }
}
}Today the linter is mostly a simple lexer and you work on tokens while building the logic on top of it. To properly enforce this, the linter needs to become a real parser. And I certainly have seen cases where working on an AST is much better. So while I think in theory you have a good point, in practice there are some challenges in enforcing the style via puppet-lint. On the other hand: we already have a real parser: Puppet itself. Today we have |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
From https://puppet.com/docs/puppet/7/style_guide.html#style_guide_conditionals-case-selector-defaults
IMO, if the control expression is a class/type parameter with type
Enumthen a default case should not be included if cases for all the valid values have been.Beta Was this translation helpful? Give feedback.
All reactions