Skip to content

[Clang] -Wformat size_t issues: suggests %zf for "broken" namespaced size_t, doesn't recognize sizeof(...) #146429

@rprichard

Description

@rprichard

From https://godbolt.org/z/zssKGfhxW (reduced from https://godbolt.org/z/3G6qjb4KT):

#include <stdio.h>

namespace foo4 {
    using size_t = unsigned char;
}

namespace foo5 {
    using size_t = float;
}

void test_func(unsigned long s) {
    // Clang suggests replacing %zu with %zu.
    printf("%zu\n", foo4::size_t {});

    // Clang suggests replacing %zu with %zf, a nonsensical specifier
    printf("%u\n", foo5::size_t {});

    // Clang suggests %lu instead of %zu.
    printf("%u\n", sizeof(int));
}

Maybe FormatSpecifier::namedTypeToLengthModifier ought to ignore a type if it's in a namespace?

Alternatively, it could check whether the typedef refers to the expected integer type.

It might be nice if sizeof(...) was regarded as "having size_t type" for the purposes of -Wformat.

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