integrated-application-development / integrated-application-development/sonar-delphi

`VariableInitialization` rule should be stricter around records

Open
#127 7 comments 0 reactions 0 assignees View on GitHub
enhancement rule
Dominant language
Java
Stars
159
Forks
31
Avg merge
5d 4h
Merged PRs (30d)
4

Description

### Prerequisites

- [X] This bug is in SonarDelphi, not SonarQube or my Delphi code.
- [X] This bug has not already been reported.

### SonarDelphi version

1.0.0

### SonarQube version

_No response_

### Issue description

This rule is particuliarly important, because the Delphi compiler itself misses so many cases on uninitialized variables, and this causes nasty real-world bugs. I hoped you could do better. (Indeed, if you offer this rule, it is because you are aware of the compiler's failures, so it *is meant* to do better!)

But no, here is a very basic program that passes the scan.

### Steps to reproduce

Run the scanner on the provided mini project. The uninitialized variable in Main is not detected.

It should be done either using `default`: `LThing := Default(TThing)`, or using a constructor (if one was defined).

I report it as a bug because this case is really a basic one.

### Minimal Delphi code exhibiting the issue

```
program BugReport;

{$APPTYPE CONSOLE}

type
TThing = record
FThing: Integer;
procedure SetThing(AVal: Integer);
function GetThing: Integer;
end;

function TThing.GetThing: Integer;
begin
Result := FThing;
end;

procedure TThing.SetThing(AVal: Integer);
begin
FThing := AVal;
end;

procedure Main;
var
LThing: TThing;
begin
WriteLn(LThing.GetThing); // used but not initialized, random output
LThing.SetThing(0);
WriteLn(LThing.GetThing);
end;

begin
Main;
ReadLn;
end.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.