RFR: 8289397: Fix warnings: Possible accidental assignment in place of a comparison. A condition expression should not be reduced to an assignment

Michael Strauß mstrauss at openjdk.org
Tue Jul 26 22:27:26 UTC 2022


On Tue, 26 Jul 2022 22:03:29 GMT, Phil Race <prr at openjdk.org> wrote:

>> - replaced with exact functional equivalent (in the presence of exceptions, for example)
>
> This warning seems to be saying "the language has this feature that the designers thought useful, but I don't like it so
> I'm going to complain". 
> 
> I don't see the difference between
> if (valid = res.validate(fctx)) {
> and
> if ((valid = res.validate(fctx)) == true) {
> 
> in both cases we are using the value returned by the assignment and if magically using it in ( X == true) instead of if (X)
> makes the warning go away that is both artificial and clumsy.
> 
> how does now it know you didn't mean
> if ((valid == res.validate(fctx)) == true) {
> 
> So I'd not make these changes.  If someone (else) does then agree with them at least fix "if(" -> "if ("

I agree with @prrace that the proposed changes don't add any value, since the conditional expression still contains an assignment. If this is to be done at all, I suggest to simply move the assignment out of the conditional, and test the assigned value:

    // old:
    if(isSame = fname.equals(container.fname)) {

   // new:
   isSame = fname.equals(container.fname);
   if (isSame) {

-------------

PR: https://git.openjdk.org/jfx/pull/851


More information about the openjfx-dev mailing list