-
Notifications
You must be signed in to change notification settings - Fork 147
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
VSTHRD114: Do not return null from non-async Task method #596
Conversation
...tudio.Threading.Analyzers.Tests/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
...tudio.Threading.Analyzers.Tests/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
catch (Exception ex) when (LaunchDebuggerExceptionFilter()) | ||
{ | ||
var messageBuilder = new StringBuilder(); | ||
messageBuilder.AppendLine("Analyzer failure while processing syntax at"); |
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.
Do multi-line exception messages get printed alright at all the interesting points? (e.g. command line builds, VS error list, infobar, etc.)
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.
Honestly I don't know and I don't know all contexts I will need to test so I will move to a single-line message. From my tests it is ok with infobar and error list.
src/Microsoft.VisualStudio.Threading.Analyzers.Tests/MultiAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
...oft.VisualStudio.Threading.Analyzers/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzer.cs
Outdated
Show resolved
Hide resolved
...oft.VisualStudio.Threading.Analyzers/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzer.cs
Outdated
Show resolved
Hide resolved
...tudio.Threading.Analyzers.Tests/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
...tudio.Threading.Analyzers.Tests/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzerTests.cs
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.
Great contribution. Thank you! I like your set of tests as well, and your use of Operation for a very simple analyzer is great!
...tudio.Threading.Analyzers.Tests/VSTHRD112AvoidNullReturnInNonAsyncTaskMethodAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
/// Finds await expressions on <see cref="Task"/> that do not use <see cref="Task.ConfigureAwait(bool)"/>. | ||
/// Also works on <see cref="ValueTask"/>. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
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.
Can you add VB as a supported language here?
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.
Do you want me to introduce vbnet tests?
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.
Just one or two sanity tests in VB would be great, yes.
@AArnott I have added 2 code fix for the rule. I don't have any answer from @sharwell regarding the use of lightup so I am not sure I will be able to have the nullable enable context handled. If that's ok for you, I suggest that we move forward with this PR and I soon as I get an answer I take care of adding a new PR to handle the nullable context. |
src/Microsoft.VisualStudio.Threading.Analyzers.Tests/Lightup/LightupHelpersTests.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.VisualStudio.Threading.Analyzers.Tests/Helpers/CSharpCodeFixVerifier`2+Test.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.VisualStudio.Threading.Analyzers.Tests/Helpers/CSharpCodeFixVerifier`2+Test.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.VisualStudio.Threading.Analyzers/VSTHRD112AvoidReturningNullTaskAnalyzer.cs
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.
THIRD-PARTY-NOTICES.txt needs to be updated with this content (for the lightup layer):
Co-Authored-By: Sam Harwell <sam@tunnelvisionlabs.com>
returnOperation.ReturnedValue.ConstantValue.HasValue && | ||
returnOperation.ReturnedValue.ConstantValue.Value == null && | ||
returnOperation.ReturnedValue.Syntax is { } returnedValueSyntax && | ||
!context.Compilation.GetSemanticModel(returnedValueSyntax.SyntaxTree).GetNullableContext(returnedValueSyntax.SpanStart).AnnotationsEnabled()) |
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 did something similar on roslyn-analyzers and I end-up with a RS1030
so I think we shouldn't be moving forward like this. @sharwell any idea of how I could rework this? On dotnet/roslyn-analyzers#3352 we decided to rewrite the analyzer as syntax analyzer but that seems more complex in here.
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.
@sharwell @AArnott I have tried a couple of things and I cannot find any good way (except for a full node analyzer) to get the semantic model using the operation analyzer. I am not sure how to move forward:
1/ Revert the part trying to handle the #nullable enable
part.
2/ Keep current implementation. That's probably a bad idea as an analyzer was written to prevent this case.
3/ Move to a syntax analyzer.
4/ Any other way?
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.
Personally I think we can drop no. 2.
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.
It's ok when bumping the version of roslyn we are using.
@@ -23,7 +23,7 @@ | |||
<PackageReference Include="MicroBuild.VisualStudio" Version="$(MicroBuildVersion)" PrivateAssets="all" /> | |||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.0.1-beta1.20209.1" /> | |||
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" Version="1.0.1-beta1.20209.1" /> | |||
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.8.2" /> | |||
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.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.
Min version where C# 8 language exists.
...Microsoft.VisualStudio.Threading.Analyzers/Microsoft.VisualStudio.Threading.Analyzers.csproj
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.
Please do a full review again.
For some reason now the tests are considered as a nullable disabled context... |
I don't so much care that someone with NRT will get duplicate diagnostics when returning null.
a1d1d41
to
d827da7
Compare
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!
Fix #593