This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix `readability-non-const-parameter` for parameter referenced by an lvalue
ClosedPublic

Authored by Sockke on Jan 12 2022, 12:11 AM.

Details

Summary

The checker missed a check for a case when the parameter is referenced by an lvalue and this could cause build breakages.

Diff Detail

Event Timeline

Sockke created this revision.Jan 12 2022, 12:11 AM
Sockke requested review of this revision.Jan 12 2022, 12:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2022, 12:11 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MTC added inline comments.Jan 12 2022, 12:44 AM
clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
86–99

86~99 is pretty close to 64~81, could you please refactor it?

Eugene.Zelenko retitled this revision from Fix `readability-non-const-parameter` for parameter referenced by an lvalue to [clang-tidy] Fix `readability-non-const-parameter` for parameter referenced by an lvalue.Jan 12 2022, 2:44 PM
Sockke added inline comments.Jan 14 2022, 3:38 AM
clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
86–99

86~99 is pretty close to 64~81, could you please refactor it?

FunctionDecl and CXXConstructDecl have different methods with the same name.

aaron.ballman accepted this revision.Feb 22 2022, 11:20 AM

LGTM with some small nits, but can you also add a release note for the fix? (If there was a bug report for this issue, you should mention that report in the release note and close the bug out.)

clang-tools-extra/test/clang-tidy/checkers/readability-non-const-parameter.cpp
292

Please spell out enough of the warning you don't expect to see (in all of these cases) so that someone reading the code knows what you expect to miss.

This revision is now accepted and ready to land.Feb 22 2022, 11:20 AM
Sockke updated this revision to Diff 410753.Feb 23 2022, 2:38 AM

Add a release note and improve test description information.

Sockke marked 2 inline comments as done.Feb 23 2022, 2:39 AM
MTC added inline comments.Feb 23 2022, 6:36 PM
clang-tools-extra/test/clang-tidy/checkers/readability-non-const-parameter.cpp
293

@Sockke Could you please add the following tests?

int &ref = std::ref(*p);
const int &cref = std::ref(*p);
const int &cref = std::cref(*p);
const int *ptr = std::as_const(p);
int *ptr = const_cast<int*>(std::as_const(p));
decltype(auto) ptr = p;
auto ptr = p;
Sockke added inline comments.Feb 23 2022, 6:56 PM
clang-tools-extra/test/clang-tidy/checkers/readability-non-const-parameter.cpp
293

These cases have been handled stably in another logic.

MTC added inline comments.Feb 23 2022, 7:05 PM
clang-tools-extra/test/clang-tidy/checkers/readability-non-const-parameter.cpp
293

Sorry for my mistake, look good to me!

Before landing make sure this is rebased against trunk, just so the pre-merge bot can run and verify there are no issues. It looks like this patch is based off a few commits that don't appear on trunk.

Sockke updated this revision to Diff 411288.Feb 24 2022, 6:56 PM

rebased.