Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google-explicit-constructor warns when explicit(false) is used #53115

Open
jgopel opened this issue Jan 10, 2022 · 6 comments
Open

google-explicit-constructor warns when explicit(false) is used #53115

jgopel opened this issue Jan 10, 2022 · 6 comments
Assignees

Comments

@jgopel
Copy link

jgopel commented Jan 10, 2022

explicit(false) can be seen as a signal that the developer considered the explicit case and determined that, in this instance, explicit is not warranted. Warning when explicit(false) is set forces a NOLINT that duplicates information already available in the syntax. Seen on clang-tidy-14.

struct foo {
    foo(int) {}  // Correctly warns
    explicit foo(long) {}  // Correctly does not warn
    explicit(false) foo(float) {}  // Incorrectly warns
    explicit(true) foo(double) {}  // Correctly does not warn
};

https://godbolt.org/z/4dojdv3s3

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 10, 2022

@llvm/issue-subscribers-clang-tidy

@njames93 njames93 self-assigned this Jan 18, 2022
@njames93
Copy link
Member

Put https://reviews.llvm.org/D117593 up for review

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 18, 2022

@llvm/issue-subscribers-c-20

@Febbe
Copy link

Febbe commented Jan 20, 2022

Maybe add also an option to either activate or deactivate the new behavior. Depending on which is chosen as default.
This would allow the Google people (and those, which use the style guide) to enforce this.
Also, it would be possible to activate a pipeline test to print a warning, so a reviewer can "accept" the implicit cast.

LGTM so far, but shouldn't all early returns like the following above/before the fixup/message construction?

  if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
      TakesInitializerList || Ctor->getExplicitSpecifier().isSpecified())
    return;

@njames93
Copy link
Member

Maybe I should add an option instead.
This does lead highlight another issue with the check not having support for these explicit specifiers.
Currently the fix emitted is like this

explict(false) CTor(int) {}

Transformed to

explicit(false) explicit Ctor(int) {}

Instead it should either be

explicit(true) Ctor(int) {}
// Or more favourable
explicit Ctor(int) {}

@jgopel
Copy link
Author

jgopel commented Jan 20, 2022

FWIW: I prefer explicit(true) to explicit - an option for the fixer to do explicit(true) instead of explicit would allow the user to select which is the preferred style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

No branches or pull requests

6 participants