-
Notifications
You must be signed in to change notification settings - Fork 466
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
Warn for platform specific type used as generic type parameter #4390
Conversation
CheckOperationAttributes(operation, context, platformSpecificOperations, platformSpecificMembers, msBuildPlatforms, typeArgument); | ||
} | ||
} | ||
else if (method.ReceiverType is INamedTypeSymbol namedType && namedType.IsGenericType) |
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.
method.IsGenericMethod
could be used for generic method invocation, but for a constructor with a generic type parameter method.IsGenericMethod
is somehow false and the method
has no any info about the generic type, method.ReceiverType
has the generic constructor info we need
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzer.cs
Outdated
Show resolved
Hide resolved
.../UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.cs
Show resolved
Hide resolved
.../UnitTests/Microsoft.NetCore.Analyzers/InteropServices/PlatformCompatibilityAnalyzerTests.cs
Show resolved
Hide resolved
[SupportedOSPlatform(""windows"")] | ||
class WindowsOnlyType {} | ||
|
||
class GenericClass<T> {} |
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 we add a test such as below:
using System.Runtime.Versioning;
class GenericClass<T>
{
}
[SupportedOSPlatform("windows")]
class WindowsOnlyType { }
class DerivedWindowsOnlyType : GenericClass<WindowsOnlyType> { }
class Test
{
void M<T>()
{
var t = new DerivedWindowsOnlyType(); // Should be flagged
}
}
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.
Another test suggestion:
using System.Runtime.Versioning;
class GenericClass<T>
{
}
[SupportedOSPlatform("windows")]
class WindowsOnlyType { }
class Test<T>
where T : WindowsOnlyType
{
void M()
{
var t = new GenericClass<T>(); // Should be flagged.
}
}
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 am not sure it could be flagged, i will try
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 these scenarios related to the attribute's inheritance and the platform attributes defined as [AttributeUsage(AttributeTargets.Assembly | ... , Inherited = false)]
. We are not supporting inherited type even for non-generic case. So I don't think these scenarios should be covered/flagged correct me if i am wrong @terrajobst
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.
We might want to warn in case the base constructor is doing any platform-specific operation. Created separate issue for discussing and further handling this scenario #4404
@buyaa-n I think you want to target this PR to |
Codecov Report
@@ Coverage Diff @@
## release/5.0.2xx #4390 +/- ##
===================================================
+ Coverage 95.71% 95.77% +0.06%
===================================================
Files 1182 1174 -8
Lines 274766 269538 -5228
Branches 18787 16826 -1961
===================================================
- Hits 262996 258154 -4842
+ Misses 9648 9273 -375
+ Partials 2122 2111 -11 |
That is right, changing target, but seems i need a new branch based on release\5.0.2xx branch |
Closing this PR as its need to target |
Warn when platform-specific type used as generic type parameter, useful when a type created using reflections
Fixes #4362