-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: System.Diagnostics.CodeAnalysis equivalent of JetBrains.Annotations.MeansImplicitUseAttribute/UsedImplicitlyAttribute #110215
Comments
Tagging subscribers to this area: @tommcdon |
I'd rather this was handled by the new trimming-related I was surprised when testing it locally and seeing it ignore the attribute and still show the method as unused. Then I found this: Duplicate? |
@julealgon In my case, |
Tagging subscribers to this area: @dotnet/illink |
@MichalStrehovsky I remember we discussed something conceptually very similar to this multiple times in the past in several occasions, and the answer was always that the fact there is no (easy) way to do this (outside of |
Right, the difference is that this proposal is not in terms of trimming, but in terms of IDE experience. For both cases it kind of defeats the purpose of the tooling. The tooling tries to help you find unused code. Placing an attribute that says "this is always used" will always make it look used, even if it is in fact not used. "Go to references" in the IDE will not find anything. My take is that this game can only be won if you don't play it. Don't use patterns that construct invisible references and if you do, don't be surprised static analysis gives you experience comparable to statically analyzing JavaScript. |
One thing that helps in some cases is to rely on the compile-time |
I disagree with the statement:
Back to the example:
The
The generated Mode 1. and mode 2. are functionally equivalent, since in mode 2 the code IS statically referenced, I would rather consider the code IS used even for mode 1. Unfortunately even for mode 2, VS does not count code reference for generated code. The tooling just reported unused code falsely. |
If the tooling cannot figure out unused code 100% correctly, we should provide some guidance to the tooling, help it to do its job right. This is exactly the purpose of this proposal. |
As far as I remember, Rider and Resharper do handle this case so this is purely a VS issue and should be reported for that. |
You'll likely get more traction if you open a VS feedback issue. The VS team will look at how to solve this. If they come up with an attribute, they'll file an issue here to get it reviewed. We don't add attributes unless there's commitment to consume them. Adding attributes is easy. Consuming them is the actual work. We don't control VS/VSCode planning and schedules. |
Background and motivation
Some class members are called via reflection, or generated source code, with some special attribute, for example:
Because there is no direct reference (generated source does not count either in VS), these members are displayed "grayed" in VS, implies the code should be deleted. There are two attributes
[MeansImplicitUse]
and[UsedImplicitly]
inJetBrains.Annotations
namespace to handle this exactly: we can either apply[MeansImplicitUse]
attribute to[Initializer]
, or[UsedImplicitly]
toInitialize()
method, to mark the members as referenced.There was also a question on stackexchange.com more than 7 years ago: Best practice to mark a method that is called via reflection?
I believe we should introduce these attributes under
System.Diagnostics.CodeAnalysis
too.API Proposal
API Usage
or,
Alternative Designs
There is no other easy way to work this around.
Risks
No risk at all because it's an additional feature for compile time only.
The text was updated successfully, but these errors were encountered: