Discussion:
[PATCH] D45405: [clang-tidy] [modernize-use-auto] Add a threshold for minimal type name length to be replaced with 'auto'
Zinovy Nis via Phabricator via cfe-commits
2018-04-07 17:24:34 UTC
Permalink
zinovy.nis created this revision.
zinovy.nis added reviewers: angelgarcia, malcolm.parsons, alexfh.
zinovy.nis added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, xazax.hun.

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


Repository:
rCTE Clang Tools Extra

https://reviews.llvm.org/D45405

Files:
clang-tidy/modernize/UseAutoCheck.cpp
clang-tidy/modernize/UseAutoCheck.h
docs/clang-tidy/checks/modernize-use-auto.rst
test/clang-tidy/modernize-use-auto-min-type-name-length.cpp
Malcolm Parsons via Phabricator via cfe-commits
2018-04-07 21:29:24 UTC
Permalink
malcolm.parsons added a comment.

Please add to release notes.


Repository:
rCTE Clang Tools Extra

https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-08 18:21:42 UTC
Permalink
zinovy.nis updated this revision to Diff 141561.
zinovy.nis added a comment.

- Updated ReleaseNotes.


https://reviews.llvm.org/D45405

Files:
clang-tidy/modernize/UseAutoCheck.cpp
clang-tidy/modernize/UseAutoCheck.h
docs/ReleaseNotes.rst
docs/clang-tidy/checks/modernize-use-auto.rst
test/clang-tidy/modernize-use-auto-min-type-name-length.cpp
Malcolm Parsons via Phabricator via cfe-commits
2018-04-09 09:28:19 UTC
Permalink
malcolm.parsons accepted this revision.
malcolm.parsons added a comment.
This revision is now accepted and ready to land.

LGTM!


https://reviews.llvm.org/D45405
Haojian Wu via Phabricator via cfe-commits
2018-04-09 09:32:30 UTC
Permalink
hokein added inline comments.


================
Comment at: docs/clang-tidy/checks/modernize-use-auto.rst:202
+ neither warn nor fix type names having a length less than the option value.
+ The option affects expressions only, not iterators.
+
----------------
nit: document the default value.


https://reviews.llvm.org/D45405
Alexander Kornienko via Phabricator via cfe-commits
2018-04-09 11:29:48 UTC
Permalink
alexfh added inline comments.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

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


https://reviews.llvm.org/D45405
Roman Lebedev via Phabricator via cfe-commits
2018-04-09 12:04:25 UTC
Permalink
lebedev.ri added inline comments.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)


https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-09 12:18:31 UTC
Permalink
zinovy.nis added inline comments.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)
Maybe we can somehow mention the current option value in a warning message?


https://reviews.llvm.org/D45405
Roman Lebedev via Phabricator via cfe-commits
2018-04-09 13:07:10 UTC
Permalink
lebedev.ri added inline comments.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Zinovy Nis via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)
Maybe we can somehow mention the current option value in a warning message?
Sure, can be done, but that will only be seen when it does fire, not when it suddenly stops firing...


