This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Change google-explicit-constructor to ignore conversions and operators marked `explicit(false)`
Needs ReviewPublic

Authored by njames93 on Jan 18 2022, 11:37 AM.

Details

Summary

These explicit(cond) feature in c++20 can be used to specify if a declaration should be implicit or explicit.
For these cases we shouldn't produce any warning as the developer is already enforcing a specific behaviour.

Fixes https://llvm.org/PR53115.

Diff Detail

Event Timeline

njames93 created this revision.Jan 18 2022, 11:37 AM
njames93 requested review of this revision.Jan 18 2022, 11:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 18 2022, 11:37 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Hmmm, this is a rule for checking a specific style guide. https://google.github.io/styleguide/cppguide.html#Implicit_Conversions doesn't say that explicit(false) is a reasonable marking. In fact, it says "Implicit conversions can sometimes be necessary and appropriate for types that are designed to be interchangeable, for example when objects of two types are just different representations of the same underlying value. In that case, contact your project leads to request a waiver of this rule."

So I think this behavior needs to be behind a flag so that the default behavior continues to match what the style guide requires (or the style guide should be updated to clarify the behavior of explicit(false)).

Hmmm, this is a rule for checking a specific style guide. https://google.github.io/styleguide/cppguide.html#Implicit_Conversions doesn't say that explicit(false) is a reasonable marking. In fact, it says "Implicit conversions can sometimes be necessary and appropriate for types that are designed to be interchangeable, for example when objects of two types are just different representations of the same underlying value. In that case, contact your project leads to request a waiver of this rule."

So I think this behavior needs to be behind a flag so that the default behavior continues to match what the style guide requires (or the style guide should be updated to clarify the behavior of explicit(false)).

My understanding of the rule(as a non Googler) was that explicit(false) is an effective way to disable the rule by explicitly declaring the constructor to be implicit. Which is much cleaner than throwing NOLINT markers about.
In any case this c++20 syntax is not supported as the current fixit produces invalid code, as evidenced in the initial bug report.

Hmmm, this is a rule for checking a specific style guide. https://google.github.io/styleguide/cppguide.html#Implicit_Conversions doesn't say that explicit(false) is a reasonable marking. In fact, it says "Implicit conversions can sometimes be necessary and appropriate for types that are designed to be interchangeable, for example when objects of two types are just different representations of the same underlying value. In that case, contact your project leads to request a waiver of this rule."

So I think this behavior needs to be behind a flag so that the default behavior continues to match what the style guide requires (or the style guide should be updated to clarify the behavior of explicit(false)).

My understanding of the rule(as a non Googler) was that explicit(false) is an effective way to disable the rule by explicitly declaring the constructor to be implicit. Which is much cleaner than throwing NOLINT markers about.

FWIW, I agree that this would be a reasonable exception to the rule and makes for cleaner code than NOLINT comments. I'm pointing out that the rule text doesn't say that exceptions are something they want to silence diagnostics for. The way I read the guideline suggests that they don't consider the diagnostic a false positive.

In any case this c++20 syntax is not supported as the current fixit produces invalid code, as evidenced in the initial bug report.

Agreed that issue needs to be fixed.

LegalizeAdulthood requested changes to this revision.Jun 22 2022, 3:06 PM

Clang-Tidy tests and docs have moved to module subdirectories.

Please rebase this onto main:HEAD and:

  • fold your changes into the appropriate subdirs, stripping the module prefix from new files.
  • make the target check-clang-extra to validate your tests
  • make the target docs-clang-tools-html to validate any docs changes
This revision now requires changes to proceed.Jun 22 2022, 3:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 22 2022, 3:06 PM
LegalizeAdulthood resigned from this revision.Mar 29 2023, 8:59 AM
This revision now requires review to proceed.Mar 29 2023, 8:59 AM

Is there an update on this? I agree adding a flag to control this behavior would be good but the Google style guide clearly does not take the explicit(false) option into consideration. Currently the only alternatives are a disabling the check completely or adding a bunch of NOLINT statements everywhere, neither of which is great.

Perhaps an alternative would be to add a separate check, either called explicit-constructor or google-explicit-constructor-permissive with this functionality.