This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Extend readability-container-size-empty to add comparisons to newly-constructed objects
ClosedPublic

Authored by joshz on Mar 31 2017, 9:22 AM.

Details

Summary

If checking for emptiness, suggest:

if (foo.empty())

instead of

if (foo != T())  (T == decltype(foo))

or

if (foo != "")

(And similarly for the negation, and similarly for the Yoda version of these comparisons.)

Diff Detail

Repository
rL LLVM

Event Timeline

joshz created this revision.Mar 31 2017, 9:22 AM
joshz added a comment.Apr 10 2017, 5:40 PM

Hey there, reviewers.
Any chance you can take a look at this change?

Thanks! :-)

aaron.ballman edited edge metadata.Apr 15 2017, 6:31 AM

... which has the wrong precedence; an extra set of parens is necessary.
However, I don't want to add a set of parens if it isn't necessary. ...

One way to handle this is to not suggest the fixit if the container part of the equality check is not a DeclRefExpr (perhaps after ignoring implicit casts, etc). This means the fixit won't get suggested in some cases where it otherwise could, such as:

std::vector<std::string> v;
if (v[0] == "") {
}

but it does mean that it will still catch the common cases.

clang-tidy/readability/ContainerSizeEmptyCheck.cpp
70

You should pull the cxxConstructExpr(hasDeclaration(cxxConstructorDecl(isDefaultConstructor()))) into a separate variable and reuse it instead of spelling it out four times.

joshz updated this revision to Diff 95488.Apr 17 2017, 2:57 PM
joshz marked an inline comment as done.
joshz edited the summary of this revision. (Show Details)

Resolved the bug, with a slightly modified version of Aaron's suggestion. (It will suggest parens for anything that wasn't just a DeclRefExpr, which may be a bit over-exuberant, but not as much as always suggesting them.)

This revision is now accepted and ready to land.Apr 18 2017, 6:40 AM
joshz added a comment.Apr 18 2017, 9:11 AM

Thanks, Aaron!

joshz added a comment.Apr 18 2017, 9:13 AM

I don't believe I have access to commit this revision myself; can someone please do it for me?

Thanks! :-)

alexfh accepted this revision.Apr 18 2017, 11:03 AM

LG with one nit.

clang-tidy/readability/ContainerSizeEmptyCheck.cpp
209

nit: Please remove the trailing period to follow the style other diagnostic messages use.

joshz updated this revision to Diff 95655.EditedApr 18 2017, 4:15 PM

Updated to fix the nit as well as to take into account some suggestions by sbenza@ on dealing with the parentheses; IsBinaryOrTernary is based on a function he wrote at Google.

PTAL.

joshz marked an inline comment as done.Apr 18 2017, 4:16 PM
aaron.ballman added inline comments.Apr 19 2017, 11:29 AM
clang-tidy/utils/ASTUtils.cpp
28 ↗(On Diff #95655)

Rather than call IgnoreImpCasts() three times, you should hoist it out of the if statements.

33 ↗(On Diff #95655)

binary doesn't match the usual naming conventions, and the asterisk should bind right rather than left.

clang-tidy/utils/ASTUtils.h
22 ↗(On Diff #95655)

The parameter name doesn't match our usual naming conventions. I'd just go with E.

joshz updated this revision to Diff 95833.Apr 19 2017, 3:06 PM
joshz marked 3 inline comments as done.

Clean up IsBinaryOrTernary

joshz added a comment.Apr 21 2017, 5:01 PM

Are there any further changes I should make, or is this good to submit now?

Thanks!

Are there any further changes I should make, or is this good to submit now?

Thanks!

This still LGTM, so it's good to submit. Do you still need someone to land the patch for you?

joshz added a comment.Apr 24 2017, 8:00 AM

Are there any further changes I should make, or is this good to submit now?

Thanks!

This still LGTM, so it's good to submit. Do you still need someone to land the patch for you?

I don't have commit access, so that would be great.

Thanks!

aaron.ballman closed this revision.Apr 24 2017, 8:10 AM

I've commit in r301185, thank you!