a new attribute to allow custom attributes to indicate that the decorated member is not unused (prevent invalid IDE0051) #67817
Replies: 2 comments 3 replies
-
I can think of several scenarios I have for this kind of thing. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[Suppress("IDE0051")]
public class FactAttribute : Attribute { ... } I wonder about class or assembly level attributes that target a member by type/name. [Suppress("xxxxxx","yyyyyy","zzzzzz")]
[SupressTarget(nameof(PreApplicationStartMethod.Type),nameof(PreApplicationStartMethod.MethodName))]
public class PreApplicationStartMethodAttribute : Attribute { ... } I appreciate that we are unlike to get this backported to net4x (although it would be nice), and that this specific attribute doesn't actually cause a problem, but I wanted to flag another scenario that this could be useful for. Perhaps |
Beta Was this translation helpful? Give feedback.
-
You could consider writing a diagnostic suppressor which recognizes attributes either whose names are either baked-in or whose definitions are annotated with some attribute you know about, and suppresses any known "unused member" warnings on them. Our own docs are not extremely complete here but this StackOverflow answer may be a good start. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is coming from my comment in #34719 (comment).
Currently there are some cases where there is no way (apart from an explicit suppression on a case-by-case basis) to get rid of IDE0051.
One example is this xUnit test, another is in the issue linked above:
xUnit allows tests to be private, but this causes IDE0051. It can be suppressed (individually, per file, per test project), but that is inconvenient - it is unnecessary noise in the code, and if done on file or project level, can hide other, valid cases when IDE0051 should be raised.
It would be ideal if a custom attribute (like xUnit's
Fact
) could indicate that the decorated member is actually used - similar to what theDynamicallyAccessedMembers
attribute can be used for now.E.g. there could exist a
DynamicallyAccessedDecoratedMember
attribute:which would be used like this (using the xUnit example):
which would cause members decorated with
[Fact]
to be recognized as used.Would such a new
DynamicallyAccessedDecoratedMemberAttribute
be something desired?Beta Was this translation helpful? Give feedback.
All reactions