feat(lint): fixed-width-types keeps widths that a foreign API chose

The rewrite itself stays token-shaped — it edits type SPELLINGS, which an AST
discards — but what it must not touch now comes from the AST.

The old exemption was per LINE: `int main`, `argc`, `argv`, `extern "`. Being a
transform, a missed exemption here does not over-report, it emits code that no
longer matches the API being called, so this is the rule where guessing from
substrings mattered most. And being per-line, it also disabled the rule for
anything sharing a line with one of those words.

Now a declaration with C language linkage, or one whose initialiser binds to an
entity declared outside the project, contributes a protected byte range and
keeps its spelling. That answers the case directly: a function declared in
somebody else's header taking `unsigned int` keeps `unsigned int`, and a local
initialised from strtoul keeps `unsigned long`, because of where those are
declared rather than because of what the line says.

main is protected from the start of its declaration to the opening brace of its
body, not for its whole extent. Its signature is fixed by the language; its body
is ordinary code. Three findings on this repository came out of that
distinction, all correct:

    for (int i = 1; i < argc; ++i)   ->   for (std::int32_t i = 1; ...)

skipped before only because `argc` appeared on the line, plus Crafter::Run's own
`int argc` and return type, which are ours rather than the language's.

All three AST rules now share one interop test instead of carrying a denylist
each, and it is the same test: whose header dictates this spelling.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-07-30 23:59:46 +02:00
commit 6438cb9ebb
4 changed files with 68 additions and 10 deletions

View file

@ -487,11 +487,40 @@ inline void AddProjectLintRules(Crafter::Configuration& cfg) {
// short/int/long → fixed-width types. Lines mentioning main/argc/argv or
// extern "C" keep their C-conventional ints.
cfg.AddLintRule("fixed-width-types", [](LintContext& ctx) {
cfg.AddAstLintRule("fixed-width-types", [](LintContext& ctx) {
if (!IsCppFile(ctx)) return;
const std::string& code = ctx.CommentStripped();
struct Rep { std::size_t pos; std::size_t len; std::string to; };
std::vector<Rep> reps;
// Byte ranges whose integer spelling is not ours to change. A wrong
// rewrite here does not merely over-report — it produces code that no
// longer matches the API it is calling — so this is derived from the
// AST rather than from substrings on the line.
//
// Replaces the old per-LINE textual exemption (`int main`, `argc`,
// `argv`, `extern "`), which both missed cases and disabled the rule
// for everything else sharing a line with one of those words.
std::vector<std::pair<std::size_t, std::size_t>> protectedRanges;
for (const Crafter::LintDecl& decl : ctx.Decls()) {
if (decl.isExternC || decl.isForeignApi) {
protectedRanges.emplace_back(decl.begin, decl.end);
continue;
}
// main's signature is fixed by the language. Only the signature:
// its body is ordinary code, and the declaration's extent covers
// the whole function.
if (decl.kind == Crafter::LintDeclKind::Function && decl.name == "main") {
std::size_t bodyBrace = ctx.content.find('{', decl.begin);
protectedRanges.emplace_back(decl.begin, bodyBrace == std::string::npos ? decl.end : bodyBrace);
}
}
auto isProtected = [&protectedRanges](std::size_t offset) {
return std::any_of(protectedRanges.begin(), protectedRanges.end(),
[offset](const std::pair<std::size_t, std::size_t>& range) {
return offset >= range.first && offset < range.second;
});
};
// Builtin integer type specifiers combine in any order (`unsigned
// long`, `long unsigned int`, ...), so match whole RUNS of these
// keywords and classify the run, rather than the words one by one.
@ -503,13 +532,8 @@ inline void AddProjectLintRules(Crafter::Configuration& cfg) {
std::size_t lineEnd = code.find('\n', lineStart);
if (lineEnd == std::string::npos) lineEnd = code.size();
std::string_view line(code.data() + lineStart, lineEnd - lineStart);
// `int main` / argc / argv keep their C-conventional type; so do
// extern "C" prototypes (the literal body is blanked in the
// stripped text, so match `extern "`).
bool exempt = line.contains("int main") || line.contains("argc") || line.contains("argv")
|| line.contains("extern \"");
std::size_t pos = 0;
while (!exempt && pos < line.size()) {
while (pos < line.size()) {
if (!IsWordChar(line[pos])) { ++pos; continue; }
std::size_t wordEnd = pos;
while (wordEnd < line.size() && IsWordChar(line[wordEnd])) ++wordEnd;
@ -546,6 +570,8 @@ inline void AddProjectLintRules(Crafter::Configuration& cfg) {
if (hasChar && !hasUnsigned && !hasSigned) continue;
if (nextWord == "double") continue;
if (isProtected(lineStart + runBegin)) continue;
std::string_view width = hasChar ? "8" : hasShort ? "16" : hasLong ? "64" : "32";
reps.push_back({lineStart + runBegin, runEnd - runBegin,
std::format("std::{}int{}_t", hasUnsigned ? "u" : "", width)});