crystal-ameba / ameba

A static code analysis tool for Crystal
https://crystal-ameba.github.io
MIT License
524 stars 39 forks source link

Add rules for enforcing type restrictions on vars and parameters #502

Open nobodywasishere opened 4 hours ago

nobodywasishere commented 4 hours ago

I'm imagining this would be a few rules, and not sure what namespace they would fall under. Something like:

Typing/InstanceVarTypeRestriction: # and ClassVarTypeRestriction
  Enabled: true
  DefaultValue: false # don't require for instance vars with default values

Typing/LocalVarTypeRestriction:
  Enabled: true

Typing/MethodParamTypeRestriction:
  Enabled: true
  DefaultValue: false
  PrivateMethods: false # don't require on private methods
  ProtectedMethods: false

Typing/MethodReturnTypeRestriction:
  Enabled: true

Typing/ProcReturnTypeRestriction:
  Enabled: true

# Maybe some others, can't think of any others currently

This would be disabled by default. It wouldn't be perfect, but would definitely be good enough for my use-cases. If this feature would be welcome, I can start prototyping an implementation.

nobodywasishere commented 4 hours ago

For getter/setter/record, there could be a rule like:

Typing/MacroCallVarTypeRestriction:
  Enabled: true
  MacroNames:
   - record
   - getter
   - setter
   - property