Skip to content

[Clippy] Modernise random/guid usage, fix discarded Guid bug, and remove dead code#244

Merged
sergey-tihon merged 2 commits intomasterfrom
clippy/improve-code-quality-20260427-a3a11aa348e8e944
Apr 28, 2026
Merged

[Clippy] Modernise random/guid usage, fix discarded Guid bug, and remove dead code#244
sergey-tihon merged 2 commits intomasterfrom
clippy/improve-code-quality-20260427-a3a11aa348e8e944

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated PR from Clippy, an AI assistant for this repository.

Summary

Four focused code-quality improvements across the library:

1. Random.Shared instead of new Random() (DocumentBuilder.cs)

GenStyleIdFromStyleName was calling (new Random()).Next(990). This allocates a new Random instance on every call and can produce identical sequences when called in rapid succession on older runtimes. Using Random.Shared is the idiomatic .NET 6+ approach: no allocation, thread-safe, and backed by a properly seeded shared instance.

2. Fix discarded Guid.NewGuid() result — latent bug (WmlComparer.Private.Methods.ProduceDocument.cs)

Two Select lambdas in the text-box ancestor-unid processing contained:

if (thisUnid == null)
    Guid.NewGuid().ToString().Replace("-", "");  // result discarded!
return thisUnid;  // still null

The assignment to thisUnid was accidentally dropped, so null would be returned and inserted into the AncestorUnids array — potentially causing downstream NullReferenceExceptions or incorrect comparison results. Fixed to thisUnid = Guid.NewGuid().ToString("N");.

3. Simplify Guid.ToString("N") (WmlComparer.Private.Methods.Util.cs)

Guid.NewGuid().ToString().Replace("-", "")Guid.NewGuid().ToString("N"). The "N" format specifier produces the same 32-character lowercase hex string without hyphens, but avoids the extra string allocation from Replace.

4. Remove #if false dead code (FieldRetriever.cs)

The original non-caching implementation of InstrText was left behind a #if false block with a comment noting the caching version is "significantly faster". This block has been dead code for a long time and can be safely removed.

Test Status

  • Build: ✅ 0 errors
  • Tests: ✅ 1699 passed, 1 skipped (pre-existing), 0 failed

Generated by 🌈 Clippy, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@3de4e604a36b5190a1c7dc4719c7341500ba8a95

- Replace `new Random()` with `Random.Shared` in GenStyleIdFromStyleName for
  thread safety and to avoid unnecessary allocations (DocumentBuilder.cs)
- Fix discarded Guid.NewGuid() assignments in WmlComparer text-box ancestor
  processing — result was computed but never assigned, leaving `thisUnid` null
  (WmlComparer.Private.Methods.ProduceDocument.cs)
- Simplify `Guid.NewGuid().ToString().Replace("-", "")` to
  `Guid.NewGuid().ToString("N")` for clarity and efficiency (WmlComparer.Private.Methods.Util.cs)
- Remove `#if false` dead code in FieldRetriever.cs — the old non-caching
  implementation has been superseded by the cached version

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sergey-tihon sergey-tihon marked this pull request as ready for review April 27, 2026 14:10
Copilot AI review requested due to automatic review settings April 27, 2026 14:10
@sergey-tihon sergey-tihon reopened this Apr 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Modernizes random/GUID generation patterns in Clippit’s Word-processing utilities and comparer pipeline, fixes a latent bug where generated GUIDs were discarded, and removes long-dead conditional code.

Changes:

  • Use Random.Shared in DocumentBuilder to avoid per-call Random allocations and weak seeding behavior.
  • Replace Guid.NewGuid().ToString().Replace("-", "") with Guid.NewGuid().ToString("N") for lower-allocation GUID formatting.
  • Fix two lambdas in comparer document production where a newly generated GUID was previously discarded, allowing null ancestor IDs to leak.
  • Remove #if false dead code in FieldRetriever.InstrText.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
Clippit/Word/DocumentBuilder.cs Switch style-id suffix generation to Random.Shared.
Clippit/FieldRetriever.cs Remove dead #if false non-caching implementation block.
Clippit/Comparer/WmlComparer.Private.Methods.Util.cs Use Guid.ToString("N") for part URI name generation.
Clippit/Comparer/WmlComparer.Private.Methods.ProduceDocument.cs Fix discarded GUID result when ancestor Unid is missing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Clippit/Comparer/WmlComparer.Private.Methods.ProduceDocument.cs
Comment thread Clippit/Comparer/WmlComparer.Private.Methods.ProduceDocument.cs
@sergey-tihon
Copy link
Copy Markdown
Owner

@copilot fix inline comments

…torUnids

Agent-Logs-Url: https://github.com/sergey-tihon/Clippit/sessions/e8cd4284-e954-4e6f-84c0-8b361d9110c5

Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

@copilot fix inline comments

Fixed in commit 19d3cf2. Both lambdas now call ae.SetAttributeValue(PtOpenXml.Unid, thisUnid) immediately after generating the GUID, so repeated appearances of the same ancestor XElement get a consistent stable ID across all atoms.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sergey-tihon sergey-tihon merged commit c632acb into master Apr 28, 2026
7 checks passed
@sergey-tihon sergey-tihon deleted the clippy/improve-code-quality-20260427-a3a11aa348e8e944 branch April 28, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants