Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi compile-time integer conversion warnings?

In Delphi XE or 2006, is there any way to detect at compile time that implicit conversions between integer types may lose data? I realize it's possible to detect this with runtime checking. I would want it to flag the following example even if the "big" value were 1. (We're considering changing int to bigint for certain database keys and want to determine the impact on a large legacy codebase.)

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  small: Integer;
  big: Int64;
begin
  big := 3000000000000;
  small := big;  // Detect me!

  Writeln(small);
end.
like image 739
TrueWill Avatar asked Jul 28 '11 20:07

TrueWill


2 Answers

You won't get any warnings or hints at compile time. The Delphi compiler does not do any program flow analysis which tells it that big contains a too large value when it is assigned to small. It silently truncates the value to make it fit in the smaller type. I tried with Shortint, a signed byte-sized type and even that did not give a warning or hint.

And there is no way to make Delphi warn you. It would be nice. Perhaps you can suggest it in QC (if it wasn't already suggested)?

like image 176
Rudy Velthuis Avatar answered Oct 21 '22 13:10

Rudy Velthuis


In Delphi, even in Delphi 10.3 -- no. But take a look into software calling 'Pascal Analyzer', mfg by www.peganza.com

They have a lot of options and one of them is (taken from software Help):

enter image description here

Source code for test, take a look into line #32:

enter image description here

Analyze result show possible bad assignment in line #32:

enter image description here

like image 33
Zam Avatar answered Oct 21 '22 15:10

Zam