Skip to content

[SUGGESTION] if (i==j) gives clang warning about (i==j) #457

Closed

Description

@realgdman

if (i==j) in cpp2, when lowered to cpp1, encloses condition in parens if ((i==j))
Which is, probably, for good, but in modern C++, for example clang, gives a warning

https://cpp2.godbolt.org/z/av3ech559

warning: equality comparison with extraneous parentheses [-Wparentheses-equality]
note: remove extraneous parentheses around the comparison to silence this warning
note: use '=' to turn this equality comparison into an assignment

It can be a problem if "threat warnings as error" enabled, in case of mixed cpp1/cpp2.

Activity

  1. JohelEGP commented on May 14, 2023

    @JohelEGP
  2. realgdman commented on May 14, 2023

    @realgdman
    Author

    Hm, looks this cpp2 valid currently
    https://cpp2.godbolt.org/z/bGPqKK3nc

    b:= false;
    c:= true;
    if(b) 		{ foo(true); } else { foo(false); }
    if(b=c) 	{ foo(true); } else { foo(false); }
    if((b=c)) 	{ foo(true); } else { foo(false); }
    
    i:= 0;
    j:= 1;
    if(i) 		{ foo(true); } else { foo(false); }
    if(i=j) 	{ foo(true); } else { foo(false); }
    if((i=j)) 	{ foo(true); } else { foo(false); }
    

    prints 0,1,1,0,1,1

  3. JohelEGP commented on May 14, 2023

    @JohelEGP
    Contributor

    You're right.
    It's only if a = b that's rejected.
    if (a = b) isn't.
    Because the grammar is 'if' logical-or-expression.

    So only if (a == b) don't need the redundant parentheses lowered to Cpp1 to avoid warnings.
    Or maybe it's good that they're lowered to Cpp1 as-is,
    because it means, "hey, you used redundant parentheses in Cpp2!"

  4. JohelEGP commented on May 14, 2023

    @JohelEGP
    Contributor

    Or maybe it's good that they're lowered to Cpp1 as-is,
    because it means, "hey, you used redundant parentheses in Cpp2!"

    So this is a non-bug (much less a suggestion).

  5. realgdman commented on May 14, 2023

    @realgdman
    Author

    I should be clearer, cppfront add extra parens. So if ((i==1)) becames if (((i==1))) etc

  6. JohelEGP commented on May 14, 2023

    @JohelEGP
    Contributor

    It's not extra. It's the same you write.
    if a is Cpp2, and has to lower to Cpp1 if (a).

  7. Repository owner locked and limited conversation to collaborators on Aug 30, 2023
  8. converted this issue into a discussion #632 on Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions