Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected -Wconstant-logical-operand warning when compiled with c2x/gnu2x #80548

Closed
fel1x-developer opened this issue Feb 3, 2024 · 3 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer duplicate Resolved as duplicate

Comments

@fel1x-developer
Copy link

#include <stdio.h>

int main() {
	if ((64 > 32) && (32 < 64))
		printf("%lu\n", sizeof(int));

	return 0;
}
$ clang -std=c2x main.c
main.c:4:36: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]
    4 |         if ((sizeof(int) > sizeof(short)) && (sizeof(int) < sizeof(long)))
      |                                           ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:4:36: note: use '&' for a bitwise operation
    4 |         if ((sizeof(int) > sizeof(short)) && (sizeof(int) < sizeof(long)))
      |                                           ^~
      |                                           &
main.c:4:36: note: remove constant to silence this warning
    4 |         if ((sizeof(int) > sizeof(short)) && (sizeof(int) < sizeof(long)))
      |                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

Build with -std=c17 or -std=gnu17 and below prints no error, but this emits warnings when built with c2x or gnu2x. Is this expected behaviour that reflects some changes in C23 or a bug?

@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Feb 3, 2024
@shafik
Copy link
Collaborator

shafik commented Feb 3, 2024

This looks related to: #64356

CC @AaronBallman

@EugeneZelenko
Copy link
Contributor

@fel1x-developer: Could you please elaborate?

@fel1x-developer
Copy link
Author

@fel1x-developer: Could you please elaborate?

This is duplicate of #64356, though it still remains in clang 17.0.6 (Darwin amd64/aarch64 on macOS Sonoma, installed through Homebrew) Is this issue resolved in Clang 18?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

3 participants