llvm / llvm/llvm-project

Add a warning for lossy integer division of constants

Open
#164,484 7 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I was kinda surprised that `return 1 / 2;` produces [no warnings](https://godbolt.org/z/WbobMYoME), even with `-Weverything`. That feels like a natural way to write one half, for someone who isn't thinking of integers and truncating division. And since it seems like you would never _intentionally_ write that[0], it should have a warning. I know there are already some warnings where arithmetic with constants doesn't evaluate to the numeric result (eg due to overflow) and this seems to fit right in there. It could even become a single warning about any time that numeric results aren't preserved (excluding bitwise operators and common patterns like `-1ull` to get a bunch of 1 bits).

[0] Ignoring obfuscated code contests...

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.