Skip to content

Implement LuceneDev1007, 1008, 6000 Analyzers & CodeFix with Unit Tests#27

Merged
paulirwin merged 2 commits intoapache:mainfrom
paulirwin:issue/1168
Apr 26, 2026
Merged

Implement LuceneDev1007, 1008, 6000 Analyzers & CodeFix with Unit Tests#27
paulirwin merged 2 commits intoapache:mainfrom
paulirwin:issue/1168

Conversation

@paulirwin
Copy link
Copy Markdown
Contributor

@paulirwin paulirwin commented Apr 24, 2026

Adds diagnostics for dictionary indexer usage ported from Java:

  • LuceneDev1007 (Design/Warning): generic IDictionary<,> / IReadOnlyDictionary<,>
    indexer read with a value-type value — may throw KeyNotFoundException.
  • LuceneDev1008 (Design/Warning): same, but reference-type value; fix should also null-check the result.
  • LuceneDev6000 (Usage/Info): non-generic IDictionary indexer read, which returns null for missing keys and should be reviewed.

Includes a code fix for LuceneDev1007/1008 that rewrites the common return dict[key]; pattern into
return dict.TryGetValue(key, out var value) ? value : default;.

Fixes #1168

🤖 Generated with Claude Code

Adds diagnostics for dictionary indexer usage ported from Java:

- LuceneDev1007 (Design/Warning): generic IDictionary<,> / IReadOnlyDictionary<,>
  indexer read with a value-type value — may throw KeyNotFoundException.
- LuceneDev1008 (Design/Warning): same, but reference-type value; fix should also
  null-check the result.
- LuceneDev6000 (Usage/Info): non-generic IDictionary indexer read, which returns
  null for missing keys and should be reviewed.

Includes a code fix for LuceneDev1007/1008 that rewrites the common
`return dict[key];` pattern into
`return dict.TryGetValue(key, out var value) ? value : default;`.

Closes #1168.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@paulirwin paulirwin added the notes:new-feature A new feature label Apr 24, 2026
@paulirwin paulirwin marked this pull request as ready for review April 24, 2026 20:22
@paulirwin paulirwin requested a review from Copilot April 24, 2026 20:22
Copy link
Copy Markdown

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

Adds new Roslyn analyzers (LuceneDev1007/1008/6000) to flag dictionary indexer reads with risky missing-key semantics, plus a code fix for LuceneDev1007/1008 and accompanying unit tests/samples.

Changes:

  • Introduces LuceneDev1007/1008 analyzers for generic dictionary indexer reads (value-type vs reference-type TValue).
  • Introduces LuceneDev6000 analyzer for non-generic IDictionary/Hashtable indexer reads.
  • Adds a code fix that rewrites return dict[key]; into a TryGetValue-based conditional return, with unit tests and samples.

Reviewed changes

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

Show a summary per file
File Description
tests/Lucene.Net.CodeAnalysis.Dev.Tests/LuceneDev6xxx/TestLuceneDev6000_NonGenericDictionaryIndexerAnalyzer.cs Unit tests for the new LuceneDev6000 analyzer behavior.
tests/Lucene.Net.CodeAnalysis.Dev.Tests/LuceneDev1xxx/TestLuceneDev1007_1008_DictionaryIndexerAnalyzer.cs Unit tests for new LuceneDev1007/1008 analyzer behavior.
tests/Lucene.Net.CodeAnalysis.Dev.CodeFixes.Tests/LuceneDev1xxx/TestLuceneDev1007_1008_DictionaryIndexerCodeFixProvider.cs Unit tests validating the new code fix output for 1007/1008.
src/Lucene.Net.CodeAnalysis.Dev/Utility/DictionaryTypeHelper.cs New helper to identify generic/non-generic dictionary indexers via symbols.
src/Lucene.Net.CodeAnalysis.Dev/Utility/Descriptors.LuceneDev6xxx.cs Adds LuceneDev6000 descriptor.
src/Lucene.Net.CodeAnalysis.Dev/Utility/Descriptors.LuceneDev1xxx.cs Adds LuceneDev1007 and LuceneDev1008 descriptors.
src/Lucene.Net.CodeAnalysis.Dev/Resources.resx Adds titles/descriptions/messages for 1007/1008/6000.
src/Lucene.Net.CodeAnalysis.Dev/LuceneDev6xxx/LuceneDev6000_NonGenericDictionaryIndexerAnalyzer.cs New analyzer implementation for non-generic IDictionary indexer reads.
src/Lucene.Net.CodeAnalysis.Dev/LuceneDev1xxx/LuceneDev1007_1008_DictionaryIndexerAnalyzer.cs New analyzer implementation for generic dictionary indexer reads.
src/Lucene.Net.CodeAnalysis.Dev.Sample/LuceneDev6xxx/LuceneDev6000_NonGenericDictionaryIndexerSample.cs Sample demonstrating LuceneDev6000 diagnostic.
src/Lucene.Net.CodeAnalysis.Dev.Sample/LuceneDev1xxx/LuceneDev1007_1008_DictionaryIndexerSample.cs Sample demonstrating LuceneDev1007/1008 diagnostics.
src/Lucene.Net.CodeAnalysis.Dev.CodeFixes/LuceneDev1xxx/LuceneDev1007_1008_DictionaryIndexerCodeFixProvider.cs New code fix provider converting return dict[key]; to TryGetValue pattern.

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

Comment thread src/Lucene.Net.CodeAnalysis.Dev/Utility/DictionaryTypeHelper.cs Outdated
- Code fix: only offer TryGetValue rewrite when receiver type exposes
  an accessible public instance TryGetValue(TKey, out TValue) method,
  to avoid generating non-compiling code when the method is only
  available via explicit interface implementation.
- Add unit test verifying PickLocalName collision-avoidance produces
  a unique name (value1) when 'value' is already in scope.
- Fix misleading comment in DictionaryTypeHelper.IsGenericDictionaryIndexer
  to match actual behavior.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@paulirwin paulirwin merged commit 3a5a687 into apache:main Apr 26, 2026
8 checks passed
@paulirwin paulirwin deleted the issue/1168 branch April 26, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:new-feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task: Review generic dictionary value retrieval to ensure that TryGetValue() is used instead of dic[key]

2 participants