[PATCH] D129570: Add new clang-tidy check to find implicit conversions from enum to integer.

Paul Fultz II via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 12 09:00:01 PDT 2022


pfultz2 created this revision.
pfultz2 added reviewers: aaron.ballman, alexfh.
pfultz2 added a project: clang-tools-extra.
Herald added subscribers: carlosgalvezp, mgorny.
Herald added a project: All.
pfultz2 requested review of this revision.
Herald added a subscriber: cfe-commits.

This check diagnoses instances where an enum is implicitly converted to an
integer. In C++11, enums can be defined as `enum class` which will prevent
such implicit conversion, however, `enum` provides no such guarantees to
prevent bugs. There can be many reasons why `enum` cannot be replaced with
`enum class` such as compatibility with C or legacy libraries.

This check will diagnose similar implicit conversions when using `enum` to
find the same class of bugs. Currently it will only warn on function or
constructor calls as such conversions are not clear to the user, but this
could be expanded in the future.

  void foo(int i);
  void f() {
      foo(e1); // e1 is implictly converted to an int
  }


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129570

Files:
  clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
  clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
  clang-tools-extra/clang-tidy/bugprone/EnumToIntCheck.cpp
  clang-tools-extra/clang-tidy/bugprone/EnumToIntCheck.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/bugprone/enum-to-int.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/test/clang-tidy/checkers/bugprone/enum-to-int.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129570.443972.patch
Type: text/x-patch
Size: 7830 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220712/414f167d/attachment.bin>


More information about the cfe-commits mailing list