9

I have a function that gives me the following warning:

[DCC Warning] filename.pas(6939): W1035 Return value of function 'function' might be undefined

The function, however, is clean, small, and does have a known, expected, return value. The first statement in the function is:

Result := '';

and there is no local variable or parameter called Result either.

Is there any kind of pragma-like directive I can surround this method with to remove this warning? This is Delphi 2007.

Unfortunately, the help system on this Delphi installation is not working, therefore i can't pop up the help for that warning right now.

Anyone know off the top of their head what i can do?

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • @NGLN In the linked duplicate the code does not definitely set `Result` and the compiler warning in that case might be because the compiler fails to see that `Abort` will terminate the method without allowing it to properly return. In my question, however, the code definitely set `Result` since it was literally the first statement of the method. Are you *sure* this is a good duplicate? – Lasse V. Karlsen Nov 19 '14 at 20:06
  • O shoot, I wielded the [dupe hammer](http://meta.stackexchange.com/questions/231625/please-remind-me-when-i-am-wielding-the-dupe-hammer); that wasn't my intention. I can see the difference now, but I think the title should've been something like _Why is this return value undefined (and how to solve)?_. But if not a duplicate, I'm still unsure this question shouldn't be closed because it sounds like _Too Localized_. I will vote to reopen, since either wasn't marked as dupe when [this answer](http://stackoverflow.com/a/4204619/757830) was posted, and leave it to the community to decide. – NGLN Nov 19 '14 at 21:44

6 Answers6

8

Are you sure you have done everything to solve the warning? Maybe you could post the code for us to look at?

You can turn off the warning locally this way:

{$WARN NO_RETVAL OFF}
function func(...): string;
begin
  ...
end;
{$WARN NO_RETVAL ON}
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • The first statement in the function is giving the function a default return value. The rest of the function is one case statement and some calls to other functions from the case points. – Lasse V. Karlsen Sep 14 '08 at 15:55
4

I am not sure that I want to see the code for this unit... after all, the error occurs at line 6939 ... Maybe some internal compiler table have been exceeded?

Lars Fosdal
  • 1,144
  • 8
  • 14
  • 1
    I agree. I've seen this warning with D2006 on a method containing a large case statement with nested branching. Removing some of the code paths made the warning go away, no matter which branches were removed. So it seems there is a limit to the amount of branching that can happen before the compiler stops looking and assumes there must be at least one path that doesn't set the result :) – WileCau Nov 17 '10 at 13:24
1

There is such a bug in Delphi compiler since, at least, Delphi4: if sum of numbers of function's parameters (including Self and Result) and local variables exceeds 31, it causes problems. For example, it can write W1035 warnings (result might be undefined). It can miss not used variables. Just try this project:

program TestCompilerProblems;

procedure Proc;
var
  a01, a02, a03, a04, a05, a06, a07, a08, a09, a10,
  a11, a12, a13, a14, a15, a16, a17, a18, a19, a20,
  a21, a22, a23, a24, a25, a26, a27, a28, a29, a30,
  a31, a32, a33, a34, a35, a36, a37, a38, a39, a40: Integer;
begin
end;

begin
  Proc;
end.

It would cause 31 hint, not 40.

Abelevich
  • 293
  • 5
  • 9
1

There seems to be some sort of bug in Delphi. Read this post, the last comment links to other bug-reports that may be the one that you have got:

http://qc.codegear.com/wc/qcmain.aspx?d=8144

Espo
  • 41,399
  • 21
  • 132
  • 159
  • I know, there's also a similar bug report about too many local variables and parameters combined causes this, but this function is really simple. Anyway, Delphi is buggy. What else is new :) – Lasse V. Karlsen Sep 14 '08 at 15:58
1

The {$WARN NO_RETVAL OFF} is what you are looking for, but generally I like to find out why stuff like this happens. You might consider formatting it differently and seeing if that helps.

Do you have any flow altering commands like Exit in there? Do you directly raise exceptions, etc? Does your case statement have an else at the end that sets a value on Result?

Might try tweaking those elements and see if that eliminates the warning too.

Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194
1

In order to get a good answer for this, you'll have to post the code. In general, the Delphi compiler will give this warning if there is a possible code path that could result in the Result not being defined. Sometimes that code path is less than obvious.

Nick Hodges
  • 16,902
  • 11
  • 68
  • 130
  • 1
    When the code path follows an initial, unconditional line of code that explicitly initialises "result" (as explained in the question), then no matter how unobvious that code path is the warning is bogus (in this case). – Deltics Nov 11 '09 at 22:01
  • I agree we need to see the code to help. In my experience, Delphi never warns about missing return value when the return type is string. Even if result is not set. – Ville Krumlinde May 25 '10 at 18:12