Recent

Author Topic: [solved] Going 'yes, I know' to warnings about unintialised variables  (Read 790 times)

RememberTP

  • New Member
  • *
  • Posts: 20
I've looked in the Programmer's Guide and the Language Reference Guide without success - is there a way to tell the compiler that you know a particular variable isn't initialised, so please don't issue a warning about it?

Hopefully this sample shows what I'm talking about: I'd like not to be warned about the way a is not initialised every compile...

Code: Pascal  [Select][+][-]
  1. procedure setItUp (var x: integer);
  2. begin
  3.   x := 1;
  4. end;
  5.  
  6. procedure useIt;
  7. var
  8.   a: integer;
  9. begin
  10.   setItUp (a);
  11.   writeLn (a);
  12. end;

« Last Edit: June 13, 2024, 08:01:16 pm by RememberTP »

dsiders

  • Hero Member
  • *****
  • Posts: 1153
Re: Going 'yes, I know' to warnings about unintialised variables
« Reply #1 on: June 10, 2024, 12:06:31 am »
I've looked in the Programmer's Guide and the Language Reference Guide without success - is there a way to tell the compiler that you know a particular variable isn't initialised, so please don't issue a warning about it?

Hopefully this sample shows what I'm talking about: I'd like not to be warned about the way a is not initialised every compile...

Code: Pascal  [Select][+][-]
  1. procedure setItUp (var x: integer);
  2. begin
  3.   x := 1;
  4. end;
  5.  
  6. procedure useIt;
  7. var
  8.   a: integer;
  9. begin
  10.   setItUp (a);
  11.   writeLn (a);
  12. end;

Turn warning 5036 off (per unit):

https://wiki.freepascal.org/$warn
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

cdbc

  • Hero Member
  • *****
  • Posts: 1250
    • http://www.cdbc.dk
Re: Going 'yes, I know' to warnings about unintialised variables
« Reply #2 on: June 10, 2024, 12:17:59 am »
Hi
Just as @dsiders says, turnit off...
In the gutter, where the blue dots, hints & warnings show an icon, you right-click on the icon and choose at the top of the popup-menu e.g.: 5036
You usually get 3 choices: insert a {%H-} in the code, insert a {$warn 5036 off} in the unit or hide via project option.
Per unit is the middle one, as per the attached screenshot...
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 6312
Re: Going 'yes, I know' to warnings about unintialised variables
« Reply #3 on: June 10, 2024, 12:31:19 am »
use an OUT instead of a VAR

The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 4224
Re: Going 'yes, I know' to warnings about unintialised variables
« Reply #4 on: June 10, 2024, 02:13:42 am »
use an OUT instead of a VAR
and for the OP's problem, that is the correct solution.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14850
  • Censorship about opinions does not belong here.
Re: Going 'yes, I know' to warnings about unintialised variables
« Reply #5 on: June 10, 2024, 10:36:34 am »
In this case, yes, that fixes it.
More in general, though, you can turn of warnings on block level, not unit level.
That prevents you from turning off a specific warning for a whole unit, That is also in the wiki. After all, it can be a situation where the warning would be correct and you overlooked it.
If you know the variable is initialized, use it only in the block that triggers the warning.
Remember the Medway disaster..

RememberTP

  • New Member
  • *
  • Posts: 20
Re: Going 'yes, I know' to warnings about unintialised variables
« Reply #6 on: June 10, 2024, 11:23:46 am »
use an OUT instead of a VAR

Ah ha, fabulous - thank you.

I stopped using Delphi around version 3 and I can't remember noticing that it had introduced this.

 

TinyPortal © 2005-2018