Skip to content

[clang-tidy] Keep parentheses when replacing index access in sizeof calls #82166

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

Merged
merged 5 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,17 @@ void LoopConvertCheck::doConversion(
ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow
? VarNameOrStructuredBinding + "."
: VarNameOrStructuredBinding;
auto Parents = Context->getParents(*Usage.Expression);
const DynTypedNodeList Parents = Context->getParents(*Usage.Expression);
if (Parents.size() == 1) {
if (const auto *Paren = Parents[0].get<ParenExpr>()) {
// Usage.Expression will be replaced with the new index variable,
// and parenthesis around a simple DeclRefExpr can always be
// removed.
Range = Paren->getSourceRange();
// removed except in case of a `sizeof` operator call.
const DynTypedNodeList GrandParents = Context->getParents(*Paren);
if (GrandParents.size() != 1 ||
GrandParents[0].get<UnaryExprOrTypeTraitExpr>() == nullptr) {
Range = Paren->getSourceRange();
}
} else if (const auto *UOP = Parents[0].get<UnaryOperator>()) {
// If we are taking the address of the loop variable, then we must
// not use a copy, as it would mean taking the address of the loop's
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ Changes in existing checks
`AllowStringArrays` option, enabling the exclusion of array types with deduced
length initialized from string literals.

- Improved :doc:`modernize-loop-convert
<clang-tidy/checks/modernize/loop-convert>` check by ensuring that fix-its
don't remove parentheses used in ``sizeof`` calls when they have array index
accesses as arguments.

- Improved :doc:`modernize-use-override
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
whitespace when deleting the ``virtual`` keyword.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ void f() {
// CHECK-FIXES-NEXT: printf("Fibonacci number %d has address %p\n", I, &I);
// CHECK-FIXES-NEXT: Sum += I + 2;

int Matrix[N][12];
unsigned size = 0;
for (int I = 0; I < N; ++I) {
size += sizeof(Matrix[I]);
size += sizeof Matrix[I];
size += sizeof((Matrix[I]));
}
// CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead
// CHECK-FIXES: for (auto & I : Matrix)
// CHECK-FIXES-NEXT: size += sizeof(I);
// CHECK-FIXES-NEXT: size += sizeof I;
// CHECK-FIXES-NEXT: size += sizeof(I);

Val Teas[N];
for (int I = 0; I < N; ++I) {
Teas[I].g();
Expand Down