[clang-tools-extra] [clang-tidy] Enhance container-data-pointer-check to suggest `c_str` for consts (PR #190590)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 8 13:49:05 PDT 2026
================
@@ -99,6 +102,34 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else if (ACE)
CE = ACE;
+ bool UseCStr = false;
+ if (CStrDecl) {
+ auto Parents = Result.Context->getParents(*UO);
+
+ if (!Parents.empty()) {
+ if (const auto *VD = Parents[0].get<VarDecl>()) {
+ const QualType VarType = VD->getType();
+ if (VarType->isPointerType()) {
+ const QualType PointeeType = VarType->getPointeeType();
+ UseCStr = PointeeType.isConstQualified();
+ }
+ } else if (const auto *Cast = Parents[0].get<CastExpr>()) {
+ const QualType CastType = Cast->getType();
+ if (CastType->isPointerType()) {
+ const QualType PointeeType = CastType->getPointeeType();
+ UseCStr = PointeeType.isConstQualified();
+ }
+ }
+ }
+
+ if (!UseCStr) {
+ QualType ContainerType = CE->getType();
+ if (ContainerType->isPointerType())
+ ContainerType = ContainerType->getPointeeType();
+ UseCStr = ContainerType.isConstQualified();
+ }
+ }
+
----------------
unterumarmung wrote:
I think this should be a separate `static` function like `shouldUseCStr`. This way the code can be simplified to have less deep nesting.
https://github.com/llvm/llvm-project/pull/190590
More information about the cfe-commits
mailing list