Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A7-1-3: Triggered on static_cast #601

Closed
nbusser opened this issue Jun 1, 2024 · 2 comments · Fixed by #748
Closed

A7-1-3: Triggered on static_cast #601

nbusser opened this issue Jun 1, 2024 · 2 comments · Fixed by #748
Assignees
Labels
Difficulty-Medium A false positive or false negative report which is expected to take 1-5 days effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Medium Standard-AUTOSAR user-report Issue reported by an end user of CodeQL Coding Standards

Comments

@nbusser
Copy link

nbusser commented Jun 1, 2024

Affected rules

  • A7-1-3

Description

For some reason, A7-1-3 is triggered on basic varibales created from static_cast

Example

void false_positive() {
    std::uint8_t u8{0};

    auto const u32 = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning
    std::uint32_t const u32b = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning

    const auto u32c = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning
    const std::uint32_t u32d = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning
}
@nbusser nbusser added the false positive/false negative An issue related to observed false positives or false negatives. label Jun 1, 2024
@lcartey lcartey added the user-report Issue reported by an end user of CodeQL Coding Standards label Oct 15, 2024
@lcartey lcartey added Difficulty-Medium A false positive or false negative report which is expected to take 1-5 days effort to address Impact-Medium labels Oct 15, 2024
@lcartey lcartey self-assigned this Oct 15, 2024
@lcartey
Copy link
Collaborator

lcartey commented Oct 15, 2024

Thanks for this report!

The following cases are false positives:

    auto const u32 = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning
    std::uint32_t const u32b = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning

    const auto u32c = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning

These are reported because we confuse the use of std::uint32_t in the cast with the potential use of std::uint_t in the variable declaration type. This can be adjusted.

The final case is interesting:

    const std::uint32_t u32d = static_cast<std::uint32_t>(u8); // Triggers A7-1-3 warning

std::uint32_t is a typedef, and the rule for A7-1-3 states:

CV-qualifiers shall be placed on the right hand side of the type that is a typedef or a using name.

So, strictly speaking this is a true positive. However, the rule contradicts itself, because it later gives this example as compliant:

void Fn(const std::uint8_t& input) // Compliant

I don't know whether they just forgot std::uint8_t was a typedef, or they intended to suggest an exclusion for standard library types but it's not clearly called out, so, for now, we're sticking with the strict definition of the rule.

lcartey added a commit that referenced this issue Oct 15, 2024
We did not correctly constrain the type mention for the type to
be before the variable declaration itself.
@nbusser-sr
Copy link

Interesting! Thanks for the feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty-Medium A false positive or false negative report which is expected to take 1-5 days effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Medium Standard-AUTOSAR user-report Issue reported by an end user of CodeQL Coding Standards
Projects
4 participants