Skip to main content
ai-slop/cpp-c-style-castmanual fix

C-style cast.

A C-style cast such as `(int)x` or `(double)total` in C++ code. It may perform a static, const, or reinterpret conversion, and nothing in the syntax says which.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

cpp-c-style-cast.cpp
✗ Flagged
double average(const std::vector<int>& values) {
    long total = std::accumulate(values.begin(), values.end(), 0L);
    return (double)total / (int)values.size();
}
Why this trips

Deterministic line match on masked source: flags a parenthesised built-in type (`int`, `char`, `short`, `long`, `float`, `double`, `bool`, `size_t`, fixed-width integers, optionally `unsigned`, `signed`, `const`, or a pointer) followed by an expression, in C++ files.

cpp-c-style-cast.cpp
✓ Clean
double average(const std::vector<int>& values) {
    long total = std::accumulate(values.begin(), values.end(), 0L);
    return static_cast<double>(total) / static_cast<double>(values.size());
}
What changed

Use `static_cast` for value conversions, and `reinterpret_cast` or `const_cast` only where that is genuinely the intent, so reviewers can see what kind of conversion happens.

Agents trained on mixed C and C++ code reach for the shorter C syntax. It compiles, and hides whether the conversion is safe.

How this rule is justified.

Rule id

ai-slop/cpp-c-style-cast

Enforcing engine

ai-slop

Detector strategy

Deterministic line match on masked source: flags a parenthesised built-in type (`int`, `char`, `short`, `long`, `float`, `double`, `bool`, `size_t`, fixed-width integers, optionally `unsigned`, `signed`, `const`, or a pointer) followed by an expression, in C++ files.

Legitimate code that is NOT flagged

`.c` and `.h` files, casts to class or other user-defined types, and named casts such as `static_cast<int>(x)` are not flagged.

Evidence

Part of aislop's C/C++ rule set. C/C++ support, including C/C++-specific AI-slop rules, arrived in aislop v0.14.1.