VariableInitialization false positive for custom managed record with Initialize operator
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- compilers
Research direction
The issue is in the VariableInitialization rule. Look at the rule's code in the Java codebase, likely under src/main/java. Find where it checks for variable initialization before use. The fix needs to detect when a local variable is a custom managed record with an Initialize operator, and skip the warning. Start by understanding how the analyzer represents Delphi types and operators. Run existing tests for the VariableInitialization rule to see the current behavior.
Written by the indexing model from the issue text.
Description
Prerequisites
- This improvement has not already been suggested.
- This improvement should not be implemented as a separate rule.
Rule to improve
Variables must be initialized before being used
Improvement description
The VariableInitialization rule reports "Initialize this variable before
using it" when a local custom managed record is passed to a routine, although
the record has an Initialize operator that assigns all its fields.
The warning also persists when the record declares and implements a Finalize
operator. In the observed case, two fields are changed after the local record
is created, and the warning appears when the record is passed as an argument.
Illustrative minimal example (using fictional names):
program ManagedRecordExample;
{$APPTYPE CONSOLE}
type
TRequestOptions = record
Code: Integer;
Name: string;
class operator Initialize(out Dest: TRequestOptions);
class operator Finalize(var Dest: TRequestOptions);
end;
class operator TRequestOptions.Initialize(out Dest: TRequestOptions);
begin
Dest.Code := 0;
Dest.Name := '';
end;
class operator TRequestOptions.Finalize(var Dest: TRequestOptions);
begin
Dest.Name := '';
end;
procedure ProcessRequest(const Options: TRequestOptions);
begin
WriteLn(Options.Code, ' ', Options.Name);
end;
procedure Run;
var
Options: TRequestOptions;
begin
Options.Name := 'Example';
ProcessRequest(Options); // Reported as uninitialized in the observed pattern
end;
begin
Run;
end.
The rule should account for Delphi's automatic invocation of Initialize
when assessing the subsequent use of a local custom managed record.
Rationale
A local custom managed record is initialized automatically by Delphi through
its Initialize operator. An explicit assignment solely to satisfy the rule
is therefore unnecessary. Implementing Finalize does not change the
initialization behavior or remove the warning.
This is a false positive in VariableInitialization, not a parsing problem
or the implicit-Self syntax issue addressed in #416. Ordinary records without
an Initialize operator should retain the rule's existing checks.
Documentation:
https://docwiki.embarcadero.com/RADStudio/Florence/en/Custom_Managed_Records
- Dominant language
- Java
- Stars
- 160
- Forks
- 36
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from integrated-application-development/sonar-delphi
-
`automated` visibility tokenized but not handled crashes the scanPossibly taken @adityaanikam claimed this 25 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
integrated-application-development/sonar-delphi#471 · 1 comment ·
-
bug triage
Difficulty 3/5 1-2 days Newbie friendliness 66/100
-
engine enhancement
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
feature rule
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
feature rule
Difficulty 5/5 Over a week Newbie friendliness 35/100
integrated-application-development/sonar-delphi#423 · 1 comment ·
All issues in integrated-application-development/sonar-delphi
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
code-maintenance
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
krizaka/krizaka-notifications#32 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
klikli-dev/occultism#1719 ·