Home Value assigned never used. Delphi bug or code bug?
Reply: 2

Value assigned never used. Delphi bug or code bug?

Shahram Banazadeh
1#
Shahram Banazadeh Published in 2018-01-11 10:20:01Z

This code is part of a third party component I am using in my application. I am trying to solve all possible problems related to hint in my application. But I can not understand this. Delphi Compiler marks first line of this code for a hint "Value assigned to NewState never used!. I just want to understand situation

  NewState := STATE_UNCHECKED;   //Value assigned never used?Really?
  while ( Node <> nil ) and ( Node.Parent <> nil ) do
  begin
    Node := Node.Parent.GetFirstChild;
    CheckedCount := 0;
    UnCheckedCount := 0;
    while True do
    begin
     ....
      if ( Node.StateIndex = STATE_PARTCHECKED ) or
         ( ( CheckedCount > 0 ) and ( UnCheckedCount > 0 ) ) then
      begin
        NewState := STATE_PARTCHECKED; ///assign value on condition
        Break;
      end;

      if Node.GetNextSibling = nil then
      begin
        if CheckedCount > 0 then
          NewState := STATE_CHECKED //assign value on condition
        else
          NewState := STATE_UNCHECKED;
        Break;
      end
      else
        Node := Node.GetNextSibling;
    end;
    Node := Node.Parent;
    if Node <> nil then
      SetState( Node,  NewState );
  end;

But when both of this statments are false this value must be used.Am i wrong?

 if ( Node.StateIndex = STATE_PARTCHECKED ) or
             ( ( CheckedCount > 0 ) and ( UnCheckedCount > 0 ) ) then

and

if Node.GetNextSibling = nil then

UPDATE

answers are correct .The Break Statment runs only after a new value assigned to variable and overwriting first value. then assigned value is never used.

a Minimal, Complete, and Verifiable example:

procedure TForm1.btn1Click(Sender: TObject);
Var x,y,z,k:integer;
begin
   X:=1;
   Y:=strtoint(edit1.text);
   Z:=Strtoint(edit2.text);
   K:=Strtoint(edit3.text);
     while true do
     begin
      If Z=y*2 then
      begin
        X:=2;
        Break;
      end;

      If Y=3 then
      begin
        If K=7 then
        begin
        X:=3;
        Break;
        end
        else
        begin
        x:=6;
        Break;
        end;
      end
     end;

    showmessage(inttostr(x));

end;
Dsm
2#
Dsm Reply to 2018-01-11 10:43:56Z

If both the statement you highlight are FALSE then the while True loop continues. The only ways out of this loop require the NewState value to be assigned. So The compiler is correct.

Craig Young
3#
Craig Young Reply to 2018-01-11 12:06:40Z

First, you misunderstand the concept "is used". In this context it means "is the variable read for any purpose before its value is changed again or the variable goes out of scope". You seem to think it means "is the variable name referred to again"; so you're incorrectly thinking when the existing value is being overwritten it "is being used".


You clearly haven't shown all the code. Whether the missing code is relevant? Without seeing it, I can't be sure.

But from the code you do show: the value in assigned in the first line is overwritten without being read in the while True loop. i.e. initial assignment is never used up to that point. However, whether those lines are reached is not clear because of the conditional code leading there.

The only place where NewState is read is in the line SetState(Node, NewState);. As Dsm points out, in the code you do show, the only way to get there is by breaking out of the while True loop immediately after overwriting the initial value.


You don't say which version of Delphi you're using, but I don't think old versions were capable of picking up this slightly complex case. It should be noted that there are certainly complex cases where compilers are incapable of making an accurate determination. But since this warning involves the compiler removing the line from compiled output, these cases are typically only reported when the logic clearly indicates it's not used.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.386719 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO