-
Notifications
You must be signed in to change notification settings - Fork 470
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
Implement DoNotIgnoreAttribute analyzer for method return values #6546
Implement DoNotIgnoreAttribute analyzer for method return values #6546
Conversation
...Analyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValueCSharpTests.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6546 +/- ##
=========================================
Coverage 96.43% 96.44%
=========================================
Files 1372 1377 +5
Lines 320265 321768 +1503
Branches 10293 10302 +9
=========================================
+ Hits 308844 310323 +1479
- Misses 8967 8988 +21
- Partials 2454 2457 +3 |
0e0c86c
to
2c77260
Compare
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...Analyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValueCSharpTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.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.
I think there are no tests for a non-async method that are awaitable (e.g, returns a Task
, Task<T>
, etc)
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/DoNotIgnoreReturnValue.cs
Outdated
Show resolved
Hide resolved
…ase VB test coverage.
if (callSite.Parent?.Kind is OperationKind.Await) | ||
{ | ||
// It would be an authoring mistake, but ensure the async method returns a value | ||
if (((IAwaitOperation)callSite.Parent).Type.SpecialType is SpecialType.System_Void) |
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.
nit: The cast here might be unnecessary.
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.
Consider testing the attribute on lambda and local functions. e.g:
Func<string> x = [return: DoNotIgnore] () => "";
x();
I'm not sure if this will work with the current implementation, or whether it's an important scenario for now.
Other than that LGTM.
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.
public class C
{
public void M()
{
for (int i = 0; i < 100; DoNotIgnore(i++))
{
}
}
[return: DoNotIgnore]
public string DoNotIgnore(int i) => i.ToString();
}
Quite unrealistic scenario. So feel free to not handle it :)
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.
[return: DoNotIgnore]
public string M1() => "";
// Do we want to produce a diagnostic (with a different ID) suggesting M2 to be annotated with DoNotIgnore?
// If yes, do we want to take it a step further and do flow analysis to flag more cases?
public string M2() => M1();
namespace System.Diagnostics.CodeAnalysis | ||
{ | ||
[System.AttributeUsage(System.AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] | ||
internal class DoNotIgnoreAttribute : System.Attribute |
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 curious when we are going to add the attribute in runtime and apply it to methods? It would be helpful to test the analyzer with real usages
if (((IAwaitOperation)callSite.Parent).Type.SpecialType is SpecialType.System_Void) | ||
{ | ||
/* | ||
* See https://github.com/dotnet/roslyn/issues/67616 |
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.
NIT: sounds like its need an action after the bug fixed, might want to add TODO here or wherever else is appropriate
* See https://github.com/dotnet/roslyn/issues/67616 | |
* TODO: https://github.com/dotnet/roslyn/issues/67616 |
} | ||
} | ||
"""); | ||
} |
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.
NIT: More test idea if it makes sense to add: Tuple/ValueTuple
class C
{
[return: System.Diagnostics.CodeAnalysis.DoNotIgnore]
string AnnotatedMethod() => null;
void M()
{
(int a, string x) value = (1, AnnotatedMethod());
}
}
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.
Overall LGTM, thanks!
We had some offline discussions about this analyzer and its potential relationship to IDE0058: Remove unnecessary expression value - .NET | Microsoft Learn and CA1806: Do not ignore method results (code analysis) - .NET | Microsoft Learn. We have not had the capacity to move that design discussion forward enough to have confidence in landing in a good place during .NET 8. I'm going to close this PR, and when we're able to resume those discussions we will update dotnet/runtime#34098. |
Contributes to dotnet/runtime#34098, adding support for method return values that are ignored.
Notable details from the approved API:
I will submit a separate PR with another analyzer that adds supprot for out param values that are ignored (as that one is more involved).