This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] add check cppcoreguidelines-pro-type-union-access
ClosedPublic

Authored by mgehre on Oct 15 2015, 1:49 PM.

Details

Summary

This check flags all access to members of unions. Passing unions as a
whole is not flagged.

Reading from a union member assumes that member was the last one
written, and writing to a union member assumes another member with a
nontrivial destructor had its destructor called. This is fragile because
it cannot generally be enforced to be safe in the language and so relies
on programmer discipline to get it right.

This rule is part of the "Type safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type7-avoid-accessing-members-of-raw-unions-prefer-variant-instead

Diff Detail

Repository
rL LLVM

Event Timeline

mgehre updated this revision to Diff 37515.Oct 15 2015, 1:49 PM
mgehre retitled this revision from to [clang-tidy] add check cppcoreguidelines-pro-type-union-access.
mgehre updated this object.
mgehre added a subscriber: cfe-commits.
sbenza accepted this revision.Oct 16 2015, 10:07 AM
sbenza edited edge metadata.

Seems straightforward.

This revision is now accepted and ready to land.Oct 16 2015, 10:07 AM
sbenza added inline comments.Oct 16 2015, 10:18 AM
clang-tidy/cppcoreguidelines/ProTypeUnionAccessCheck.cpp
20 ↗(On Diff #37515)

This should check that we are in C++ code.

if (!getLangOpts().CPlusPlus)
  return;
mgehre marked an inline comment as done.Oct 16 2015, 11:48 AM
This revision was automatically updated to reflect the committed changes.