-
Notifications
You must be signed in to change notification settings - Fork 468
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
Create DoNotCompareSpanToNullAnalyzer #6838
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6838 +/- ##
========================================
Coverage 96.46% 96.46%
========================================
Files 1427 1432 +5
Lines 341928 342196 +268
Branches 11268 11280 +12
========================================
+ Hits 329848 330113 +265
Misses 9231 9231
- Partials 2849 2852 +3 |
# Conflicts: # src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
# Conflicts: # src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif # src/NetAnalyzers/RulesMissingDocumentation.md
# Conflicts: # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx # src/NetAnalyzers/RulesMissingDocumentation.md
@buyaa-n @carlossanlop Can I get a review on this? |
# Conflicts: # src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md # src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif # src/NetAnalyzers/RulesMissingDocumentation.md # src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt
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.
Thank you @CollinAlpert, overall the PR looks good.
Testing with runtime found 20 hits, all looks valid. Seems the fixer is not working for Debug.Assert(conditional)
statements, please check.
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Usage/DoNotCompareSpanToNullTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
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.
I left a couple suggestions for the message/description.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
Thank you @gewarren looks much better! |
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.
@CollinAlpert thank you for addressing feedback, looks good, but there were different suggestion on the PR that fixing findings in runtime that changes the requirement:
Comparing Span with
default
is equally problematic as comparing with null. The analyzer should flag comparing withdefault
as well.
Please apply this suggestion by updating the suggestion in the message and removing the code-fix that offering to use default
. We could suggest remove the condition instead, but that might be cause other issues, so maybe just suggest only the IsEmpty
. And please update to flag comparing with default
too. Tag @jkotas and @stephentoub in case they have other/better suggestions.
Thank you for addressing the requested changes @CollinAlpert, comparing with 'default' flagged a few more cases in runtime. Using same message for both case looks confusing even comparing null and default literally same. Anyway, lets hold this analyzer PR for a little more. Based on the expert's feedback on the runtime PR, we might want to do a few more changes in this PR. |
Sure, sounds reasonable! |
How did you actually do it please? I know one can start Visual Studio and press ALT+F11 to check a complete solution. However, I would really like to know if one can run a single analyzer on a whole repo in an easy way. |
Here is the doc Testing against the Runtime and Roslyn Analyzers repo, |
Thank you @CollinAlpert, we want to only offer
Title can be: CC @gewarren for message improvement ideas. |
I'd tweak the description a bit to:
|
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.
LGTM, thank you @CollinAlpert!
This reverts commit 3e60082.
This PR adds an analyzer which warns when a
Span<T>
orReadOnlySpan<T>
is compared tonull
. Different than described in the issue it does not detectis null
/is not null
patterns, since that does not compile. For the same reason it does not analyzeMemory<T>
andReadOnlyMemory<T>
.Fixes dotnet/runtime#84265.