Pelles C > General discussions

warning #2046

(1/2) > >>

John Z:
In Pelles C version 11 a lot of attention was given to 'Expressions with no effect.'
such as in
RetVal = fwrite(temp2, 1, 5, p_ini);
where RetVal is never used.  It is understandable why save it if it is never used.

My question is a similar situation such as
RetVal = WritePrivateProfileStringW(....);  or ans = MessageBoxW(gHWND...);
do not raise the same warning when RetVal, or ans are not used.

Done that way on purpose?

John Z

frankie:
I don't think that the code:

--- Code: ---RetVal = fwrite(temp2, 1, 5, p_ini);
--- End code ---
cause the warning "Expressions with no effect.", even if RetVal is not used.
This because a call to a function can cause side effects that can't be traced by the compiler, so a no-effect condition can't be detected.
For the same reason you don't get any warning for the other code samples you reported.
Probably the warning is triggered elsewhere.
Try to produce a minimal example that shows the problem and post it so we can analyze it together.

John Z:
Thanks Frankie,

Here is an example attached.  It demonstrates my question regarding #2046, at least the first part.

Here is a deconstructed ;D example. 
In creating it I found there may be a bit more to the issue, as you suggested, but it still does what I suggested.
Just compile to see the  #2046 warnings.

There are 4 tiny procedures named Warning_Check1-4
Warning_Check1 and Warning_Check2 show the #2046 warning moving when using or not using the return value, and when used there is no warning generated.

Warning_Check3 shows the error on both statements when not using the return value from either

Warning_Check4 is an interesting complication because here I just change the return variable declaration type and then there are no warnings when the return is not used.

So perhaps the warning should be that the return value may be larger than the store variable declaration, but then why does using it eliminate the error message?

Building main.obj.
C:\Program Files\PellesC\Files\Hello_World\main.c(143): warning #2046: Expression with no effect removed.
C:\Program Files\PellesC\Files\Hello_World\main.c(161): warning #2046: Expression with no effect removed.
C:\Program Files\PellesC\Files\Hello_World\main.c(181): warning #2046: Expression with no effect removed.
C:\Program Files\PellesC\Files\Hello_World\main.c(183): warning #2046: Expression with no effect removed.
Done.

John Z

"You learn more from your mistakes than from your successes."

frankie:
I made a check, and can definitely conclude that the warning is related to type conversion, not to the unused value.
The warning description is really confusing, leading the user to wrong assumptions. Which expression with 'no-effect' is removed? The type conversion!.
The last is then coded only, and if only, the result value is used elsewhere.
To explain also the case reported by John:

--- Quote ---My question is a similar situation such as
RetVal = WritePrivateProfileStringW(....);  or ans = MessageBoxW(gHWND...);
do not raise the same warning when RetVal, or ans are not used.
--- End quote ---
Try this snippet:

--- Code: ---// This will not generate warning because the type of variable is the same as returned
// value, so no conversion is required
void Warning_Check5(HWND hwnd)
{
BOOL RetVal;
int ans;

RetVal = WritePrivateProfileStringW(NULL, NULL, NULL, NULL);
ans = MessageBoxW(hwnd, NULL, NULL, 0);
}

--- End code ---
Then this one:

--- Code: ---// This will generate the warning because type conversion is required
void Warning_Check6(HWND hwnd)
{
unsigned RetVal;
unsigned ans;

RetVal = WritePrivateProfileStringW(NULL, NULL, NULL, NULL);
ans = MessageBoxW(hwnd, NULL, NULL, 0);
}

--- End code ---

John Z:
Thanks frankie - Excellent clarification/explanation  AS USUAL  :)

When I saw the results from trying size_t it seemed to be related but I could not fathom why there was no error
report in the other cases where  the value was subsequently 'used.'  Clear now. thumbs up!
 
Maybe an opportunity for Pelles V12.

John Z

Navigation

[0] Message Index

[#] Next page

Go to full version