This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] [modernize-use-auto] Add a threshold for minimal type name length to be replaced with 'auto'
ClosedPublic

Authored by zinovy.nis on Apr 7 2018, 10:22 AM.

Details

Summary

The threshold option is 'MinTypeNameLength' with default value '5'.
With MinTypeNameLength = 5 'int'/'bool' and 'const int'/'const bool' will not be converted to 'auto',
while 'unsigned' and 'long long int' will be.

Diff Detail

Event Timeline

zinovy.nis created this revision.Apr 7 2018, 10:22 AM

Please add to release notes.

  • Updated ReleaseNotes.
This revision is now accepted and ready to land.Apr 9 2018, 2:25 AM
hokein added a subscriber: hokein.Apr 9 2018, 2:32 AM
hokein added inline comments.
docs/clang-tidy/checks/modernize-use-auto.rst
202

nit: document the default value.

alexfh added inline comments.Apr 9 2018, 4:29 AM
clang-tidy/modernize/UseAutoCheck.cpp
290

Maybe make the default 5? Or does anyone really want to replace int/long/char/bool/... with auto?

lebedev.ri added inline comments.
clang-tidy/modernize/UseAutoCheck.cpp
290

That might be a bit surprising behavioral change..
At least it should be spelled out in the release notes.
(my 5 cent: defaulting to 0 would be best)

zinovy.nis added inline comments.Apr 9 2018, 5:18 AM
clang-tidy/modernize/UseAutoCheck.cpp
290

Maybe we can somehow mention the current option value in a warning message?

lebedev.ri added inline comments.Apr 9 2018, 6:07 AM
clang-tidy/modernize/UseAutoCheck.cpp
290

Sure, can be done, but that will only be seen when it does fire, not when it suddenly stops firing...

alexfh requested changes to this revision.Apr 9 2018, 8:14 AM
alexfh added inline comments.
clang-tidy/modernize/UseAutoCheck.cpp
290

Maybe we can somehow mention the current option value in a warning message?

We don't do that for options of other checks (and for the other option here). I don't think this case is substantially different.

290

That might be a bit surprising behavioral change..

For many it will be a welcome change ;)

At least it should be spelled out in the release notes.

No objections here.

(my 5 cent: defaulting to 0 would be best)

You see it, 5 is a better default, otherwise you'd say "0 cent" ;)

On a serious note, the style guides I'm more or less familiar with recommend the use of auto for "long/cluttery type names" [1], "if and only if it makes the code more readable or easier to maintain" [2], or to "save writing a longish, hard-to-remember type that the compiler already knows but a programmer could get wrong" [3]. None of these guidelines seem to endorse the use of auto instead of int, bool or the like.

From my perspective, the default value of 5 would cover a larger fraction of real-world use cases.

[1] https://google.github.io/styleguide/cppguide.html#auto
[2] http://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable
[3] https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names

420–421

Consider using tooling::fixit::getText(Loc, Context) instead.

This revision now requires changes to proceed.Apr 9 2018, 8:14 AM
zinovy.nis added inline comments.Apr 9 2018, 8:34 AM
clang-tidy/modernize/UseAutoCheck.cpp
290

Or even 7 to ignore 'double' too.

alexfh added inline comments.Apr 9 2018, 9:09 AM
clang-tidy/modernize/UseAutoCheck.cpp
290

I'd go with 5, which is super-easy to explain: it's the smallest value that will always lead to shorter code. 7 doesn't have this advantage.

zinovy.nis marked 9 inline comments as done.
  • Default value is 5.
  • Switched to 'tooling::fixit::getText'.
  • Updated rst docs.
zinovy.nis edited the summary of this revision. (Show Details)Apr 9 2018, 12:50 PM
alexfh accepted this revision.Apr 10 2018, 8:14 AM

Looks good. Thank you!

This revision is now accepted and ready to land.Apr 10 2018, 8:14 AM
This revision was automatically updated to reflect the committed changes.

This change had two different problems.
Please watch the bots?

Roman, I see you've fixed them. Thanks a lot!
I did not face with these errors on MSVS'201 so had no chance to fix early.

ср, 11 апр. 2018 г. в 0:02, Roman Lebedev via Phabricator <
reviews@reviews.llvm.org>:

lebedev.ri added a comment.

This change had two different problems.
Please watch the bots?

Repository:

rL LLVM

https://reviews.llvm.org/D45405

Roman, I see you've fixed them. Thanks a lot!
I did not face with these errors on MSVS'201 so had no chance to fix early.

No stress, but as Roman said, please watch the bots after committing a patch: http://lab.llvm.org:8011/console.

ср, 11 апр. 2018 г. в 0:02, Roman Lebedev via Phabricator <
reviews@reviews.llvm.org>:

lebedev.ri added a comment.

This change had two different problems.
Please watch the bots?

The build was broken by someone else's commit then. From my side there was
a warning only I fixed immediately.

ср, 11 апр. 2018 г. в 16:26, Alexander Kornienko via Phabricator <
reviews@reviews.llvm.org>:

alexfh added a comment.

In https://reviews.llvm.org/D45405#1063890, @zinovy.nis wrote:

Roman, I see you've fixed them. Thanks a lot!
I did not face with these errors on MSVS'201 so had no chance to fix

early.

No stress, but as Roman said, please watch the bots after committing a
patch: http://lab.llvm.org:8011/console.

ср, 11 апр. 2018 г. в 0:02, Roman Lebedev via Phabricator <
reviews@reviews.llvm.org>:

lebedev.ri added a comment.

This change had two different problems.
Please watch the bots?

Repository:

rL LLVM

https://reviews.llvm.org/D45405