View Issue Details

IDProjectCategoryView StatusLast Update
0038076pas2jstranspilerpublic2021-03-02 22:09
ReporterSven Barth Assigned ToMattias Gaertner  
PrioritynormalSeverityminorReproducibilityalways
Status feedbackResolutionopen 
Product Versiontrunk 
Summary0038076: Hint about local variable being assigned, but not used for record field
DescriptionIf I have a record type and don't explicitly read from a field somewhere , but do write to it, then the transpiler will bring the hint "Hint: Local variable "xxx" is assigned but never used".
Steps To ReproduceCompile attached example.
TagsNo tags attached.
Fixed in Revision
Attached Files

Activities

Sven Barth

2020-11-13 15:59

developer  

trectype.pp (203 bytes)   
program trectype;

type
  TTest = record
    f1: Integer;
    f2: Integer;
  end;

function Test: TTest;
begin
  Result.f1 := 42;
  Result.f2 := 21;
end;

begin
  Writeln(Test.f1);
end.
trectype.pp (203 bytes)   

Mattias Gaertner

2020-11-28 11:51

manager   ~0127233

In this particular example the hint is correct. The f2 is never read.

Mattias Gaertner

2020-11-28 11:52

manager   ~0127234

OTOH when passing the record to an untyped or jsvalue value, then the analyzer should mark all fields as used. Maybe you mean this case?

Sven Barth

2020-11-28 16:00

developer   ~0127239

For what it's worth FPC or Delphi do not complain if a field of a record isn't used, only if the whole variable isn't used in any way. After all I might use a record from some unit where I'm only interested in some fields, but the routine to populate the record fills all anyway.

For example imagine this:

=== code begin ===

uses
  Types, SomeUnit;

var
  s: TSize;
begin
  s := GetScreenSize; // from SomeUnit
  if s.cx < 1024 then
    Writeln('You poor soul')
  else
    Writeln('Your screen is wide enough');
end.

=== code end ===

Should there really be a hint about TSize.cy being assigned, but unused when I'm not interested in it at all?

Mattias Gaertner

2020-11-28 16:34

manager   ~0127241

The argument of "I'm only interested..all anyway" is applicable to all such hints, not only record fields.
About Delphi/fpc compatibility: pas2js differ here from Delphi/FPC, as records are not continuous blocks of memory, and pas2js can omit fields.

Maybe there can be a separate msg number for record fields, so users can disable this particular type of half used field.

Mattias Gaertner

2020-12-29 03:40

manager   ~0127886

There now separate hints:
4501 field "x" not used
4502 field "x" assigned but never used

Sven Barth

2021-03-02 22:09

developer   ~0129332

Sorry for the late response.

> OTOH when passing the record to an untyped or jsvalue value, then the analyzer should mark all fields as used. Maybe you mean this case?

Yes, that is also the case as the following example shows:

program pas2js;

{$mode delphi}

procedure Foo(aArg: JSValue);
begin

end;

type
  TTest = record
    Field1: String;
  end;

var
  t: TTest;

procedure Test;
begin
  t.Field1 := 'Hello World';
  Foo(t);
end;

begin
  Test;
end.


This is especially annoying if one uses e.g. TJSJSON.stringify() (which works rather well with records).

Issue History

Date Modified Username Field Change
2020-11-13 15:59 Sven Barth New Issue
2020-11-13 15:59 Sven Barth File Added: trectype.pp
2020-11-28 11:47 Mattias Gaertner Assigned To => Mattias Gaertner
2020-11-28 11:47 Mattias Gaertner Status new => assigned
2020-11-28 11:51 Mattias Gaertner Note Added: 0127233
2020-11-28 11:52 Mattias Gaertner Status assigned => feedback
2020-11-28 11:52 Mattias Gaertner Note Added: 0127234
2020-11-28 16:00 Sven Barth Note Added: 0127239
2020-11-28 16:00 Sven Barth Status feedback => assigned
2020-11-28 16:34 Mattias Gaertner Note Added: 0127241
2020-12-29 03:40 Mattias Gaertner Status assigned => resolved
2020-12-29 03:40 Mattias Gaertner Resolution open => fixed
2020-12-29 03:40 Mattias Gaertner Note Added: 0127886
2021-03-02 22:09 Sven Barth Status resolved => feedback
2021-03-02 22:09 Sven Barth Resolution fixed => open
2021-03-02 22:09 Sven Barth Note Added: 0129332