integrated-application-development / integrated-application-development/sonar-delphi
New rule: `out` arguments should not be assigned before the call
- Dominant language
- Java
- Stars
- 159
- Forks
- 31
- Avg merge
- 5d 4h
- Merged PRs (30d)
- 4
Description
### 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
OutArgumentShouldNotBeCalledInitialized
### Rule description
The discussion at the end of #127 leads me to propose this as a separate rule; but I would prefer it as a part of the rule proposed in #132, because it also has to do with the possible confusion between `var` and `out` arguments (an integrated rule could be called `OutAndVarArgumentConfusion`).
The Delphi compiler does not actually make a difference between `out` and `var`. This is confusing, and can lead to accepting `var` as initializing the variable passed to it (rule #132 proposed against this). Another confusion is passing an initialized variable as `out` argument, which means that the argument should actually be `var`. This rule aims to detect that case.
Should be warned against:
```
var LVal: Integer := Default(TMyRecord); //useless
FillMyRecord({out argument} LVal); // an out argument should come out initialized
```
An exception is when the variable is actually used before the call. Then it is just a reuse of an existing variable.
```
var LVal: Integer := 12;
UseTheValue(LVal);
FillMyRecord({out argument} LVal); // this is a variable being reused, ok
```
### Rationale
Track the confusion between `out` and `var` arguments. While there is no difference for the compiler, there is a huge difference in intent and readability.
Contributor guide
Assessment
This issue has not been assessed yet.