Skip to content

[clang-tidy-17] bugprone-non-zero-enum-to-bool-conversion flags false positive #77427

Closed as not planned
@craffael

Description

@craffael

Steps to reproduce:

  1. Create the file main.cc:
#include <iostream>

enum class TikzOutputCtrl : unsigned int {
  RenderCells = 1,
  CellNumbering = 2
};

TikzOutputCtrl operator&(const TikzOutputCtrl &lhs, const TikzOutputCtrl &rhs) {
    return static_cast<TikzOutputCtrl>(static_cast<unsigned int>(lhs) &
                                   static_cast<unsigned int>(rhs));
}

int main() {
    auto output_ctrl = TikzOutputCtrl::RenderCells;
    const bool q = static_cast<bool>(output_ctrl & TikzOutputCtrl::CellNumbering);

    std::cout << q << '\n';

}
  1. Create a .clang-tidy file:
---
Checks:          'bugprone-non-zero-enum-to-bool-conversion'

  1. Run clang-tidy-17 with std=c++20. E.g. use the following CMakeLists.txt and set CMAKE_EXPORT_COMPILE_COMMANDS=On:
add_executable(main main.cc)
target_compile_features(main PUBLIC cxx_std_20)

Output of clang-tidy-17:

main.cc:15:20: warning: conversion of 'TikzOutputCtrl' into 'bool' will always return 'true', enum doesn't have a zero-value enumerator [bugprone-non-zero-enum-to-bool-conversion]
   15 |     const bool q = static_cast<bool>(output_ctrl & TikzOutputCtrl::CellNumbering);
      |                    ^
main.cc:3:12: note: enum is defined here
    3 | enum class TikzOutputCtrl : unsigned int {
      |            ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions