Implement LuceneDev1007, 1008, 6000 Analyzers & CodeFix with Unit Tests#27
Merged
paulirwin merged 2 commits intoapache:mainfrom Apr 26, 2026
Merged
Implement LuceneDev1007, 1008, 6000 Analyzers & CodeFix with Unit Tests#27paulirwin merged 2 commits intoapache:mainfrom
paulirwin merged 2 commits intoapache:mainfrom
Conversation
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>
There was a problem hiding this comment.
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/Hashtableindexer reads. - Adds a code fix that rewrites
return dict[key];into aTryGetValue-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.
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds diagnostics for dictionary indexer usage ported from Java:
indexer read with a value-type value — may throw KeyNotFoundException.
Includes a code fix for LuceneDev1007/1008 that rewrites the common
return dict[key];pattern intoreturn dict.TryGetValue(key, out var value) ? value : default;.Fixes #1168
🤖 Generated with Claude Code