This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Ignore default arguments in modernize-default-member-init
ClosedPublic

Authored by malcolm.parsons on Jan 4 2017, 3:59 AM.

Details

Summary

Default member initializers cannot refer to constructor parameters, but modernize-default-member-init was trying to when the default constructor had default arguments.

Change the check to ignore default arguments to the default constructor.

Fixes PR31524.

Diff Detail

Repository
rL LLVM

Event Timeline

malcolm.parsons retitled this revision from to [clang-tidy] Ignore default arguments in modernize-default-member-init.
malcolm.parsons updated this object.
malcolm.parsons added a subscriber: Eugene.Zelenko.
aaron.ballman edited edge metadata.Jan 4 2017, 7:12 AM

Btw, when creating a patch, it's helpful to the reviewers (especially ones who are only casually interested) to put some of the context from the bug report into the patch summary rather than only list the PR.

clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
161 ↗(On Diff #83031)

Any varDecl? Or do we want to limit it to only parmVarDecl instead?

int i = 12;
struct S {
  int j;
  S() : j(i) {}
};

It seems reasonable to suggest the member initialization be "fixed" to: int j = i;

malcolm.parsons edited edge metadata.
malcolm.parsons added inline comments.
clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
161 ↗(On Diff #83031)

I would find a default member initializer that is not a constant to be surprising.
Maybe I want to limit it to enumConstantDecl.

Only match DeclRefExprs to EnumConstantDecls.

aaron.ballman added inline comments.Jan 4 2017, 8:00 AM
clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
161 ↗(On Diff #83031)

I wouldn't; consider a global synchronization object, like a mutex (this is a relatively common thing).

malcolm.parsons added inline comments.Jan 4 2017, 9:22 AM
clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
161 ↗(On Diff #83031)

Note that this check doesn't consider initializations of members that are classes, e.g. std::lock_guard.
I have no plans to make it consider them.

aaron.ballman added inline comments.Jan 4 2017, 9:34 AM
clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
161 ↗(On Diff #83031)

Ah, okay, then it's less pressing, though the pattern is still not unheard of for non-class types. I don't have a strong opinion any longer, though.

test/clang-tidy/modernize-use-default-member-init.cpp
224 ↗(On Diff #83056)

Can we also have a positive test with an enumerator?

malcolm.parsons added inline comments.Jan 4 2017, 9:37 AM
test/clang-tidy/modernize-use-default-member-init.cpp
224 ↗(On Diff #83056)

See lines 159-166.

aaron.ballman accepted this revision.Jan 4 2017, 9:39 AM
aaron.ballman edited edge metadata.

LGTM!

test/clang-tidy/modernize-use-default-member-init.cpp
224 ↗(On Diff #83056)

Ah, thanks, my eyes missed that. :-)

This revision is now accepted and ready to land.Jan 4 2017, 9:39 AM
This revision was automatically updated to reflect the committed changes.