Skip to content

-Wshorten-64-to-32 should not warn when value is bounded by conditionals #105837

@Explorer09

Description

@Explorer09
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    if (argc < 2)
        return 0;

    long long x = strtoll(argv[1], NULL, 10);
    if (x >= 0 && x <= INT_MAX) {
        int y = x;
        printf("%d\n", y);
    }
    return 0;
}

Compiling with clang-19 -Wshorten-64-to-32 produces this warning on the int y = x; line:

warning: implicit conversion loses integer precision: 'long long' to 'int' [-Wshorten-64-to-32]

Expected result: No warning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerenhancementImproving things as opposed to bug fixing, e.g. new or missing featurequality-of-implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions