RFR: 8289397: Fix warnings: Possible accidental assignment in place of a comparison. A condition expression should not be reduced to an assignment
Andy Goryachev
angorya at openjdk.org
Tue Jul 26 22:35:12 UTC 2022
On Tue, 26 Jul 2022 21:14:58 GMT, Andy Goryachev <angorya at openjdk.org> wrote:
> - replaced with exact functional equivalent (in the presence of exceptions, for example)
I think the warning serves a good purpose of preventing a bug, it also offloads thinking to the compiler.
At the same time, the byte code emitted by two variants is identical, i.e.:
source
`public int accidental(int x)
{
boolean rv;
if(rv = (x == 0)) {
return 1;
}
return 2;
}
public int explicit(int x)
{
boolean rv;
if((rv = (x == 0)) == true) {
return 1;
}
return 2;
}`
byte code:
`public int accidental(int);
Code:
0: iload_1
1: ifne 8
4: iconst_1
5: goto 9
8: iconst_0
9: dup
10: istore_2
11: ifeq 16
14: iconst_1
15: ireturn
16: iconst_2
17: ireturn`
public int explicit(int);
Code:
0: iload_1
1: ifne 8
4: iconst_1
5: goto 9
8: iconst_0
9: dup
10: istore_2
11: ifeq 16
14: iconst_1
15: ireturn
16: iconst_2
17: ireturn`
I don't know why github can't create a code block normally... sorry.
-------------
PR: https://git.openjdk.org/jfx/pull/851
More information about the openjfx-dev
mailing list