https://reviews.llvm.org/D45405
Alexander Kornienko via Phabricator via cfe-commits
2018-04-09 15:17:08 UTC
Permalink
alexfh requested changes to this revision.
alexfh added inline comments.
This revision now requires changes to proceed.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Zinovy Nis via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)
Maybe we can somehow mention the current option value in a warning message?
Sure, can be done, but that will only be seen when it does fire, not when it suddenly stops firing...
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.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Zinovy Nis via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)
Maybe we can somehow mention the current option value in a warning message?
Sure, can be done, but that will only be seen when it does fire, not when it suddenly stops firing...
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.
That might be a bit surprising behavioral change..
For many it will be a welcome change ;)
Post by Roman Lebedev via Phabricator via cfe-commits
At least it should be spelled out in the release notes.
No objections here.
Post by Roman Lebedev via Phabricator via cfe-commits
(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


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:420-421
+ if (MinTypeNameLength != 0 &&
+ Lexer::getSourceText(CharSourceRange::getTokenRange(Range),
+ Context->getSourceManager(), Context->getLangOpts())
+ .size() < MinTypeNameLength)
----------------
Consider using tooling::fixit::getText(Loc, Context) instead.


https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-09 15:34:59 UTC
Permalink
zinovy.nis added inline comments.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Alexander Kornienko via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Zinovy Nis via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)
Maybe we can somehow mention the current option value in a warning message?
Sure, can be done, but that will only be seen when it does fire, not when it suddenly stops firing...
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.
That might be a bit surprising behavioral change..
For many it will be a welcome change ;)
Post by Alexander Kornienko via Phabricator via cfe-commits
At least it should be spelled out in the release notes.
No objections here.
Post by Alexander Kornienko via Phabricator via cfe-commits
(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
Or even 7 to ignore 'double' too.


https://reviews.llvm.org/D45405
Alexander Kornienko via Phabricator via cfe-commits
2018-04-09 16:09:19 UTC
Permalink
alexfh added inline comments.


================
Comment at: clang-tidy/modernize/UseAutoCheck.cpp:290
+ : ClangTidyCheck(Name, Context), RemoveStars(Options.get("RemoveStars", 0)),
+ MinTypeNameLength(Options.get("MinTypeNameLength", 0)) {}

----------------
Post by Zinovy Nis via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Zinovy Nis via Phabricator via cfe-commits
Post by Roman Lebedev via Phabricator via cfe-commits
Post by Alexander Kornienko via Phabricator via cfe-commits
Maybe make the default 5? Or does anyone really want to replace `int/long/char/bool/...` with `auto`?
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)
Maybe we can somehow mention the current option value in a warning message?
Sure, can be done, but that will only be seen when it does fire, not when it suddenly stops firing...
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.
That might be a bit surprising behavioral change..
For many it will be a welcome change ;)
Post by Alexander Kornienko via Phabricator via cfe-commits
At least it should be spelled out in the release notes.
No objections here.
Post by Alexander Kornienko via Phabricator via cfe-commits
(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
Or even 7 to ignore 'double' too.
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.


https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-09 19:49:35 UTC
Permalink
zinovy.nis updated this revision to Diff 141719.
zinovy.nis marked 9 inline comments as done.
zinovy.nis added a comment.

- Default value is **5**.
- Switched to 'tooling::fixit::getText'.
- Updated rst docs.


https://reviews.llvm.org/D45405

Files:
clang-tidy/modernize/UseAutoCheck.cpp
clang-tidy/modernize/UseAutoCheck.h
docs/ReleaseNotes.rst
docs/clang-tidy/checks/modernize-use-auto.rst
test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp
test/clang-tidy/modernize-use-auto-cast.cpp
test/clang-tidy/modernize-use-auto-min-type-name-length.cpp
test/clang-tidy/modernize-use-auto-new.cpp
Alexander Kornienko via Phabricator via cfe-commits
2018-04-10 15:14:54 UTC
Permalink
alexfh accepted this revision.
alexfh added a comment.
This revision is now accepted and ready to land.

Looks good. Thank you!


https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-10 18:08:43 UTC
Permalink
This revision was automatically updated to reflect the committed changes.
Closed by commit rL329730: [clang-tidy] [modernize-use-auto] Add a threshold for minimal type name length
 (authored by zinovy.nis, committed by ).
Herald added subscribers: llvm-commits, klimek.

Changed prior to commit:
https://reviews.llvm.org/D45405?vs=141719&id=141879#toc

Repository:
rL LLVM

https://reviews.llvm.org/D45405

Files:
clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.cpp
clang-tools-extra/trunk/clang-tidy/modernize/UseAutoCheck.h
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-auto.rst
clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast-remove-stars.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-cast.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-min-type-name-length.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-use-auto-new.cpp
Roman Lebedev via Phabricator via cfe-commits
2018-04-10 21:02:54 UTC
Permalink
lebedev.ri added a comment.

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


Repository:
rL LLVM

https://reviews.llvm.org/D45405
Zinovy Nis via cfe-commits
2018-04-11 04:16:45 UTC
Permalink
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 <
Post by Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added a comment.
This change had two different problems.
Please watch the bots?
rL LLVM
https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-11 04:17:04 UTC
Permalink
zinovy.nis added a subscriber: angelgarcia.
zinovy.nis added a comment.

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 <
Post by Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added a comment.
This change had two different problems.
Please watch the bots?
rL LLVM
https://reviews.llvm.org/D45405
Repository:
rL LLVM

https://reviews.llvm.org/D45405
Alexander Kornienko via Phabricator via cfe-commits
2018-04-11 13:26:13 UTC
Permalink
alexfh added a comment.
Post by Zinovy Nis via cfe-commits
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.
Post by Zinovy Nis via cfe-commits
ср, 11 апр. 2018 г. в 0:02, Roman Lebedev via Phabricator <
Post by Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added a comment.
This change had two different problems.
Please watch the bots?
Repository:
rL LLVM

https://reviews.llvm.org/D45405
Zinovy Nis via Phabricator via cfe-commits
2018-04-11 13:47:10 UTC
Permalink
zinovy.nis added a comment.

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 <
Post by Alexander Kornienko via Phabricator via cfe-commits
alexfh added a comment.
Post by Zinovy Nis via cfe-commits
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.
Post by Zinovy Nis via cfe-commits
ср, 11 апр. 2018 г. в 0:02, Roman Lebedev via Phabricator <
Post by Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added a comment.
This change had two different problems.
Please watch the bots?
rL LLVM
https://reviews.llvm.org/D45405
Repository:
rL LLVM

https://reviews.llvm.org/D45405

Loading...