← Back to Pull requests

[clang-tidy] Fix duplicated struct body in modernize-use-using #247637

Open
Neeraj3008 opened this pull request with 44 lines diff across 3 files
View on GitHub
Neeraj3008 Neeraj3008 commented on Dec 01, 2026 13:42 UTC
Author

Fixes the duplicated struct body produced by modernize-use-using when converting typedefs containing anonymous struct declarations with comments.

The suffix text was previously extracted before accounting for the complete tag declaration. When a comment was present, this caused the struct body to be retained in the suffix and duplicated in the generated alias.

Recalculate the suffix text starting immediately after the complete tag declaration. This preserves comments without including the struct body twice.

Add regression coverage for anonymous structs containing both leading and trailing member comments.

Testing:

  • Focused use-using.cpp lit test passed.

  • Full modernize test directory: 164 passed, 1 unsupported, 0 failed.
  • Fixes #246778

    AI assistance: ChatGPT was used to assist with debugging, understanding the relevant code, and developing the implementation and regression tests. I reviewed and tested the final changes and take responsibility for this contribution.

    Diff: 44 lines (+32 / -12) across 3 files Reviewer: zeyi2 Review duration: 4855m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.34) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.34 · 44 lines diff across 3 files
    0.34
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 5 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.228
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    7 items
    github-actions[bot] github-actions[bot] commented on Dec 01, 2026 13:42 UTC
    Bot

    <!--LLVM NEW CONTRIBUTOR COMMENT-->

    Hello @Neeraj3008 :wave:

    Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

    All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
    The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.

  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
  • Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.

    ---

    Frequently asked questions

    How do I add reviewers?

    This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

    You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

    What if there are no comments?

    If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

    Are any special GitHub settings required to contribute to LLVM?

    We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.

    ---

    If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

    Thank you,
    The LLVM Community

    llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Dec 01, 2026 13:43 UTC
    Bot

    <!--LLVM PR SUMMARY COMMENT-->
    @llvm/pr-subscribers-clang-tidy

    @llvm/pr-subscribers-clang-tools-extra

    Author: Neeraj3008

    <details>
    <summary>Changes</summary>

    Fixes the duplicated struct body produced by modernize-use-using when converting typedefs containing anonymous struct declarations with comments.

    The suffix text was previously extracted before accounting for the complete tag declaration. When a comment was present, this caused the struct body to be retained in the suffix and duplicated in the generated alias.

    Recalculate the suffix text starting immediately after the complete tag declaration. This preserves comments without including the struct body twice.

    Add regression coverage for anonymous structs containing both leading and trailing member comments.

    Testing:

  • Focused use-using.cpp lit test passed.

  • Full modernize test directory: 164 passed, 1 unsupported, 0 failed.
  • Fixes #<!-- -->246778

    ---
    Full diff: https://github.com/llvm/llvm-project/pull/247637.diff

    2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp (+19-12)

  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp (+9)
  • ``````

    diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
    index 504a2c40bcfff8..c2359276358529 100644
    --- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
    +++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
    @@ -439,18 +439,6 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
       if (!IsFirstTypedefInGroup)
         stripLeadingComma(SuffixTextInfo);
     
    -  const bool SuffixHasComment = SuffixTextInfo.Tokens.HasComment;
    -  std::string SuffixText;
    -  if (SuffixHasComment) {
    -    SuffixText = SuffixTextInfo.Text;
    -  } else if (QualifierStr.empty() &&
    -             findFirstNonWhitespace(SuffixTextInfo.Text) != StringRef::npos &&
    -             SuffixTextInfo.Tokens.HasPointerOrRef &&
    -             !SuffixTextInfo.Tokens.HasIdentifier) {
    -    SuffixText = SuffixTextInfo.Text;
    -  }
    -  const std::string QualifierText = SuffixHasComment ? "" : QualifierStr;
    -
       if (!ReplaceRange.getEnd().isMacroID()) {
         const SourceLocation::IntTy Offset = FunctionPointerCase ? 0 : Name.size();
         LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Offset);
    @@ -467,9 +455,28 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
             CharSourceRange::getTokenRange(LastTagDeclRange->second), SM, LO);
         if (!TagType)
           return;
    +
         Type = *TagType;
    +
    +    const SourceLocation AfterTag = Lexer::getLocForEndOfToken(
    +        LastTagDeclRange->second.getEnd(), 0, SM, LO);
    +
    +    SuffixTextInfo = getRangeTextInfo(AfterTag, NameLoc, SM, LO);
    +  }
    +
    +  const bool SuffixHasComment = SuffixTextInfo.Tokens.HasComment;
    +  std::string SuffixText;
    +
    +  if (SuffixHasComment) {
    +    SuffixText = SuffixTextInfo.Text;
    +  } else if (QualifierStr.empty() &&
    +             findFirstNonWhitespace(SuffixTextInfo.Text) != StringRef::npos &&
    +             SuffixTextInfo.Tokens.HasPointerOrRef &&
    +             !SuffixTextInfo.Tokens.HasIdentifier) {
    +    SuffixText = SuffixTextInfo.Text;
       }
     
    +  const std::string QualifierText = SuffixHasComment ? "" : QualifierStr;
       std::string TypeExpr =
           LeadingTextInfo.Text + Type + QualifierText + SuffixText;
       TypeExpr = StringRef(TypeExpr).rtrim(" \t").str();
    diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
    index 36dc93faca4615..22c137ca0db035 100644
    --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
    +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
    @@ -144,6 +144,15 @@ typedef struct Q1 { int a; } S1;
     typedef struct { int b; } S2;
     // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
     // CHECK-FIXES: using S2 = struct { int b; };
    +typedef struct {
    +  /// comment
    +  int n;
    +} S2LeadingComment;
    +// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: use 'using' instead of 'typedef'
    +// CHECK-FIXES: using S2LeadingComment = struct {
    +// CHECK-FIXES-NEXT:   /// comment
    +// CHECK-FIXES-NEXT:   int n;
    +// CHECK-FIXES-NEXT: };
     struct Q2 { int c; } typedef S3;
     // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
     // CHECK-FIXES: using S3 = struct Q2 { int c; };
    
    
    ``````

    </details>

    Neeraj3008 Neeraj3008 commented on Dec 01, 2026 13:50 UTC
    Author

    I have read the LLVM AI Tool Use Policy, Code-Review Policy and Practices, and Developer Policy. I have also updated the PR description to disclose my AI tool usage. I understand that I am responsible for reviewing and explaining all submitted changes.

    zeyi2 zeyi2 commented on Dec 01, 2026 15:34 UTC
    Reviewer

    Please add a Release Note entry :)

    Neeraj3008 Neeraj3008 commented on Dec 04, 2026 09:35 UTC
    Author

    @zeyi2 I've added the requested release note and resolved the merge conflicts by rebasing my branch onto the latest upstream main.

    Neeraj3008 Neeraj3008 commented on Dec 04, 2026 12:46 UTC
    Author

    i noticed macOS arm64 ci check is failing during cmake configurations because the installed cmake binary requires a new macOS version than the runner provides. is this a know environment issue or is there something i should investigate

    Reviewers
    Assignees
    Neeraj3008 Neeraj3008
    Labels
    risk: low confidence: 89.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 33.7% (LOW) · Higher = larger blast radius
    33.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.8% · Lower = rushed or shallow review
    89.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.4% · Safe to merge
    3.4%