Skip to content

Add a warning for lossy integer division of constants #164484

@RedBeard0531

Description

@RedBeard0531

I was kinda surprised that return 1 / 2; produces no warnings, 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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions