This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add a check for RAII temporaries.
ClosedPublic

Authored by bkramer on Jul 22 2014, 2:24 AM.

Details

Summary

This tries to find code similar that immediately destroys
an object that looks like it's trying to follow RAII.

{
  scoped_lock(&global_mutex);
  critical_section();
}

This checker will have false positives if someone uses this pattern
to legitimately invoke a destructor immediately (or the statement is
at the end of a scope anyway). To reduce the number we ignore this
pattern in macros (this is heavily used by gtest) and ignore objects
with no user-defined destructor.

Diff Detail

Repository
rL LLVM

Event Timeline

bkramer updated this revision to Diff 11741.Jul 22 2014, 2:24 AM
bkramer retitled this revision from to [clang-tidy] Add a check for RAII temporaries..
bkramer updated this object.
bkramer added reviewers: alexfh, djasper.
bkramer added a subscriber: Unknown Object (MLST).
djasper added inline comments.Jul 22 2014, 3:00 AM
clang-tidy/misc/UnusedRAII.cpp
51 ↗(On Diff #11741)

Maybe: "object destroyed immediately after creation; did .."?

Should we suggestion a fix? E.g. insert "give_me_a_name"?

test/clang-tidy/misc-unused-raii.cpp
21 ↗(On Diff #11741)

Add a test with a templated RAII class.

Also, make this "void main()" (or "void f()") or return something. We should try to get these tests warning free.

bkramer updated this revision to Diff 11742.Jul 22 2014, 3:26 AM
  • Reworded warning message.
  • Added a test case with a templated RAII object, renamed main method.
  • No FixItHint because we don't have a source location for the place where the name would go :(
bkramer updated this revision to Diff 11747.Jul 22 2014, 5:15 AM
  • Added FixIts. This is tricky because we have to remove the parens when encountering Foo(); // Foo is a type. -> Foo give_me_a_name;

otherwise it'll parse as a function decl.

  • Added another way of reducing false positives: If the expression is the last one in

the compoundStmt we don't warn, as it doesn't change behavior.

djasper accepted this revision.Jul 22 2014, 5:23 AM
djasper edited edge metadata.

I think, this looks good as a first version.

As discussed offline, follow-ups could:

  • Ignore objects that are known to have an empty destructor.
  • Ignore constructors with more than one argument (reducing false positives at the cost of false negatives).
  • Distinguish the case of attempted delegated constructors (consider adding a test now).
clang-tidy/misc/UnusedRAII.cpp
53 ↗(On Diff #11747)

Add a test for this code path.

72 ↗(On Diff #11747)

Hm. Fix-It-wise, wouldn't it be better to create a single replacement instead of an insertion and a removal?

clang-tidy/misc/UnusedRAII.h
36 ↗(On Diff #11747)

nit: missing period at the end of the sentence.

This revision is now accepted and ready to land.Jul 22 2014, 5:23 AM
bkramer closed this revision.Jul 22 2014, 5:39 AM
bkramer updated this revision to Diff 11748.

Closed by commit rL213647 (authored by d0k).