Skip to content

Commit

Permalink
[clang-tidy]mark record initList as non-const param
Browse files Browse the repository at this point in the history
```
struct XY {
  int *x;
  int *y;
};
void recordInitList(int *x) {
  XY xy = {x, nullptr};
}
```
x cannot be const int* becase it in a initialize list which only accept int*

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D158152
  • Loading branch information
HerrCai0907 committed Aug 22, 2023
1 parent a0db738 commit 1c94124
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Expand Up @@ -103,7 +103,7 @@ void NonConstParameterCheck::check(const MatchFinder::MatchResult &Result) {
} else if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("Mark")) {
const QualType T = VD->getType();
if ((T->isPointerType() && !T->getPointeeType().isConstQualified()) ||
T->isArrayType())
T->isArrayType() || T->isRecordType())
markCanNotBeConst(VD->getInit(), true);
else if (T->isLValueReferenceType() &&
!T->getPointeeType().isConstQualified())
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Expand Up @@ -241,6 +241,10 @@ Changes in existing checks
do-while loops into account for the `AllowIntegerConditions` and
`AllowPointerConditions` options.

- Improved :doc:`readability-non-const-parameter
<clang-tidy/checks/readability/non-const-parameter>` check to ignore
false-positives in initializer list of record.

- Improved :doc:`readability-static-accessed-through-instance
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
identify calls to static member functions with out-of-class inline definitions.
Expand Down
Expand Up @@ -222,6 +222,24 @@ struct XY {
void recordpointer(struct XY *xy) {
*(xy->x) = 0;
}
void recordInitList(int *x) {
XY xy = {x, nullptr};
}

struct XYConst {
int const *x;
};
// CHECK-MESSAGES: :[[@LINE+1]]:30: warning: pointer parameter 'x' can be pointer to const
void recordInitListDiag(int *x) {
// CHECK-FIXES: {{^}}void recordInitListDiag(const int *x) {{{$}}
XYConst xy = {x};
}
typedef XYConst XYConstAlias;
// CHECK-MESSAGES: :[[@LINE+1]]:35: warning: pointer parameter 'x' can be pointer to const
void recordInitListAliasDiag(int *x) {
// CHECK-FIXES: {{^}}void recordInitListAliasDiag(const int *x) {{{$}}
XYConstAlias xy = {x};
}

class C {
public:
Expand Down

0 comments on commit 1c94124

Please sign in to comment.