This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add a new check, readability-simplify-subscript-expr, that simplifies subscript expressions.
ClosedPublic

Authored by shuaiwang on Apr 16 2018, 2:42 PM.

Event Timeline

shuaiwang created this revision.Apr 16 2018, 2:42 PM

Please also take a look on 26817 for another idea for .data().

clang-tidy/readability/RedundantDataCallCheck.cpp
20 ↗(On Diff #142701)

Please separate with empty line.

32 ↗(On Diff #142701)

Isn't this list redundant for using namespace clang::ast_matchers?

docs/ReleaseNotes.rst
63

I would suggest Finds redundant .data() calls. Same in documentation.

Please also move to new checks list in alphabetical order.

docs/clang-tidy/checks/readability-redundant-data-call.rst
14 ↗(On Diff #142701)

Please look on other check documentation for example of option descriptions.

Eugene.Zelenko added a project: Restricted Project.
Eugene.Zelenko added inline comments.Apr 16 2018, 3:46 PM
docs/ReleaseNotes.rst
63

Please enclose .data() in ``. Same in documentation.

Have you run this over any large code bases to see whether the check triggers in practice?

docs/clang-tidy/checks/list.rst
96

This change looks unrelated to the patch and should be handled separately.

docs/clang-tidy/checks/readability-redundant-data-call.rst
6 ↗(On Diff #142701)

What does it mean for a data() call to be redundant? I think the documentation needs to explain that a bit more.

shuaiwang updated this revision to Diff 143204.Apr 19 2018, 5:51 PM
shuaiwang marked 7 inline comments as done.

Addressed review comments.

Eugene.Zelenko added inline comments.Apr 19 2018, 6:00 PM
clang-tidy/readability/RedundantDataCallCheck.cpp
22 ↗(On Diff #143204)

Actually you should use static, not anonymous namespace for variables and functions.

docs/ReleaseNotes.rst
143

r is after p :-)

146

Please remove This check and enclose .data() in `, not . Same for documentation.

docs/clang-tidy/checks/readability-redundant-data-call.rst
6 ↗(On Diff #143204)

Please use as much of 80 characters as possible.

8 ↗(On Diff #143204)

operator[] should be enclosed in `, not .

14 ↗(On Diff #143204)

Please insert empty line above.

22 ↗(On Diff #143204)

I think will be good idea to specify default value as complete string.

shuaiwang updated this revision to Diff 143207.Apr 19 2018, 6:17 PM
shuaiwang marked 7 inline comments as done.

Addressed more comments.

clang-tidy/readability/RedundantDataCallCheck.cpp
22 ↗(On Diff #143204)

Done, but why?

Eugene.Zelenko added inline comments.Apr 19 2018, 6:28 PM
clang-tidy/readability/RedundantDataCallCheck.cpp
22 ↗(On Diff #143204)
shuaiwang marked 2 inline comments as done.Apr 29 2018, 11:28 AM

Have you run this over any large code bases to see whether the check triggers in practice?

I'm still curious about this, btw.

clang-tidy/readability/RedundantDataCallCheck.cpp
45 ↗(On Diff #143207)

Should this check apply equally to std::string::c_str() as well as std::string::data()?

57 ↗(On Diff #143207)

I think the word "redundant" isn't going to be immediately obvious to a user who would write code like that. Perhaps "accessing an element of the container does not require a call to 'data()'; did you mean to use 'operator[]()' instead?"

docs/clang-tidy/checks/readability-redundant-data-call.rst
7 ↗(On Diff #143207)

doing array subscript -> doing an array subscript

Eugene.Zelenko added inline comments.May 2 2018, 6:23 AM
clang-tidy/readability/RedundantDataCallCheck.cpp
45 ↗(On Diff #143207)

readability-redundant-string-cstr do both.

aaron.ballman added inline comments.May 2 2018, 6:38 AM
clang-tidy/readability/RedundantDataCallCheck.cpp
31 ↗(On Diff #143207)

No need to register any of these matchers unless in C++ mode.

45 ↗(On Diff #143207)

Yup! But that makes me wonder if the name "redundant-data-call" is an issue. Perhaps the check name should focus more on the array subscript in the presence of an operator[]()?

shuaiwang added inline comments.May 2 2018, 5:04 PM
clang-tidy/readability/RedundantDataCallCheck.cpp
45 ↗(On Diff #143207)

How about "readability-circumlocutionary-subscript"?
"readability-circumlocutionary-element-access"?
"circumlocutionary" -> "verbose"?

Have you run this over any large code bases to see whether the check triggers in practice?

I'm still curious about this, btw.

Yes it triggers in Google's code base.

Have you run this over any large code bases to see whether the check triggers in practice?

I'm still curious about this, btw.

Yes it triggers in Google's code base.

Were there any false positives that you saw?

Have you run this over any large code bases to see whether the check triggers in practice?

I'm still curious about this, btw.

Yes it triggers in Google's code base.

Were there any false positives that you saw?

From randomly checking several triggerings no I didn't find any false positives. I feel the check should be pretty safe in terms of false positives because we only trigger on configured types.

Have you run this over any large code bases to see whether the check triggers in practice?

I'm still curious about this, btw.

Yes it triggers in Google's code base.

Were there any false positives that you saw?

From randomly checking several triggerings no I didn't find any false positives. I feel the check should be pretty safe in terms of false positives because we only trigger on configured types.

Good to hear, thank you for the information.

clang-tidy/readability/RedundantDataCallCheck.cpp
45 ↗(On Diff #143207)

hah, I think circumlocutionary might be a bit too much. ;-) I think readability-simplify-array-subscript-expr might be reasonable, however. Right now, the simplification is just for foo.data()[0] but it seems plausible that there are other array subscript simplifications that could be added in the future, like a[1 + 1] being converted to a[2] or x ? a[200] : a[400] going to a[x ? 200 : 400] (etc).

shuaiwang added inline comments.May 7 2018, 11:15 AM
clang-tidy/readability/RedundantDataCallCheck.cpp
45 ↗(On Diff #143207)

Just readability-simplify-subscript-expr?
Since after simplification the subscript operation is done by calling overloaded operator[] on an object instead of built-in subscript operator on an array.

Let me know if this name looks good to you and I'll do the actual renaming (together with addressing other comments) after your confirmation.

aaron.ballman added inline comments.May 7 2018, 1:21 PM
clang-tidy/readability/RedundantDataCallCheck.cpp
45 ↗(On Diff #143207)

I think readability-simplify-subscript-expr is a reasonable name.

shuaiwang retitled this revision from [clang-tidy] Add a new check, readability-redundant-data-call, that finds and removes redundant calls to .data(). to [clang-tidy] Add a new check, readability-simplify-subscript-expr, that simplifies subscript expressions..May 13 2018, 9:53 AM
shuaiwang marked 10 inline comments as done.

Rename to readability-simplify-subscript-expr and addressed other comments.

aaron.ballman accepted this revision.May 13 2018, 2:17 PM

LGTM with a few minor nits to be fixed.

clang-tidy/readability/SimplifySubscriptExprCheck.cpp
54–55

Can remove the braces.

63

Remove braces.

clang-tidy/readability/SimplifySubscriptExprCheck.h
23

This URL looks stale.

This revision is now accepted and ready to land.May 13 2018, 2:17 PM
shuaiwang updated this revision to Diff 146532.May 13 2018, 3:12 PM
shuaiwang marked 3 inline comments as done.

Addressed review comments.

Addressed review comments.

This patch was approved; do you need someone to commit this for you?

Addressed review comments.

This patch was approved; do you need someone to commit this for you?

Yes please :)

aaron.ballman closed this revision.May 16 2018, 1:16 PM

Committed in r332519, thank you for the patch!