This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Annotate immutable data structure methods with LLVM_NODISCARD.
ClosedPublic

Authored by NoQ on May 29 2018, 2:52 PM.

Details

Summary

This would warn the developer on the very common bug that consists in writing, eg., Factory.add(Set, Item); instead of Set = Factory.add(Set, Item);. Because in the first snippet the updated Set object is discarded while the original object remains unchanged (because it's, well, immutable), which isn't what you ever want to do.

LLVM_NODISCARD is essentially warn_unused_result.

For now no such bugs were found in llvm or clang (in fact, these data structures are mostly used by the clang static analyzer), but i made these mistakes myself knowingly and i see many such bugs during code review.

Adding @chandlerc because he's the code owner for ADT and analyzer folks because it's mostly about the analyzer.

Diff Detail

Repository
rL LLVM

Event Timeline

NoQ created this revision.May 29 2018, 2:52 PM
NoQ set the repository for this revision to rL LLVM.
george.karpenkov accepted this revision.May 29 2018, 3:00 PM
This revision is now accepted and ready to land.May 29 2018, 3:00 PM
This revision was automatically updated to reflect the committed changes.