Recent

Author Topic: Why sometimes warning and sometimes hint ?  (Read 974 times)

440bx

  • Hero Member
  • *****
  • Posts: 943
Why sometimes warning and sometimes hint ?
« on: March 07, 2019, 09:39:37 pm »
Hello,

The compiler sometimes emits a hint for some condition and sometimes a warning for what seems to be the exact same condition. For instance,
Code: Pascal  [Select]
  1. Compile Project, Mode: Win32, Target: lib\i386-win32\: Success, Warnings: 1, Hints: 4
  2. (73,51) Hint: Parameter "lParam" not used
  3. (265,25) Warning: Local variable "ps" does not seem to be initialized
  4. (285,39) Hint: Local variable "ClientRect" does not seem to be initialized
  5. (313,53) Hint: Local variable "TextSize" does not seem to be initialized
  6. (479,24) Hint: Local variable "Msg" does not seem to be initialized
  7.  
for "ps" it emits a warning (used in BeginPaint) whereas it emits only a hint for "ClientRect" (used in GetClientRect). 

I'd like to know if there is a reason for the difference ?... is one case really different than the other that, one deserves a warning while the other only a hint ?

Thank you for your help.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 1300
Re: Why sometimes warning and sometimes hint ?
« Reply #1 on: March 07, 2019, 10:25:47 pm »
The compiler sometimes emits a hint for some condition and sometimes a warning for what seems to be the exact same condition. For instance,
...
I'd like to know if there is a reason for the difference ?... is one case really different than the other that, one deserves a warning while the other only a hint ?
When passing an uninitialized variable by reference - hint, because it can be initialized within the procedure. When passing an uninitialized variable by value - warning.

440bx

  • Hero Member
  • *****
  • Posts: 943
Re: Why sometimes warning and sometimes hint ?
« Reply #2 on: March 07, 2019, 10:52:10 pm »
When passing an uninitialized variable by reference - hint, because it can be initialized within the procedure. When passing an uninitialized variable by value - warning.
Hi Serge, that sounds reasonable but, in the case of BeginPaint and GetClientRect both variables, "ps" and "ClientRect", are uninitialized and passed by reference or did I miss something in what you explained ?

The only difference I can see is that ClientRect is a structure/record while ps is essentially an ordinal type but, while in some programs, I get a hint for one and a warning for the other, in other programs that seem to declare and use the variables in exactly the same way, I get a hint for both (compiler modes and directives exactly the same.)   I can't make sense out of it.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 1300
Re: Why sometimes warning and sometimes hint ?
« Reply #3 on: March 07, 2019, 10:59:07 pm »
Hi Serge, that sounds reasonable but, in the case of BeginPaint and GetClientRect both variables, "ps" and "ClientRect", are uninitialized and passed by reference or did I miss something in what you explained ?
Example:
Code: Pascal  [Select]
  1. procedure TestByRef(var N: Integer);
  2. begin
  3.   N := N;
  4. end;
  5.  
  6. procedure TestByVal(N: Integer);
  7. begin
  8.   N := N;
  9. end;
  10.  
  11. var
  12.   N: Integer;
  13. begin
  14.   TestByVal(N); // Comment out - only a hint for the last call appears
  15.   TestByRef(N);
  16. end.

440bx

  • Hero Member
  • *****
  • Posts: 943
Re: Why sometimes warning and sometimes hint ?
« Reply #4 on: March 07, 2019, 11:24:29 pm »
Example:
That example you posted confirms that what you first posted is correct. 

I'll see if I can make an example that uses two variables passed by reference and one causes a warning while the other causes a hint.   I already have a few programs where that happens but, they are much too large to post them as examples (among other reasons why I cannot simply attach them.)

Thank you for providing an example of when (and why) that difference takes place.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 943
Re: Why sometimes warning and sometimes hint ?
« Reply #5 on: March 07, 2019, 11:45:55 pm »
I figured it out.  I've gotten used to not giving much attention to warnings and hints about variables passed as API parameters but, the compiler's warning was right (not to mention very useful) in this case.

I copied and pasted a SelectObject(ps.hdc, GetStockObject(afont)) from a WM_PAINT handler to another handler where the dc variable was not ps.hdc but simply dc (a dc I was getting with GetDc.)  The compiler's warning was more than justified since ps.hdc had definitely not been initialized there.

Serge, thank you for your help.  My attempt to provide an example of the discrepancy lead me to find that problem.  Strangely, in spite of using a junk dc in the SelectObject call, the program worked correctly (it used the right font.. go figure.)
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.