-
Notifications
You must be signed in to change notification settings - Fork 5
Implement LuceneDev6001, 6002, 6003 Analyzers & CodeFixes with Unit Tests #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Implement LuceneDev6001, 6002, 6003 Analyzers & CodeFixes with Unit Tests #14
Conversation
NightOwl888
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a full review, but I noticed a number of things that should be fixed.
Inconsistent StringComparison Behavior
Both of these IndexOf() calls are identical, being called using StringComparison.CurrentCulture. However, we are getting an error only in the first case and in the latter case we are getting a warning. We should always get an error when choosing anything other than StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase because this is a Java to .NET translation bug.
Invalid error on Char overload
The char overload is always StringComparison.Ordinal and it is better optimized than using a string. There is no IndexOf(char, StringComparison) overload, but the code fix is prompting to change the code to call one.
Prompt to change case comparison
It doesn't make much sense to prompt the user to change from case sensitive to case insensitive or vice versa. The code fix should be smart enough to suggest the same case comparison that is already selected. For example, if the value is StringComparison.CurrentCulture there should be a prompt to change to StringComparison.Ordinal. If the value is StringComparison.CurrentCultureIgnoreCase the prompt should be for StringComparison.OrdinalIgnoreCase.
Similarly, if there is no StringComparison passed to a string, we should not assume that StringComparison.OrdinalIgnoreCase was the intention.
No prompt to optimize single-character string to char on ReadOnlySpan
On string, we get a CA1866 diagnostic to change from "H" to 'H' (string to char) for performance reasons.
However, on ReadOnlySpan<char> we are not getting a prompt even though the char overload exists for IndexOf() and LastIndexOf(). So, this is a gap that the Microsoft analyzers don't cover that we should.
NOTE: I was wrong about the Microsoft analyzers not detecting escaped single-char strings. We should copy their implementation and modify it to work with spans. We can rely on CA1865-CA1867 for strings, but as I mentioned before, we should elevate the importance to warning in the Lucene.NET codebase.
Note that StartsWith() and EndsWith() do not have a char overload on spans even though they exist on string. So, it is not sensible to have analyzers suggest to switch to char in these cases.
Lucene.Net.CodeAnalysis.Dev.Sample is not set up to show diagnostics
It looks like you have some code that is commented out that is supposed to fire the diagnostics, but the idea here is that we have them shown by default (I think - @paulirwin correct me if I am wrong). It is more or less a demo showing the diagnostics in IDEs that are not Visual Studio (in VS we have the VSIX project that launches the test instance of Visual Studio with the analyzers loaded).
Also, it isn't clear what is going on with the sample for LuceneDev6003, as it has none of the target methods (IndexOf, LastIndexOf, StartsWtih, or EndsWith) in it.
Directory.Packages.props
Outdated
| </PropertyGroup> | ||
|
|
||
| <ItemGroup Label="NuGet Package Reference Versions"> | ||
| <PackageVersion Include="J2N" Version="2.1.0" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J2N apparently isn't being used. Please remove it.
| // instance through a field. | ||
|
|
||
| // 6001: Missing StringComparison argument | ||
| public static readonly DiagnosticDescriptor LuceneDev6001_MissingStringComparison = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a title, description, and message (which can be formatted with arguments) for each of these diagnostic IDs. This is the information that the user will see.
We put them in resource files so we can localize the strings later if we ever get around to it.
…add Documentation as per suggestion
|
@NightOwl888 also the test that previously failed and comment out for discussion is resolved |
Related Issue: [Lucene.NET #1211](apache/lucenenet#1211)
Summary:
This PR adds the analyzers and codefixes for the following LuceneDev rules:
LuceneDev6001 – String overloads of
StartsWith/EndsWith/IndexOf/LastIndexOfmust specifyStringComparison.Split into:
LuceneDev6001_1: MissingStringComparison→ ErrorLuceneDev6001_2: InvalidStringComparison→ WarningLuceneDev6002 – Span overloads of
StartsWith/EndsWith/IndexOf/LastIndexOfshould not specifyStringComparison.Ordinaland only allowOrdinalorOrdinalIgnoreCase.Split into:
LuceneDev6002_1: RedundantOrdinal→ WarningLuceneDev6002_2: Invalid comparison → ErrorLuceneDev6003 – Single-character string arguments should use the
charoverload instead of a string → InfoChanges included:
_1and_2for clarity and control.