Uninitialized Return Value

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Uninitialized Return Value

Post by mikep »

Shouldn't the following code at least issue a warning message about an uninitialized return value:

Code: Select all

Private Function GetValue(ByVal c1 as Integer) as Integer	
	If c1 <> 0 Then
		GetValue = c1
	End If
End Function
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Uninitialized Return Value

Post by dkinzer »

mikep wrote:Shouldn't the following code at least issue a warning message about an uninitialized return value
Yes, it should and it does here unless the compiler option --warn=no-undefined-var is specified. Check your .pjt file and the IDE user options file to see if that specific warning (or warnings in general) is being suppressed.

Alternately, you can try this code which turns on the undefined variable warning for the code within.

Code: Select all

#pragma warning&#40;push;undefined-var&#58;on&#41;
Private Function GetValue&#40;ByVal c1 as Integer&#41; as Integer
   If c1 <> 0 Then
      GetValue = c1
   End If
End Function
#pragma warning&#40;pop&#41;
The resulting message should be something like this:

Code: Select all

foo.bas&#58;12&#58; Warning&#40;2&#41;&#58; "GetValue", not all paths set the return value
Perhaps the undefined return value warning should be distinguished from the more general undefined variable warning.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Uninitialized Return Value

Post by mikep »

dkinzer wrote:
mikep wrote:Shouldn't the following code at least issue a warning message about an uninitialized return value
Yes, it should and it does here unless the compiler option --warn=no-undefined-var is specified.
Or the routine is not referenced by other code. I suppose that this type of warning is issued by the code generation backend. No generation for dead code means no warning.

Problem solved for me but it might be worth investigating if warnings can be issued for this type of problem. A warning list of dead routines might be even better.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Uninitialized Return Value

Post by dkinzer »

mikep wrote:Or the routine is not referenced by other code.
The warning is currently generated during the code generation phase and it uses information generated during the optimization phase. Since the optimization phase is only performed for procedures that appear in the call graph (i.e. those that are invoked), moving the warning to the optimization phase wouldn't help.
mikep wrote:A warning list of dead routines might be even better.
A warning option for unused procedures could be added. It would be best, I believe, if it were not on by default as is the case for the warning for unused parameters.
- Don Kinzer
Post Reply