Disable warnings for classes on a broader scope than class or file
In our codebase there are a bunch of classes which basically reflect the layout of our database tables.
These classes are public and have public properties with getters and setters. However, they are never instantiated directly by our code, neither are setters accessed by our code.
Instead, our ORM library (ServiceStack.OrmLite) takes care of construction and population of these class instances, doing it through reflection.
Every time I create one of these classes, I'm getting a bunch of warnings which I have to disable manually using comments or attributes:
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable ClassNeverInstantiated.Global
Could we please have a way to configure these warnings for certain class patterns so they don't have to be disabled manually?
A couple of solutions pop into my mind about how we could configure disabled warnings:
- If a class is in a certain namespace (this would be configurable on the project level as there are no namespace attributes in C#)
- If a class extends another class or implements interface
- If a class is used as a generic parameter to a generic class or method
- If a class has a certain attribute
The combination of 2 and 3 would be ideal for my purpose.
Basically, if I have a class called MyDatabaseModel and using it in OrmLite like this:
myConnection.From<MyDatabaseModel>
, I would like to see the instantiation and setter access warnings gone, because using it like this would mean that OrmLite would instantiate and populate this class for me.
Likewise, if I'm returning an instance of this class from an implementation of ServiceStack.Service.Post, this would mean that all the getters are accessed, because that's something the serializer would do.
Thanks!
Please sign in to leave a comment.
Hi Alex,
Here are several options that can help in your case:
1. Apply attribute "MeansImplicitUse" from "JetBrains.Annotations" to a generic parameter of a class or a method when this type parameter implies implicit usage of the type argument:
public class GenericClass<[MeansImplicitUse(ImplicitUseTargetFlags.WithMembers)]TClass>
{
public void GenericMethod<[MeansImplicitUse(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature, ImplicitUseTargetFlags.WithMembers)]TMethod>()
{}
}
2.Apply attribute "MeansImplicitUse" to an attribute:
[MeansImplicitUse]
public class SomeAttribute : Attribute {}
3. Apply attribute "UsedImplicitly" to the type where usage warning should be suppressed. This attribute should be set on a type itself and does not work when set on base type or interface
With Entity Framwork, I have the same problem. On a namespace called Data.Map, there are the mappers for the database (implementations of IEntityTypeConfiguration<TEntity>).
If I could disable warnigs for this namespace or to this classes on a project level config, since I can't just use a attibute because this interface is from a external library, would be the better than just use comments to do it.
@Augustocb,
Here are corresponding feature requests for that: https://youtrack.jetbrains.com/issue/RSRP-453657, https://youtrack.jetbrains.com/issue/RSRP-473844.
@Augustocb,
as far as I know, instances of IEntityTypeConfiguration are explicitly created and passed to "ApplyConfiguration" method. In this case ReSharper/Rider should not show usage inspections on the types that are used in "ApplyConfiguration". Could you provide more details about types where you need to suppress usage inspections? Is there any way to implicitly create instance of IEntityTypeConfiguration ?
Ivan Serduk,
You can call the ApplyConfigurationsFromAssembly(GetType().Assembly), that applyes the configuration automatically, using reflection (I think). You don't instantiate the classes.