sonar-delphi icon indicating copy to clipboard operation
sonar-delphi copied to clipboard

New rule: `var` arguments should be initialized before the call

Open Chadehoc opened this issue 2 years ago • 1 comments

Prerequisites

  • [X] This rule has not already been suggested.
  • [X] This should be a new rule, not an improvement to an existing rule.
  • [X] This rule would be generally useful, not specific to my code or setup.

Suggested rule title

VarArgumentsShouldBeInitialized

Rule description

In #127, there was a discussion on whether var argument should be considered as initializing a variable. Although out arguments should be preferred for that, a confusion in the Delphi compiler (out and var are processed similarly) has lead some teams to accept initialization through var. So, this was suggested as a new rule rather than an improvement or the rule that "variables should be initialized before being used".

This rule states that a variable is "used" when passed as a var argument to a function call, so doing so without the variable being initialized should be warned against.

Rationale

If you do not accept in your code base that initialization could be done via a var argument, then the rationale is the same as stating that a variable should be initialized before being used.

It is especially important for record variables, for which the Delphi compiler does not enforce this basic rule!

Chadehoc avatar Dec 11 '23 08:12 Chadehoc

Thanks, this seems like a good companion rule to VariableInitialization.

For the rule title, I'm thinking: VarArgumentInitialization

cirras avatar Dec 12 '23 01:12 cirras